summaryrefslogtreecommitdiffhomepage
path: root/src/http.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/http.go')
-rw-r--r--src/http.go82
1 files changed, 52 insertions, 30 deletions
diff --git a/src/http.go b/src/http.go
index e944af2..6feb865 100644
--- a/src/http.go
+++ b/src/http.go
@@ -1,4 +1,4 @@
-package src
+package app
import (
"cmp"
@@ -8,14 +8,14 @@ import (
"log"
"math"
"net/http"
- "os"
- "path/filepath"
"slices"
"strconv"
"strings"
+ . "git.thomasvoss.com/euro-cash.eu/pkg/try"
+
+ "git.thomasvoss.com/euro-cash.eu/src/dbx"
"git.thomasvoss.com/euro-cash.eu/src/email"
- "git.thomasvoss.com/euro-cash.eu/src/mintage"
)
type middleware = func(http.Handler) http.Handler
@@ -24,30 +24,26 @@ func Run(port int) {
fs := http.FileServer(http.Dir("static"))
final := http.HandlerFunc(finalHandler)
mux := http.NewServeMux()
+
+ mwareB := chain(firstHandler, i18nHandler) // [B]asic
+ mwareC := chain(mwareB, countryHandler) // [C]ountry
+ mwareM := chain(mwareC, mintageHandler) // [M]intage
+
+ mux.Handle("GET /codes/", fs)
mux.Handle("GET /designs/", fs)
mux.Handle("GET /favicon.ico", fs)
mux.Handle("GET /fonts/", fs)
+ mux.Handle("GET /storage/", fs)
mux.Handle("GET /style.min.css", fs)
- mux.Handle("GET /coins/mintages", chain(
- firstHandler,
- i18nHandler,
- countryHandler,
- mintageHandler,
- )(final))
- mux.Handle("GET /coins/designs", chain(
- firstHandler,
- i18nHandler,
- countryHandler,
- )(final))
- mux.Handle("GET /", chain(
- firstHandler,
- i18nHandler,
- )(final))
+ mux.Handle("GET /coins/designs", mwareC(final))
+ mux.Handle("GET /coins/mintages", mwareM(final))
+ mux.Handle("GET /collecting/crh", mwareC(final))
+ mux.Handle("GET /", mwareB(final))
mux.Handle("POST /language", http.HandlerFunc(setUserLanguage))
portStr := ":" + strconv.Itoa(port)
log.Println("Listening on", portStr)
- log.Fatal(http.ListenAndServe(portStr, mux))
+ Try(http.ListenAndServe(portStr, mux))
}
func chain(xs ...middleware) middleware {
@@ -142,20 +138,14 @@ func mintageHandler(next http.Handler) http.Handler {
td.Type = "circ"
}
- path := filepath.Join("data", "mintages", td.Code)
- f, err := os.Open(path)
- if err != nil {
- throwError(http.StatusInternalServerError, err, w, r)
- return
- }
- defer f.Close()
-
- td.Mintages, err = mintage.Parse(f, path)
+ var err error
+ td.Mintages, err = dbx.GetMintages(td.Code)
if err != nil {
throwError(http.StatusInternalServerError, err, w, r)
return
}
+ processMintages(&td.Mintages, td.Type)
next.ServeHTTP(w, r)
})
}
@@ -189,7 +179,7 @@ func throwError(status int, err error, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(status)
go func() {
if err := email.ServerError(err); err != nil {
- log.Print(err)
+ log.Println(err)
}
}()
errorTmpl.Execute(w, struct {
@@ -200,3 +190,35 @@ func throwError(status int, err error, w http.ResponseWriter, r *http.Request) {
Msg: http.StatusText(status),
})
}
+
+func processMintages(md *dbx.MintageData, typeStr string) {
+ var typ int
+ switch typeStr {
+ case "nifc":
+ typ = dbx.TypeNifc
+ case "proof":
+ typ = dbx.TypeProof
+ default:
+ typ = dbx.TypeCirc
+ }
+
+ md.Standard = slices.DeleteFunc(md.Standard,
+ func(x dbx.MSRow) bool { return x.Type != typ })
+ md.Commemorative = slices.DeleteFunc(md.Commemorative,
+ func(x dbx.MCRow) bool { return x.Type != typ })
+ slices.SortFunc(md.Standard, func(x, y dbx.MSRow) int {
+ if x.Year != y.Year {
+ return x.Year - y.Year
+ }
+ return strings.Compare(x.Mintmark, y.Mintmark)
+ })
+ slices.SortFunc(md.Commemorative, func(x, y dbx.MCRow) int {
+ if x.Year != y.Year {
+ return x.Year - y.Year
+ }
+ if x.Number != y.Number {
+ return x.Number - y.Number
+ }
+ return strings.Compare(x.Mintmark, y.Mintmark)
+ })
+}