diff options
Diffstat (limited to 'lex-serve/main.go')
-rw-r--r-- | lex-serve/main.go | 18 |
1 files changed, 7 insertions, 11 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"` |