1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package main
import (
"cmp"
"context"
"errors"
"flag"
"fmt"
"log"
"math"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"git.thomasvoss.com/euro-cash.eu/lib"
"git.thomasvoss.com/euro-cash.eu/lib/mintage"
"git.thomasvoss.com/euro-cash.eu/template"
"github.com/a-h/templ"
)
var components = map[string]templ.Component{
"/": template.Root(),
"/about": template.About(),
"/coins": template.Coins(),
"/coins/designs": template.CoinsDesigns(),
"/coins/designs/nl": template.CoinsDesignsNl(),
"/coins/mintages": template.CoinsMintages(),
"/language": template.Language(),
}
func main() {
lib.InitPrinters()
port := flag.Int("port", 8080, "port number")
flag.Parse()
fs := http.FileServer(http.Dir("static"))
mux := http.NewServeMux()
mux.Handle("GET /designs/", fs)
mux.Handle("GET /favicon.ico", fs)
mux.Handle("GET /fonts/", fs)
mux.Handle("GET /style.css", fs)
mux.Handle("GET /coins/mintages", i18nHandler(mintageHandler(http.HandlerFunc(finalHandler))))
mux.Handle("GET /", i18nHandler(http.HandlerFunc(finalHandler)))
mux.Handle("POST /language", http.HandlerFunc(setUserLanguage))
portStr := ":" + strconv.Itoa(*port)
log.Println("Listening on", portStr)
log.Fatal(http.ListenAndServe(portStr, mux))
}
func finalHandler(w http.ResponseWriter, r *http.Request) {
p := r.Context().Value("printer").(lib.Printer)
/* Strip trailing slash from the URL */
path := r.URL.Path
if path != "/" && path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
if c, ok := components[path]; !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, p.T("Page not found"))
} else {
/* When a user clicks on the language button to be taken to the
language selection page, we need to set a redirect cookie so
that after selecting a language they are taken back to the
original page they came from. */
if path == "/language" {
http.SetCookie(w, &http.Cookie{
Name: "redirect",
Value: cmp.Or(r.Referer(), "/"),
})
}
template.Base(c).Render(r.Context(), w)
}
}
func i18nHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var p, pZero lib.Printer
if c, err := r.Cookie("locale"); errors.Is(err, http.ErrNoCookie) {
log.Println("Language cookie not set")
} else {
var ok bool
p, ok = lib.Printers[strings.ToLower(c.Value)]
if !ok {
log.Printf("Language ā%sā is unsupported\n", c.Value)
}
}
ctx := context.WithValue(
r.Context(), "printer", cmp.Or(p, lib.DefaultPrinter))
if p == pZero {
http.SetCookie(w, &http.Cookie{
Name: "redirect",
Value: r.URL.Path,
})
template.Base(template.Language()).Render(ctx, w)
} else {
next.ServeHTTP(w, r.WithContext(ctx))
}
})
}
func mintageHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
countries := lib.SortedCountries(
r.Context().Value("printer").(lib.Printer))
code := strings.ToLower(cmp.Or(r.FormValue("code"), countries[0].Code))
ctype := strings.ToLower(cmp.Or(r.FormValue("type"), "circ"))
path := filepath.Join("data", "mintages", code)
f, _ := os.Open(path) // TODO: Handle error
defer f.Close()
set, _ := mintage.Parse(f, path) // TODO: Handle error
var idx mintage.CoinType
switch ctype {
case "circ":
idx = mintage.TypeCirculated
case "nifc":
idx = mintage.TypeNIFC
case "proof":
idx = mintage.TypeProof
}
ctx := context.WithValue(r.Context(), "code", code)
ctx = context.WithValue(ctx, "type", ctype)
ctx = context.WithValue(ctx, "table", set.Tables[idx])
ctx = context.WithValue(ctx, "countries", countries)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func setUserLanguage(w http.ResponseWriter, r *http.Request) {
loc := r.FormValue("locale")
_, ok := lib.Printers[strings.ToLower(loc)]
if !ok {
w.WriteHeader(http.StatusUnprocessableEntity)
fmt.Fprintf(w, "Locale ā%sā is invalid or unsupported", loc)
return
}
http.SetCookie(w, &http.Cookie{
Name: "locale",
Value: loc,
MaxAge: math.MaxInt32,
})
if c, err := r.Cookie("redirect"); errors.Is(err, http.ErrNoCookie) {
http.Redirect(w, r, "/", http.StatusFound)
} else {
http.SetCookie(w, &http.Cookie{
Name: "redirect",
MaxAge: -1,
})
http.Redirect(w, r, c.Value, http.StatusFound)
}
}
|