sklad: support running under a prefix

This commit is contained in:
Přemysl Eric Janouch 2019-04-15 03:16:19 +02:00
parent dc0536c011
commit 5b7113905e
Signed by: p
GPG Key ID: A0420B94F92B9493
6 changed files with 57 additions and 46 deletions

View File

@ -49,14 +49,14 @@
<h1>sklad</h1> <h1>sklad</h1>
{{ block "HeaderControls" . }} {{ block "HeaderControls" . }}
<a href=/>Obaly</a> <a href="container">Obaly</a>
<a href=/series>Řady</a> <a href="series">Řady</a>
<form method=get action=/search> <form method=get action="search">
<input type=text name=q autofocus><input type=submit value="Hledat"> <input type=text name=q autofocus><input type=submit value="Hledat">
</form> </form>
<form method=post action=/logout> <form method=post action="logout">
<input type=submit value="Odhlásit"> <input type=submit value="Odhlásit">
</form> </form>
{{ end }} {{ end }}

View File

@ -24,18 +24,18 @@
<header> <header>
<h2>{{ .Container.Id }} <h2>{{ .Container.Id }}
{{ range .Container.Path }} {{ range .Container.Path }}
<small>&laquo; <a href="/?id={{ . }}">{{ . }}</a></small> <small>&laquo; <a href="container?id={{ . }}">{{ . }}</a></small>
{{ end }} {{ end }}
</h2> </h2>
<form method=post action="/label?id={{ .Container.Id }}"> <form method=post action="label?id={{ .Container.Id }}">
<input type=submit value="Vytisknout štítek"> <input type=submit value="Vytisknout štítek">
</form> </form>
<form method=post action="/?id={{ .Container.Id }}&amp;remove"> <form method=post action="container?id={{ .Container.Id }}&amp;remove">
<input type=submit value="Odstranit"> <input type=submit value="Odstranit">
</form> </form>
</header> </header>
<form method=post action="/?id={{ .Container.Id }}"> <form method=post action="container?id={{ .Container.Id }}">
<textarea name=description rows=5> <textarea name=description rows=5>
{{ .Container.Description }} {{ .Container.Description }}
</textarea> </textarea>
@ -66,7 +66,7 @@
<header> <header>
<h2>Nový obal</h2> <h2>Nový obal</h2>
</header> </header>
<form method=post action="/"> <form method=post action="container">
<textarea name=description rows=5 <textarea name=description rows=5
placeholder="Popis obalu nebo jeho obsahu"></textarea> placeholder="Popis obalu nebo jeho obsahu"></textarea>
<footer> <footer>
@ -94,15 +94,15 @@
{{ range .Children }} {{ range .Children }}
<section> <section>
<header> <header>
<h3><a href="/?id={{ .Id }}">{{ .Id }}</a> <h3><a href="container?id={{ .Id }}">{{ .Id }}</a>
{{ range .Path }} {{ range .Path }}
<small>&laquo; <a href="/?id={{ . }}">{{ . }}</a></small> <small>&laquo; <a href="container?id={{ . }}">{{ . }}</a></small>
{{ end }} {{ end }}
</h3> </h3>
<form method=post action="/label?id={{ .Id }}"> <form method=post action="label?id={{ .Id }}">
<input type=submit value="Vytisknout štítek"> <input type=submit value="Vytisknout štítek">
</form> </form>
<form method=post action="/?id={{ .Id }}&amp;remove"> <form method=post action="container?id={{ .Id }}&amp;remove">
<input type=submit value="Odstranit"> <input type=submit value="Odstranit">
</form> </form>
</header> </header>
@ -112,7 +112,7 @@
{{ if .Children }} {{ if .Children }}
<p> <p>
{{ range .Children }} {{ range .Children }}
<a href="/?id={{ .Id }}">{{ .Id }}</a> <a href="container?id={{ .Id }}">{{ .Id }}</a>
{{ end }} {{ end }}
{{ end }} {{ end }}
</section> </section>

