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>
{{ block "HeaderControls" . }}
<a href=/>Obaly</a>
<a href=/series>Řady</a>
<a href="container">Obaly</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">
</form>
<form method=post action=/logout>
<form method=post action="logout">
<input type=submit value="Odhlásit">
</form>
{{ end }}

View File

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

View File

@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"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) {
redirect := r.FormValue("redirect")
if redirect == "" {
redirect = "/"
redirect = "container"
}
session := sessionGet(w, r)
@ -81,7 +68,7 @@ func handleLogout(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value(sessionContextKey{}).(*Session)
session.LoggedIn = false
http.Redirect(w, r, "/", http.StatusSeeOther)
http.Redirect(w, r, "login", http.StatusSeeOther)
}
func handleContainerPost(r *http.Request) error {
@ -315,6 +302,37 @@ func handleLabel(w http.ResponseWriter, r *http.Request) {
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() {
// Randomize the RNG for session string generation.
rand.Seed(time.Now().UnixNano())
@ -340,13 +358,6 @@ func main() {
templates[name] = template.Must(template.ParseFiles("base.tmpl", name))
}
http.HandleFunc("/login", wrap(handleLogin))
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)))
http.HandleFunc("/", handle)
log.Fatalln(http.ListenAndServe(address, nil))
}

View File

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

View File

@ -24,7 +24,7 @@
{{ else }}
<section>
<form method=post action="/series">
<form method=post action="series">
<header>
<h3>Nová řada</h3>
<input type=text name=prefix placeholder="Prefix řady">
@ -38,7 +38,7 @@
{{ range .AllSeries }}
<section>
<header>
<h3><a href="/series?prefix={{ .Prefix }}">{{ .Prefix }}</a></h3>
<h3><a href="series?prefix={{ .Prefix }}">{{ .Prefix }}</a></h3>
{{ with $count := len .Containers }}
{{ if eq $count 1 }}
<p>{{ $count }} obal
@ -48,11 +48,11 @@
<p>{{ $count }} obalů
{{ 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=submit value="Uložit">
</form>
<form method=post action="/series?prefix={{ .Prefix }}&amp;remove">
<form method=post action="series?prefix={{ .Prefix }}&amp;remove">
<input type=submit value="Odstranit">
</form>
</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.
w.Header().Set("Cache-Control", "no-store")
redirect := "/login"
redirect := "login"
if r.RequestURI != "/" && r.Method == http.MethodGet {
redirect += "?redirect=" + url.QueryEscape(r.RequestURI)
}