wordsearch/standalone/server/server.go

81 lines
2.4 KiB
Go
Raw Permalink Normal View History

2023-10-07 17:12:47 +01:00
package server
import (
"fmt"
"html/template"
"log"
"net/http"
2023-10-07 18:05:05 +01:00
"sort"
2023-10-07 17:12:47 +01:00
2023-10-08 15:05:12 +01:00
"github.com/ray1729/wordsearch/anagram"
"github.com/ray1729/wordsearch/match"
2023-10-07 17:12:47 +01:00
)
func New(matchDB match.DB, anagramDB anagram.DB) http.Handler {
2023-10-07 17:12:47 +01:00
mux := http.NewServeMux()
2023-10-08 14:25:57 +01:00
mux.HandleFunc("/search", searchHandler(matchDB, anagramDB))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, home, nil)
})
2023-10-07 17:12:47 +01:00
return withRequestLogger(mux)
}
func withRequestLogger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.Method, r.URL.Path, r.URL.Query())
h.ServeHTTP(w, r)
})
}
2023-10-08 14:25:57 +01:00
func searchHandler(matchDB match.DB, anagramDB anagram.DB) func(w http.ResponseWriter, r *http.Request) {
2023-10-07 17:12:47 +01:00
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
log.Printf("error parsing form: %v", err)
http.Error(w, "error parsing form", http.StatusBadRequest)
return
}
2023-10-07 18:05:05 +01:00
switch r.Form.Get("mode") {
2023-10-07 17:12:47 +01:00
case "match":
2023-10-08 14:25:57 +01:00
params := matchResults(matchDB, r.Form.Get("pattern"))
2023-10-07 18:05:05 +01:00
renderTemplate(w, results, params)
2023-10-07 17:12:47 +01:00
case "anagrams":
2023-10-07 18:05:05 +01:00
params := anagramResults(anagramDB, r.Form.Get("pattern"))
renderTemplate(w, results, params)
2023-10-07 17:12:47 +01:00
default:
2023-10-08 14:25:57 +01:00
renderTemplate(w, results, ResultParams{})
2023-10-07 17:12:47 +01:00
}
}
}
2023-10-07 18:05:05 +01:00
func anagramResults(db anagram.DB, pattern string) ResultParams {
var params ResultParams
params.Results = db.FindAnagrams(pattern)
if len(params.Results) > 0 {
params.Preamble = fmt.Sprintf("Anagrams of %q:", pattern)
} else {
params.Preamble = fmt.Sprintf("Found no anagrams of %q", pattern)
}
sort.Slice(params.Results, func(i, j int) bool { return params.Results[i] < params.Results[j] })
return params
}
2023-10-08 14:25:57 +01:00
func matchResults(db match.DB, pattern string) ResultParams {
2023-10-07 18:05:05 +01:00
var params ResultParams
params.Results = db.FindMatches(pattern)
if len(params.Results) > 0 {
params.Preamble = fmt.Sprintf("Matches for %q:", pattern)
} else {
params.Preamble = fmt.Sprintf("Found no matches for %q", pattern)
}
sort.Slice(params.Results, func(i, j int) bool { return params.Results[i] < params.Results[j] })
return params
2023-10-07 17:12:47 +01:00
}
func renderTemplate(w http.ResponseWriter, t *template.Template, params any) {
err := t.Execute(w, params)
if err != nil {
log.Printf("Error rendering template %s: %v", t.Name(), err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}