diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/sklad/base.tmpl | 8 | ||||
-rw-r--r-- | cmd/sklad/container.tmpl | 20 | ||||
-rw-r--r-- | cmd/sklad/main.go | 59 | ||||
-rw-r--r-- | cmd/sklad/search.tmpl | 6 | ||||
-rw-r--r-- | cmd/sklad/series.tmpl | 8 | ||||
-rw-r--r-- | cmd/sklad/session.go | 2 |
6 files changed, 57 insertions, 46 deletions
diff --git a/cmd/sklad/base.tmpl b/cmd/sklad/base.tmpl index d92a818..956bfa4 100644 --- a/cmd/sklad/base.tmpl +++ b/cmd/sklad/base.tmpl @@ -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 }} diff --git a/cmd/sklad/container.tmpl b/cmd/sklad/container.tmpl index 3300a1f..147e124 100644 --- a/cmd/sklad/container.tmpl +++ b/cmd/sklad/container.tmpl @@ -24,18 +24,18 @@ <header> <h2>{{ .Container.Id }} {{ range .Container.Path }} - <small>« <a href="/?id={{ . }}">{{ . }}</a></small> + <small>« <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 }}&remove"> + <form method=post action="container?id={{ .Container.Id }}&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>« <a href="/?id={{ . }}">{{ . }}</a></small> + <small>« <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 }}&remove"> + <form method=post action="container?id={{ .Id }}&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> diff --git a/cmd/sklad/main.go b/cmd/sklad/main.go index 5dc2174..5b68e00 100644 --- a/cmd/sklad/main.go +++ b/cmd/sklad/main.go @@ -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, ¶ms) } +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)) } diff --git a/cmd/sklad/search.tmpl b/cmd/sklad/search.tmpl index cf704cf..9fef455 100644 --- a/cmd/sklad/search.tmpl +++ b/cmd/sklad/search.tmpl @@ -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>« <a href="/?id={{ . }}">{{ . }}</a></small> + <small>« <a href="container?id={{ . }}">{{ . }}</a></small> {{ end }} </h3> </header> diff --git a/cmd/sklad/series.tmpl b/cmd/sklad/series.tmpl index 0e31e0f..fbc0e91 100644 --- a/cmd/sklad/series.tmpl +++ b/cmd/sklad/series.tmpl @@ -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 }}&remove"> + <form method=post action="series?prefix={{ .Prefix }}&remove"> <input type=submit value="Odstranit"> </form> </header> diff --git a/cmd/sklad/session.go b/cmd/sklad/session.go index 02fe0b0..23681a2 100644 --- a/cmd/sklad/session.go +++ b/cmd/sklad/session.go @@ -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) } |