View File

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path"
"path/filepath" "path/filepath"
"time" "time"
@ -25,24 +26,10 @@ func executeTemplate(name string, w io.Writer, data interface{}) {
} }
} }
func wrap(inner func(http.ResponseWriter, *http.Request)) func(
http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.Method == http.MethodGet {
w.Header().Set("Cache-Control", "no-store")
}
inner(w, r)
}
}
func handleLogin(w http.ResponseWriter, r *http.Request) { func handleLogin(w http.ResponseWriter, r *http.Request) {
redirect := r.FormValue("redirect") redirect := r.FormValue("redirect")
if redirect == "" { if redirect == "" {
redirect = "/" redirect = "container"
} }
session := sessionGet(w, r) session := sessionGet(w, r)
@ -81,7 +68,7 @@ func handleLogout(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(sessionContextKey{}).(*Session) session := r.Context().Value(sessionContextKey{}).(*Session)
session.LoggedIn = false session.LoggedIn = false
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "login", http.StatusSeeOther)
} }
func handleContainerPost(r *http.Request) error { func handleContainerPost(r *http.Request) error {
@ -315,6 +302,37 @@ func handleLabel(w http.ResponseWriter, r *http.Request) {
executeTemplate("label.tmpl", w, &params) executeTemplate("label.tmpl", w, &params)
} }
func handle(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.Method == http.MethodGet {
w.Header().Set("Cache-Control", "no-store")
}
switch _, base := path.Split(r.URL.Path); base {
case "login":
handleLogin(w, r)
case "logout":
sessionWrap(handleLogout)(w, r)
case "container":
sessionWrap(handleContainer)(w, r)
case "series":
sessionWrap(handleSeries)(w, r)
case "search":
sessionWrap(handleSearch)(w, r)
case "label":
sessionWrap(handleLabel)(w, r)
case "":
http.Redirect(w, r, "container", http.StatusSeeOther)
default:
http.NotFound(w, r)
}
}
func main() { func main() {
// Randomize the RNG for session string generation. // Randomize the RNG for session string generation.
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
@ -340,13 +358,6 @@ func main() {
templates[name] = template.Must(template.ParseFiles("base.tmpl", name)) templates[name] = template.Must(template.ParseFiles("base.tmpl", name))
} }
http.HandleFunc("/login", wrap(handleLogin)) http.HandleFunc("/", handle)
http.HandleFunc("/logout", sessionWrap(wrap(handleLogout)))
http.HandleFunc("/", sessionWrap(wrap(handleContainer)))
http.HandleFunc("/series", sessionWrap(wrap(handleSeries)))
http.HandleFunc("/search", sessionWrap(wrap(handleSearch)))
http.HandleFunc("/label", sessionWrap(wrap(handleLabel)))
log.Fatalln(http.ListenAndServe(address, nil)) log.Fatalln(http.ListenAndServe(address, nil))
} }

View File

@ -8,7 +8,7 @@
{{ range .Series }} {{ range .Series }}
<section> <section>
<header> <header>
<h3><a href="/series?prefix={{ .Prefix }}">{{ .Prefix }}</a></h3> <h3><a href="series?prefix={{ .Prefix }}">{{ .Prefix }}</a></h3>
<p>{{ .Description }} <p>{{ .Description }}
</header> </header>
</section> </section>
@ -21,9 +21,9 @@
{{ range .Containers }} {{ range .Containers }}
<section> <section>
<header> <header>
<h3><a href="/?id={{ .Id }}">{{ .Id }}</a> <h3><a href="container?id={{ .Id }}">{{ .Id }}</a>
{{ range .Path }} {{ range .Path }}
<small>&laquo; <a href="/?id={{ . }}">{{ . }}</a></small> <small>&laquo; <a href="container?id={{ . }}">{{ . }}</a></small>
{{ end }} {{ end }}
</h3> </h3>
</header> </header>

View File

@ -24,7 +24,7 @@
{{ else }} {{ else }}
<section> <section>
<form method=post action="/series"> <form method=post action="series">
<header> <header>
<h3>Nová řada</h3> <h3>Nová řada</h3>
<input type=text name=prefix placeholder="Prefix řady"> <input type=text name=prefix placeholder="Prefix řady">
@ -38,7 +38,7 @@
{{ range .AllSeries }} {{ range .AllSeries }}
<section> <section>
<header> <header>
<h3><a href="/series?prefix={{ .Prefix }}">{{ .Prefix }}</a></h3> <h3><a href="series?prefix={{ .Prefix }}">{{ .Prefix }}</a></h3>
{{ with $count := len .Containers }} {{ with $count := len .Containers }}
{{ if eq $count 1 }} {{ if eq $count 1 }}
<p>{{ $count }} obal <p>{{ $count }} obal
@ -48,11 +48,11 @@
<p>{{ $count }} obalů <p>{{ $count }} obalů
{{ end }} {{ end }}
{{ end }} {{ end }}
<form method=post action="/series?prefix={{ .Prefix }}"> <form method=post action="series?prefix={{ .Prefix }}">
<input type=text name=description value="{{ .Description }}" <input type=text name=description value="{{ .Description }}"
><input type=submit value="Uložit"> ><input type=submit value="Uložit">
</form> </form>
<form method=post action="/series?prefix={{ .Prefix }}&amp;remove"> <form method=post action="series?prefix={{ .Prefix }}&amp;remove">
<input type=submit value="Odstranit"> <input type=submit value="Odstranit">
</form> </form>
</header> </header>

View File

@ -50,7 +50,7 @@ func sessionWrap(inner func(http.ResponseWriter, *http.Request)) func(
// though I don't expect any substantial improvements of anything. // though I don't expect any substantial improvements of anything.
w.Header().Set("Cache-Control", "no-store") w.Header().Set("Cache-Control", "no-store")
redirect := "/login" redirect := "login"
if r.RequestURI != "/" && r.Method == http.MethodGet { if r.RequestURI != "/" && r.Method == http.MethodGet {
redirect += "?redirect=" + url.QueryEscape(r.RequestURI) redirect += "?redirect=" + url.QueryEscape(r.RequestURI)
} }