mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-24 21:19:19 +00:00
login page tweaks
This commit is contained in:
parent
73b7144394
commit
85a114e591
@ -4,8 +4,8 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ func (afs assetsfs) Open(name string) (fs.File, error) {
|
|||||||
return os.DirFS("src/assets").Open(name)
|
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
|
var tmpl *template.Template
|
||||||
tmpl, found := FS.templates[path]
|
tmpl, found := FS.templates[path]
|
||||||
if !found {
|
if !found {
|
||||||
@ -39,6 +39,11 @@ func Render(path string, writer io.Writer, data interface{}) {
|
|||||||
FS.templates[path] = tmpl
|
FS.templates[path] = tmpl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return tmpl
|
||||||
|
}
|
||||||
|
|
||||||
|
func Render(path string, writer io.Writer, data interface{}) {
|
||||||
|
tmpl := Template(path)
|
||||||
tmpl.Execute(writer, data)
|
tmpl.Execute(writer, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,13 +24,17 @@
|
|||||||
<body>
|
<body>
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
<img src="./static/graphicarts/anchor.svg" alt="">
|
<img src="./static/graphicarts/anchor.svg" alt="">
|
||||||
|
{% if .error %}
|
||||||
|
<div class="text-danger text-center my-3">{% .error %}</div>
|
||||||
|
{% end %}
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="username">Username</label>
|
<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>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="password">Password</label>
|
<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>
|
</div>
|
||||||
<button class="btn btn-block btn-default" type="submit">Login</button>
|
<button class="btn btn-block btn-default" type="submit">Login</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -3,6 +3,7 @@ package router
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -34,6 +35,12 @@ func (c *Context) JSON(status int, data interface{}) {
|
|||||||
c.Out.Write([]byte("\n"))
|
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) {
|
func (c *Context) VarInt64(key string) (int64, error) {
|
||||||
if val, ok := c.Vars[key]; ok {
|
if val, ok := c.Vars[key]; ok {
|
||||||
return strconv.ParseInt(val, 10, 64)
|
return strconv.ParseInt(val, 10, 64)
|
||||||
|
@ -41,12 +41,12 @@ func (m *authMiddleware) handler(c *router.Context) {
|
|||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
// TODO: show error
|
// TODO: show error
|
||||||
c.Out.Header().Set("Content-Type", "text/html")
|
c.HTML(http.StatusOK, assets.Template("login.html"), map[string]string{
|
||||||
assets.Render("login.html", c.Out, nil)
|
"username": username,
|
||||||
|
"error": "Invalid username/password",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.HTML(http.StatusOK, assets.Template("login.html"), nil)
|
||||||
c.Out.Header().Set("Content-Type", "text/html")
|
|
||||||
assets.Render("login.html", c.Out, nil)
|
|
||||||
}
|
}
|
||||||
|
@ -51,8 +51,7 @@ func (s *Server) handler() http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleIndex(c *router.Context) {
|
func (s *Server) handleIndex(c *router.Context) {
|
||||||
c.Out.Header().Set("Content-Type", "text/html")
|
c.HTML(http.StatusOK, assets.Template("index.html"), nil)
|
||||||
assets.Render("index.html", c.Out, nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleStatic(c *router.Context) {
|
func (s *Server) handleStatic(c *router.Context) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user