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
|
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"text/template"
)
func main() {
var handler = handler{lawsByCC: map[string]map[string]law{}}
text, _ := ioutil.ReadFile("countries.json")
err := json.Unmarshal(text, &handler.countries)
if err != nil {
log.Fatal("search.json", err)
}
lawFiles, err := ioutil.ReadDir("laws")
if err != nil {
log.Fatal(err)
}
for _, file := range lawFiles {
text, err := ioutil.ReadFile("laws/" + file.Name())
if err != nil {
log.Fatal(file.Name(), err)
}
var laws []law
err = json.Unmarshal([]byte(text), &laws)
if err != nil {
log.Fatal(file.Name(), err)
}
cc := strings.SplitN(file.Name(), ".", 2)[0]
handler.lawsByCC[cc] = map[string]law{}
for _, law := range laws {
if law.Redir != "" {
handler.lawsByCC[cc][law.Redir] = law
}
}
}
http.HandleFunc("/", handler.handle)
println("listening on 8000")
log.Fatal(http.ListenAndServe(":8000", nil))
}
var tpl, _ = template.New("").Funcs(template.FuncMap{
"ToUpper": strings.ToUpper,
}).ParseGlob("views/*")
type handler struct {
countries map[string]country
lawsByCC map[string]map[string]law
}
func (h *handler) handle(w http.ResponseWriter, r *http.Request) {
if r.Host == "lex.surf" || r.Host == "lex.localhost" {
if r.URL.Path != "/" {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("page not found"))
return
}
err := tpl.ExecuteTemplate(w, "index.html", map[string]any{
"Countries": h.countries,
"Domain": r.Host,
})
if err != nil {
log.Fatal(err)
}
return
}
parts := strings.SplitN(r.Host, ".", 2)
if len(parts) != 2 {
w.Write([]byte("unknown host"))
return
}
cc := parts[0]
host := parts[1]
key := strings.TrimLeft(r.URL.Path, "/")
if len(key) > 0 {
val, ok := h.lawsByCC[cc][key]
if !ok {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("unknown law"))
return
}
http.Redirect(w, r, val.Url, 302)
} else {
query := r.URL.Query().Get("q")
if query != "" {
country, ok := h.countries[cc]
if !ok {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("search not implemented for this country"))
return
}
if country.HasPlaceholder() {
http.Redirect(w, r, strings.Replace(country.SearchURL, "%s", url.QueryEscape(query), 1), 302)
return
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
_, hasJSONLaws := h.lawsByCC[cc]
err := tpl.ExecuteTemplate(w, "search.html", map[string]any{
"TLD": cc,
"Domain": host,
"Country": h.countries[cc],
"HasJSONLaws": hasJSONLaws,
})
if err != nil {
log.Fatal(err)
}
}
}
type law struct {
Url string
Title string
Abbr string
Redir string
}
type country struct {
Name string
SearchURL string `json:"search_url"`
}
func (c country) HasPlaceholder() bool {
return strings.Contains(c.SearchURL, "%s")
}
|