summaryrefslogtreecommitdiff
path: root/lex-serve/main.go
blob: cf8e9ae8c2570bc2b12eec9201a4283e3de5f888 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main

import (
	"bytes"
	"embed"
	"encoding/json"
	"html/template"
	"log/slog"
	"net"
	"net/http"
	"net/url"
	"os"
	"path"
	"slices"
	"strings"
	"sync"

	"push-f.com/lex-surf/internal/lex"

	"github.com/BurntSushi/toml"
)

//go:embed countries.toml
var countriesTOML []byte

func main() {
	log := slog.New(slog.NewTextHandler(os.Stderr, nil))

	socketPath := os.Getenv("SOCKET_PATH")
	if socketPath == "" {
		log.Error("SOCKET_PATH must be set")
		os.Exit(1)
	}

	domain := os.Getenv("DOMAIN")
	if domain == "" {
		log.Error("DOMAIN environment variable must be set")
		os.Exit(1)
	}

	lawsDir := os.Getenv("LAWS_DIR")
	if lawsDir == "" {
		log.Error("LAWS_DIR must be set")
		os.Exit(1)
	}

	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))
		os.Exit(1)
	}
	if len(meta.Undecoded()) != 0 {
		log.Error("unknown keys in countries.toml", "keys", meta.Undecoded())
		os.Exit(1)
	}

	lawFiles, err := os.ReadDir(lawsDir)
	if err == nil {
		for _, file := range lawFiles {
			cc := strings.SplitN(file.Name(), ".", 2)[0]
			handler.loadLaws(cc)
		}
	} else if !os.IsNotExist(err) {
		log.Error("failed to read laws directory", Error(err), "path", lawsDir)
		os.Exit(1)
	}
	http.HandleFunc("/", handler.handle)
	listener, err := net.Listen("unix", socketPath)
	if err != nil {
		log.Error("failed to listen", Error(err))
		os.Exit(1)
	}
	if err := os.Chmod(socketPath, 0666); err != nil {
		log.Error("failed to set socket permissions", Error(err), "path", socketPath)
		return
	}
	err = http.Serve(listener, nil)
	log.Error("failed to serve", Error(err))
}

//go:embed templates
var templates embed.FS

var tpl, _ = template.New("").Option("missingkey=error").ParseFS(templates, "templates/*")

type handler struct {
	logger    *slog.Logger
	domain    string
	countries map[string]country
	lawsDir   string
	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
	}
	if r.Host == h.domain {
		if r.URL.Path != "/" {
			w.WriteHeader(http.StatusNotFound)
			w.Write([]byte("page not found"))
			return
		}
		var firstTier []link
		var secondTier []link
		var thirdTier []link
		for key, country := range h.countries {
			link := link{
				URL:  "//" + key + "." + h.domain,
				Code: strings.ToUpper(key),
				Name: country.Name,
			}
			if len(h.lawsByCC[key]) > 0 {
				firstTier = append(firstTier, link)
			} else if country.HasPlaceholder() {
				secondTier = append(secondTier, link)
			} else {
				thirdTier = append(thirdTier, link)
			}
		}
		var html bytes.Buffer
		err := tpl.ExecuteTemplate(&html, "index.html.tmpl", map[string]any{
			"FirstTier":  slices.SortedFunc(slices.Values(firstTier), compareLink),
			"SecondTier": slices.SortedFunc(slices.Values(secondTier), compareLink),
			"ThirdTier":  slices.SortedFunc(slices.Values(thirdTier), compareLink),
			"Domain":     r.Host,
		})
		if err != nil {
			h.logger.Error("failed to execute index template", Error(err))
			http.Error(w, "internal server error", http.StatusInternalServerError)
			return
		}
		w.Write(html.Bytes())
		return
	}

	cc, isSubdomain := strings.CutSuffix(r.Host, "."+h.domain)
	if !isSubdomain {
		h.logger.Error("request for unknown hosts", "host", r.Host)
		w.Write([]byte("unknown host"))
		return
	}
	key := strings.TrimLeft(r.URL.Path, "/")
	if len(key) > 0 {
		h.lawsMu.RLock()
		defer h.lawsMu.RUnlock()
		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)
			}
		}
		h.lawsMu.RLock()
		defer h.lawsMu.RUnlock()
		_, hasJSONLaws := h.lawsByCC[cc]
		var html bytes.Buffer
		err := tpl.ExecuteTemplate(&html, "search.html.tmpl", map[string]any{
			"TLD":         cc,
			"Domain":      h.domain,
			"Country":     h.countries[cc],
			"HasJSONLaws": hasJSONLaws,
		})
		if err != nil {
			h.logger.Error("failed to execute search template", Error(err))
			http.Error(w, "internal server error", http.StatusInternalServerError)
			return
		}
		w.Write(html.Bytes())
	}
}

func (h *handler) loadLaws(country string) {
	filePath := path.Join(h.lawsDir, country+".json")
	text, err := os.ReadFile(filePath)
	if err != nil {
		h.logger.Error("failed to read file", Error(err), "path", filePath)
		return
	}
	var laws []lex.Law
	err = json.Unmarshal([]byte(text), &laws)
	if err != nil {
		h.logger.Error("failed to parse file", Error(err), "path", filePath)
		return
	}
	h.lawsMu.Lock()
	defer h.lawsMu.Unlock()
	h.lawsByCC[country] = map[string]lex.Law{}
	for _, law := range laws {
		if law.Redir != "" {
			h.lawsByCC[country][law.Redir] = law
		}
	}
}

type country struct {
	Name      string
	SearchURL string `toml:"search-url"`
}

func (c country) HasPlaceholder() bool {
	return strings.Contains(c.SearchURL, "%s")
}

type link struct {
	URL  string
	Code string
	Name string
}

func compareLink(a, b link) int {
	if a.Code > b.Code {
		return 1
	} else if a.Code < b.Code {
		return -1
	}
	return 0
}

func Error(value error) slog.Attr { return slog.String("error", value.Error()) }