summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/dbx/db.go7
-rw-r--r--src/http.go8
-rw-r--r--src/i18n.go2
-rw-r--r--src/templates.go40
-rw-r--r--src/templates/coins-designs-ad.html.tmpl2
-rw-r--r--src/templates/coins-designs-be.html.tmpl5
-rw-r--r--src/templates/coins-designs-hr.html.tmpl8
-rw-r--r--src/try/try.go14
8 files changed, 41 insertions, 45 deletions
diff --git a/src/dbx/db.go b/src/dbx/db.go
index 5bd65a5..fcb345e 100644
--- a/src/dbx/db.go
+++ b/src/dbx/db.go
@@ -9,9 +9,9 @@ import (
"sort"
"strings"
+ "git.thomasvoss.com/euro-cash.eu/pkg/atexit"
+ . "git.thomasvoss.com/euro-cash.eu/pkg/try"
"github.com/mattn/go-sqlite3"
-
- . "git.thomasvoss.com/euro-cash.eu/src/try"
)
var (
@@ -22,6 +22,7 @@ var (
func Init(sqlDir fs.FS) {
db = Try2(sql.Open("sqlite3", DBName))
Try(db.Ping())
+ atexit.Register(Close)
Try(applyMigrations(sqlDir))
/* TODO: Remove debug code */
@@ -120,7 +121,7 @@ func applyMigrations(dir fs.FS) error {
if err := tx.Commit(); err != nil {
return err
}
- log.Printf("Applied database migration ‘%s’", f)
+ log.Printf("Applied database migration ‘%s’\n", f)
}
if last != "" {
diff --git a/src/http.go b/src/http.go
index a5897b4..6feb865 100644
--- a/src/http.go
+++ b/src/http.go
@@ -12,6 +12,8 @@ import (
"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"
)
@@ -41,9 +43,7 @@ func Run(port int) {
portStr := ":" + strconv.Itoa(port)
log.Println("Listening on", portStr)
- err := http.ListenAndServe(portStr, mux)
- dbx.Close()
- log.Fatal(err)
+ Try(http.ListenAndServe(portStr, mux))
}
func chain(xs ...middleware) middleware {
@@ -179,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 {
diff --git a/src/i18n.go b/src/i18n.go
index 726a75c..cb54e28 100644
--- a/src/i18n.go
+++ b/src/i18n.go
@@ -261,7 +261,7 @@ func (p Printer) M(val any) string {
default:
go func() {
if err := email.ServerError(badMType{"TODO"}); err != nil {
- log.Print(err)
+ log.Println(err)
}
}()
/* Hopefully this never happens */
diff --git a/src/templates.go b/src/templates.go
index 8479759..4deeb67 100644
--- a/src/templates.go
+++ b/src/templates.go
@@ -7,6 +7,9 @@ import (
"log"
"strings"
+ . "git.thomasvoss.com/euro-cash.eu/pkg/try"
+ "git.thomasvoss.com/euro-cash.eu/pkg/watch"
+
"git.thomasvoss.com/euro-cash.eu/src/dbx"
)
@@ -31,29 +34,36 @@ var (
}
)
-func BuildTemplates(dir fs.FS) {
- ents, err := fs.ReadDir(dir, ".")
- if err != nil {
- log.Fatal(err)
- }
-
+func BuildTemplates(dir fs.FS, debugp bool) {
+ ents := Try2(fs.ReadDir(dir, "."))
notFoundTmpl = buildTemplate(dir, "-404")
errorTmpl = buildTemplate(dir, "-error")
-
templates = make(map[string]*template.Template, len(ents))
+
for _, e := range ents {
- path := "/"
- name := strings.TrimSuffix(e.Name(), ".html.tmpl")
- switch {
- case name[0] == '-':
- continue
- case name != "index":
- path += strings.ReplaceAll(name, "-", "/")
+ name := e.Name()
+ buildAndSetTemplate(dir, name)
+ if debugp {
+ go watch.FileFS(dir, name, func() {
+ buildAndSetTemplate(dir, name)
+ log.Printf("Template ‘%s’ updated\n", name)
+ })
}
- templates[path] = buildTemplate(dir, name)
}
}
+func buildAndSetTemplate(dir fs.FS, name string) {
+ path := "/"
+ name = strings.TrimSuffix(name, ".html.tmpl")
+ switch {
+ case name[0] == '-':
+ return
+ case name != "index":
+ path += strings.ReplaceAll(name, "-", "/")
+ }
+ templates[path] = buildTemplate(dir, name)
+}
+
func buildTemplate(dir fs.FS, name string) *template.Template {
names := [...]string{"-base", "-navbar", name}
for i, s := range names {
diff --git a/src/templates/coins-designs-ad.html.tmpl b/src/templates/coins-designs-ad.html.tmpl
index c42930a..20199bb 100644
--- a/src/templates/coins-designs-ad.html.tmpl
+++ b/src/templates/coins-designs-ad.html.tmpl
@@ -85,7 +85,7 @@
{{ .T `
The bottom of the coat of arms has the motto ‘%sVIRTVS VNITA
FORTIOR%s’ (‘UNITED VIRTUE IS STRONGER’).
- ` `<span lang="la">` `</span>` }}
+ ` `<span lang="la">` `</span>` | safe }}
</p>
</main>
{{ end }} \ No newline at end of file
diff --git a/src/templates/coins-designs-be.html.tmpl b/src/templates/coins-designs-be.html.tmpl
index 22f533f..4dcd325 100644
--- a/src/templates/coins-designs-be.html.tmpl
+++ b/src/templates/coins-designs-be.html.tmpl
@@ -21,8 +21,7 @@
target="_blank"
href="https://www.wikipedia.org/wiki/Royal_cypher"
>`
- `</a>`
- }}
+ `</a>` | safe }}
</p>
<p>
{{ .T `
@@ -44,4 +43,4 @@
` }}
</p>
</main>
-{{ end }}
+{{ end }} \ No newline at end of file
diff --git a/src/templates/coins-designs-hr.html.tmpl b/src/templates/coins-designs-hr.html.tmpl
index b6333ba..8b6976f 100644
--- a/src/templates/coins-designs-hr.html.tmpl
+++ b/src/templates/coins-designs-hr.html.tmpl
@@ -26,10 +26,10 @@
<p>
{{ .T `
The 1-, 2-, and 5 euro cent coins were designed by Maja
- Škripelj and feature a motif of the letters ‘HR’ in the
+ Škripelj and feature a motif of the letters ‘ⰘⰓ’ from the
%sGlagolitic script%s — an old Slavic script that saw use in
- Croatia up until the 19th century — with ‘HR’ representing
- Croatia’s country code.`
+ Croatia up until the 19th century — representing Croatia’s country
+ code (‘HR’ in the Latin alphabet).`
`<a
target="_blank"
href="https://www.wikipedia.org/wiki/Glagolitic_script"
@@ -80,4 +80,4 @@
`</a>` | safe }}
</p>
</main>
-{{ end }}
+{{ end }} \ No newline at end of file
diff --git a/src/try/try.go b/src/try/try.go
deleted file mode 100644
index fc086d1..0000000
--- a/src/try/try.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package try
-
-import "log"
-
-func Try(e error) {
- if e != nil {
- log.Fatal(e)
- }
-}
-
-func Try2[T any](x T, e error) T {
- Try(e)
- return x
-}