diff --git a/GNUmakefile b/GNUmakefile index 483f13b..d4b71b2 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -36,7 +36,7 @@ push: tidy no-dirty check ## push changes to git remote git push git main .PHONY: deploy -deploy: build ## deploy changes to the production server +deploy: build confirm ## deploy changes to the production server rsync ./ods root@ods.adyxax.org:/usr/local/bin/ ssh root@ods.adyxax.org "systemctl restart ods" diff --git a/index.html b/index.html index c01d42d..f98fd30 100644 --- a/index.html +++ b/index.html @@ -14,13 +14,13 @@
- {{ if ne .Query "" }} + {{ if .HasQuery }} {{ end }}

- {{ if ne .Query "" }} + {{ if .HasQuery }} {{ .Query }} est un mot IN{{ else }}valid">{{ end }}VALIDE au scrabble. {{ end }}

diff --git a/main.go b/main.go index 7cd4630..ab42630 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,20 @@ package main import ( - "context" "embed" - "fmt" - "html/template" - "log/slog" - "net" - "net/http" - "os" - "os/signal" - "strings" - "syscall" - "time" - "unicode" - "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 @@ -33,14 +30,16 @@ var static embed.FS var indexTemplate = template.Must(template.New("index").ParseFS(templatesFS, "index.html")) type IndexData struct { - Invalid bool - Query string + HasQuery bool + Query string + Invalid bool } func getIndex() http.Handler { data := IndexData{ - Query: "", - Invalid: true, + HasQuery: false, + Query: "", + Invalid: true, } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-store, no-cache") @@ -53,8 +52,9 @@ func postIndex() http.Handler { words := strings.Split(ods, "\n") return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data := IndexData{ - Query: r.FormValue("query"), - Invalid: true, + HasQuery: true, + Query: r.FormValue("query"), + Invalid: true, } query, _, _ := transform.String(normalizer, strings.TrimSpace(strings.ToUpper(data.Query))) for _, w := range words { @@ -68,72 +68,12 @@ func postIndex() http.Handler { }) } -func run( - ctx context.Context, - getenv func(string) string, -) error { - ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM) - defer cancel() - - mux := http.NewServeMux() - mux.Handle("GET /static/", http.FileServer(http.FS(static))) - mux.Handle("GET /", getIndex()) - mux.Handle("POST /", postIndex()) - - host := getenv("ODS_HOST") - if host == "" { - host = "127.0.0.1" - } - port := getenv("ODS_PORT") - if port == "" { - port = "8080" - } - - httpServer := &http.Server{ - Addr: net.JoinHostPort(host, port), - Handler: mux, - } - errChan := make(chan error, 1) - go func() { - slog.Info("backend http server listening", "address", httpServer.Addr) - if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { - errChan <- fmt.Errorf("error listening and serving backend http server on %s: %w", httpServer.Addr, err) - } - }() - - select { - case err := <-errChan: - return err - case <-ctx.Done(): - slog.Info("backend http server shutting down") - shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second) - defer shutdownCancel() - if err := httpServer.Shutdown(shutdownCtx); err != nil { - return fmt.Errorf("error shutting down backend http server: %w", err) - } - } - - return nil -} - func main() { - ctx := context.Background() - - var opts *slog.HandlerOptions - if os.Getenv("ODS_DEBUG") != "" { - opts = &slog.HandlerOptions{ - AddSource: true, - Level: slog.LevelDebug, - } - } - logger := slog.New(slog.NewJSONHandler(os.Stdout, opts)) - slog.SetDefault(logger) - - if err := run( - ctx, - os.Getenv, - ); err != nil { - fmt.Fprintf(os.Stderr, "%+v\n", err) - os.Exit(1) + 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) } }