diff options
author | Thomas Voss <mail@thomasvoss.com> | 2025-06-09 02:51:27 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2025-06-09 02:51:27 +0200 |
commit | 52db1d03e5067de4ba77c3a12465149d268eb3d1 (patch) | |
tree | 96286b94957e2546a789cbdcad2677b6f03b0973 /src/http.go | |
parent | 09defec0a015e7ddb118bd453ea2925a5cb9d5dc (diff) |
Begin migration towards SQLite
Diffstat (limited to 'src/http.go')
-rw-r--r-- | src/http.go | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/http.go b/src/http.go index eaaeb71..4c2f5c6 100644 --- a/src/http.go +++ b/src/http.go @@ -12,8 +12,8 @@ import ( "strconv" "strings" + "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 @@ -42,7 +42,9 @@ func Run(port int) { portStr := ":" + strconv.Itoa(port) log.Println("Listening on", portStr) - log.Fatal(http.ListenAndServe(portStr, mux)) + err := http.ListenAndServe(portStr, mux) + dbx.DB.Close() + log.Fatal(err) } func chain(xs ...middleware) middleware { @@ -138,12 +140,13 @@ func mintageHandler(next http.Handler) http.Handler { } var err error - td.Mintages, err = mintage.Parse(td.Code) + 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) }) } @@ -188,3 +191,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) + }) +} |