diff options
Diffstat (limited to 'lex-serve')
-rw-r--r-- | lex-serve/main.go | 18 | ||||
-rw-r--r-- | lex-serve/main_test.go | 7 |
2 files changed, 11 insertions, 14 deletions
diff --git a/lex-serve/main.go b/lex-serve/main.go index 98cc403..2996aef 100644 --- a/lex-serve/main.go +++ b/lex-serve/main.go @@ -14,6 +14,8 @@ import ( "strings" "sync" + "push-f.com/lex-surf/internal/lex" + "github.com/BurntSushi/toml" ) @@ -41,7 +43,7 @@ func main() { os.Exit(1) } - var handler = handler{logger: log, domain: domain, lawsDir: lawsDir, lawsByCC: map[string]map[string]law{}} + var handler = handler{logger: log, domain: domain, lawsDir: lawsDir, lawsByCC: map[string]map[string]lex.Law{}} meta, err := toml.NewDecoder(bytes.NewReader(countriesTOML)).Decode(&handler.countries) if err != nil { log.Error("failed to parse countries.toml", Error(err)) @@ -88,12 +90,13 @@ type handler struct { domain string countries map[string]country lawsDir string - lawsByCC map[string]map[string]law + lawsByCC map[string]map[string]lex.Law lawsMu sync.RWMutex } func (h *handler) handle(w http.ResponseWriter, r *http.Request) { if r.Host == "internal.invalid" { + // lex-fetch uses this special Host to notify us that the laws for a country should be (re-)loaded. h.loadLaws(r.URL.Query().Get("country")) return } @@ -176,7 +179,7 @@ func (h *handler) loadLaws(country string) { h.logger.Error("failed to read file", Error(err), "path", filePath) return } - var laws []law + var laws []lex.Law err = json.Unmarshal([]byte(text), &laws) if err != nil { h.logger.Error("failed to parse file", Error(err), "path", filePath) @@ -184,7 +187,7 @@ func (h *handler) loadLaws(country string) { } h.lawsMu.Lock() defer h.lawsMu.Unlock() - h.lawsByCC[country] = map[string]law{} + h.lawsByCC[country] = map[string]lex.Law{} for _, law := range laws { if law.Redir != "" { h.lawsByCC[country][law.Redir] = law @@ -192,13 +195,6 @@ func (h *handler) loadLaws(country string) { } } -type law struct { - URL string - Title string - Abbr string - Redir string -} - type country struct { Name string SearchURL string `toml:"search-url"` diff --git a/lex-serve/main_test.go b/lex-serve/main_test.go index 00cc3a3..4783fb3 100644 --- a/lex-serve/main_test.go +++ b/lex-serve/main_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/peter-evans/patience" + "push-f.com/lex-surf/internal/lex" ) func newHandler() handler { @@ -20,7 +21,7 @@ func newHandler() handler { SearchURL: "https://lex.gov.zu/?search=%s", }, }, - lawsByCC: map[string]map[string]law{}, + lawsByCC: map[string]map[string]lex.Law{}, } } @@ -116,8 +117,8 @@ func TestSearchRedirect(t *testing.T) { func TestLawRedirect(t *testing.T) { h := newHandler() - h.lawsByCC["zu"] = map[string]law{} - h.lawsByCC["zu"]["zepl"] = law{ + h.lawsByCC["zu"] = map[string]lex.Law{} + h.lawsByCC["zu"]["zepl"] = lex.Law{ URL: "https://lex.gov.zu/zeppelin-code", } |