From 16b01fd1dfb8797b5529ea444f5b26cd26be9c3c Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sun, 6 Apr 2025 10:10:37 +0200 Subject: refactor: embed countries.json and templates in binary --- lexsurf.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lexsurf.go b/lexsurf.go index 45eca6a..d0fb690 100644 --- a/lexsurf.go +++ b/lexsurf.go @@ -1,6 +1,7 @@ package main import ( + "embed" "encoding/json" "io/ioutil" "log" @@ -11,6 +12,9 @@ import ( "text/template" ) +//go:embed countries.json +var countriesJSON []byte + func main() { domain := os.Getenv("DOMAIN") if domain == "" { @@ -18,10 +22,9 @@ func main() { } var handler = handler{domain: domain, lawsByCC: map[string]map[string]law{}} - text, _ := ioutil.ReadFile("countries.json") - err := json.Unmarshal(text, &handler.countries) + err := json.Unmarshal(countriesJSON, &handler.countries) if err != nil { - log.Fatal("search.json", err) + log.Fatal("countries.json ", err) } lawFiles, err := ioutil.ReadDir("laws") @@ -51,9 +54,12 @@ func main() { log.Fatal(http.ListenAndServe(":8000", nil)) } +//go:embed templates +var templates embed.FS + var tpl, _ = template.New("").Funcs(template.FuncMap{ "ToUpper": strings.ToUpper, -}).ParseGlob("templates/*") +}).ParseFS(templates, "templates/*") type handler struct { domain string -- cgit v1.2.3