login page tweaks

This commit is contained in:
Nazar Kanaev 2021-03-17 12:17:17 +00:00
parent 73b7144394
commit 85a114e591
5 changed files with 26 additions and 11 deletions

View File

@ -4,8 +4,8 @@ import (
"embed"
"html/template"
"io"
"io/ioutil"
"io/fs"
"io/ioutil"
"os"
)
@ -23,7 +23,7 @@ func (afs assetsfs) Open(name string) (fs.File, error) {
return os.DirFS("src/assets").Open(name)
}
func Render(path string, writer io.Writer, data interface{}) {
func Template(path string) *template.Template {
var tmpl *template.Template
tmpl, found := FS.templates[path]
if !found {
@ -39,6 +39,11 @@ func Render(path string, writer io.Writer, data interface{}) {
FS.templates[path] = tmpl
}
}
return tmpl
}
func Render(path string, writer io.Writer, data interface{}) {
tmpl := Template(path)
tmpl.Execute(writer, data)
}

View File

@ -24,13 +24,17 @@
<body>
<form action="" method="post">
<img src="./static/graphicarts/anchor.svg" alt="">
{% if .error %}
<div class="text-danger text-center my-3">{% .error %}</div>
{% end %}
<div class="form-group">
<label for="username">Username</label>
<input name="username" class="form-control" id="username" autocomplete="off">
<input name="username" class="form-control" id="username" autocomplete="off"
value="{% if .username %}{% .username %}{% end %}" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" id="password" type="password">
<input name="password" class="form-control" id="password" type="password" required>
</div>
<button class="btn btn-block btn-default" type="submit">Login</button>
</form>

View File

@ -3,6 +3,7 @@ package router
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"strconv"
@ -34,6 +35,12 @@ func (c *Context) JSON(status int, data interface{}) {
c.Out.Write([]byte("\n"))
}
func (c *Context) HTML(status int, tmpl *template.Template, data interface{}) {
c.Out.WriteHeader(status)
c.Out.Header().Set("Content-Type", "text/html")
tmpl.Execute(c.Out, data)
}
func (c *Context) VarInt64(key string) (int64, error) {
if val, ok := c.Vars[key]; ok {
return strconv.ParseInt(val, 10, 64)

View File

@ -41,12 +41,12 @@ func (m *authMiddleware) handler(c *router.Context) {
return
} else {
// TODO: show error
c.Out.Header().Set("Content-Type", "text/html")
assets.Render("login.html", c.Out, nil)
c.HTML(http.StatusOK, assets.Template("login.html"), map[string]string{
"username": username,
"error": "Invalid username/password",
})
return
}
}
c.Out.Header().Set("Content-Type", "text/html")
assets.Render("login.html", c.Out, nil)
c.HTML(http.StatusOK, assets.Template("login.html"), nil)
}

View File

@ -51,8 +51,7 @@ func (s *Server) handler() http.Handler {
}
func (s *Server) handleIndex(c *router.Context) {
c.Out.Header().Set("Content-Type", "text/html")
assets.Render("index.html", c.Out, nil)
c.HTML(http.StatusOK, assets.Template("index.html"), nil)
}
func (s *Server) handleStatic(c *router.Context) {