diff options
Diffstat (limited to 'src/http.go')
-rw-r--r-- | src/http.go | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/src/http.go b/src/http.go index b0d5bcd..ac6f6da 100644 --- a/src/http.go +++ b/src/http.go @@ -10,6 +10,8 @@ import ( "net/http" "slices" "strconv" + "strings" + "time" . "git.thomasvoss.com/euro-cash.eu/pkg/try" @@ -34,7 +36,11 @@ func Run(port int) { mux.Handle("GET /favicon.ico", fs) mux.Handle("GET /fonts/", fs) mux.Handle("GET /storage/", fs) - mux.Handle("GET /style.min.css", fs) + if Debugp { + mux.Handle("GET /style.css", fs) + } else { + mux.Handle("GET /style.min.css", fs) + } mux.Handle("GET /coins/designs", mwareC(final)) mux.Handle("GET /coins/mintages", mwareM(final)) mux.Handle("GET /collecting/crh", mwareC(final)) @@ -57,7 +63,10 @@ func chain(xs ...middleware) middleware { func firstHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), "td", &templateData{}) + ctx := context.WithValue(r.Context(), "td", &templateData{ + Debugp: Debugp, + Printers: i18n.Printers, + }) next.ServeHTTP(w, r.WithContext(ctx)) }) } @@ -81,8 +90,9 @@ func finalHandler(w http.ResponseWriter, r *http.Request) { original page they came from. */ if path == "/language" { http.SetCookie(w, &http.Cookie{ - Name: "redirect", - Value: cmp.Or(r.Referer(), "/"), + Name: "redirect", + Value: cmp.Or(r.Referer(), "/"), + Expires: time.Now().Add(24 * time.Hour), }) } @@ -101,15 +111,16 @@ func i18nHandler(next http.Handler) http.Handler { } td := r.Context().Value("td").(*templateData) - td.Printer = cmp.Or(p, i18n.DefaultPrinter) if p == pZero { + td.Printer = bestFitLanguage(r.Header.Get("Accept-Language")) http.SetCookie(w, &http.Cookie{ Name: "redirect", Value: r.URL.Path, }) templates["/language"].Execute(w, td) } else { + td.Printer = p next.ServeHTTP(w, r) } }) @@ -188,3 +199,39 @@ func throwError(status int, err error, w http.ResponseWriter, r *http.Request) { Msg: http.StatusText(status), }) } + +func bestFitLanguage(qry string) i18n.Printer { + type option struct { + bcp string + quality float64 + } + var xs []option + + for subqry := range strings.SplitSeq(qry, ",") { + var o option + subqry = strings.TrimSpace(subqry) + parts := strings.Split(subqry, ";") + o.bcp = strings.ToLower(parts[0]) + if len(parts) == 1 { + o.quality = 1 + } else { + n, err := fmt.Sscanf(parts[1], "q=%f", &o.quality) + if n != 1 || err != nil { + /* Malformed query string; just give up */ + return i18n.DefaultPrinter + } + } + xs = append(xs, o) + } + + slices.SortFunc(xs, func(x, y option) int { + return cmp.Compare(y.quality, x.quality) + }) + + for _, x := range xs { + if p, ok := i18n.Printers[x.bcp]; ok { + return p + } + } + return i18n.DefaultPrinter +} |