2024-03-25 13:58:48 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2024-03-27 10:08:09 +01:00
|
|
|
"golang.org/x/text/runes"
|
|
|
|
"golang.org/x/text/transform"
|
|
|
|
"golang.org/x/text/unicode/norm"
|
2024-03-25 13:58:48 +01:00
|
|
|
"html/template"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2024-03-27 10:08:09 +01:00
|
|
|
"unicode"
|
2024-03-25 13:58:48 +01:00
|
|
|
)
|
|
|
|
|
2024-04-02 02:06:10 +02:00
|
|
|
// Variables to customise the search
|
2024-03-25 13:58:48 +01:00
|
|
|
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
|
2024-03-27 10:08:09 +01:00
|
|
|
Invalid bool
|
2024-03-25 13:58:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getIndex() http.Handler {
|
|
|
|
data := IndexData{
|
|
|
|
HasQuery: false,
|
|
|
|
Query: "",
|
2024-03-27 10:08:09 +01:00
|
|
|
Invalid: true,
|
2024-03-25 13:58:48 +01:00
|
|
|
}
|
|
|
|
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 {
|
2024-03-27 10:08:09 +01:00
|
|
|
normalizer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
|
|
|
|
words := strings.Split(ods, "\n")
|
2024-03-25 13:58:48 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data := IndexData{
|
|
|
|
HasQuery: true,
|
|
|
|
Query: r.FormValue("query"),
|
2024-03-27 10:08:09 +01:00
|
|
|
Invalid: true,
|
2024-03-25 13:58:48 +01:00
|
|
|
}
|
2024-03-27 10:08:09 +01:00
|
|
|
query, _, _ := transform.String(normalizer, strings.TrimSpace(strings.ToUpper(data.Query)))
|
2024-03-25 13:58:48 +01:00
|
|
|
for _, w := range words {
|
|
|
|
if w == query {
|
|
|
|
data.Invalid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-04-02 02:06:10 +02:00
|
|
|
slog.Info("post", "word", query, "invalid", data.Invalid)
|
2024-03-25 13:58:48 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|