summaryrefslogtreecommitdiff
path: root/main.go
blob: ab42630aeb55cfe61818ad7455b969c4504b8173 (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
package main

import (
	"embed"
	"golang.org/x/text/runes"
	"golang.org/x/text/transform"
	"golang.org/x/text/unicode/norm"
	"html/template"
	"log/slog"
	"net/http"
	"strings"
	"unicode"
)

// Variables to customise the search
const (
	listenStr = "0.0.0.0:8090"
)

//go:embed ods.txt
var ods string

//go:embed index.html
var templatesFS embed.FS

//go:embed static/*
var static embed.FS

// html templates
var indexTemplate = template.Must(template.New("index").ParseFS(templatesFS, "index.html"))

type IndexData struct {
	HasQuery bool
	Query    string
	Invalid  bool
}

func getIndex() http.Handler {
	data := IndexData{
		HasQuery: false,
		Query:    "",
		Invalid:  true,
	}
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Cache-Control", "no-store, no-cache")
		indexTemplate.ExecuteTemplate(w, "index.html", data)
	})
}

func postIndex() http.Handler {
	normalizer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
	words := strings.Split(ods, "\n")
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		data := IndexData{
			HasQuery: true,
			Query:    r.FormValue("query"),
			Invalid:  true,
		}
		query, _, _ := transform.String(normalizer, strings.TrimSpace(strings.ToUpper(data.Query)))
		for _, w := range words {
			if w == query {
				data.Invalid = false
				break
			}
		}
		slog.Info("post", "word", query, "invalid", data.Invalid)
		indexTemplate.ExecuteTemplate(w, "index.html", data)
	})
}

func main() {
	http.Handle("GET /static/", http.FileServer(http.FS(static)))
	http.Handle("GET /", getIndex())
	http.Handle("POST /", postIndex())
	slog.Info("listening", "addr", listenStr)
	if err := http.ListenAndServe(listenStr, nil); err != nil && err != http.ErrServerClosed {
		slog.Error("error listening and serving", "error", err)
	}
}