Better API handling

main
Elnu 2 years ago
parent 61c1ce5502
commit f558d7f0c1

@ -69,10 +69,11 @@ func ParseEntry(entry jmdict.JmdictEntry) Entry {
}
}
func Search(query string) (exactResults []Entry, otherResults []Entry, truncated bool) {
func Search(query string) queryResult {
query = strings.TrimSpace(query)
exactResults = make([]Entry, 0)
otherResults = make([]Entry, 0)
exactResults := make([]Entry, 0)
otherResults := make([]Entry, 0)
truncated := false
count := 0
for _, jmdictEntry := range dict.Entries {
exactMatch := false
@ -105,25 +106,25 @@ func Search(query string) (exactResults []Entry, otherResults []Entry, truncated
break
}
}
return
return queryResult{
Query: query,
ExactResults: exactResults,
OtherResults: otherResults,
Truncated: truncated,
Count: len(exactResults) + len(otherResults),
}
}
type searchTemplateData struct {
type queryResult struct {
// Fields must be capitalized
// to be accessible in templates
Query string
ExactResults []Entry
OtherResults []Entry
Truncated bool
Count int
}
func initSearchTemplateData(exactResults []Entry, otherResults []Entry, truncated bool) searchTemplateData {
return searchTemplateData{
ExactResults: exactResults,
OtherResults: otherResults,
Truncated: truncated,
Count: len(exactResults) + len(otherResults),
}
}
func main() {
err := LoadDict()
if err != nil {
@ -138,46 +139,49 @@ func main() {
func(w http.ResponseWriter, r *http.Request) any { return nil },
[]string{http.MethodGet},
))
redirectToHome := func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusPermanentRedirect)
rawSearchHandler := func(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(0)
q := r.FormValue("q")
var redirect string
if q == "" {
redirect = "/"
} else {
redirect = "/search/" + q
}
http.Redirect(w, r, redirect, http.StatusMovedPermanently)
}
r.HandleFunc("/search", redirectToHome)
r.HandleFunc("/search/", redirectToHome)
r.HandleFunc("/search", rawSearchHandler)
r.HandleFunc("/search/", rawSearchHandler)
r.HandleFunc("/search/{query}", httputils.GenerateHandler(
"index.html",
func(w http.ResponseWriter, r *http.Request) bool {
return true
},
func(w http.ResponseWriter, r *http.Request) any {
query := mux.Vars(r)["query"]
return struct {
Query string
Results searchTemplateData
}{
Query: query,
Results: initSearchTemplateData(Search(query)),
// template file
func(w http.ResponseWriter, r *http.Request) string {
if r.Header.Get("HX-Request") == "" {
return "index.html"
}
return "search.html"
},
[]string{http.MethodGet},
))
r.HandleFunc("/api/search", httputils.GenerateHandler(
"search.html",
// handler whether or not to use template
func(w http.ResponseWriter, r *http.Request) bool {
// If Accept: applicaiton/json we'll use the template
if r.Header.Get("Accept") != "application/json" {
return true
}
// Otherwise, let's send JSON
query := mux.Vars(r)["query"]
result := Search(query)
jsonBytes, _ := json.Marshal(append(result.ExactResults, result.OtherResults...))
w.Header().Set("Content-Type", "application/json; charset=utf-8")
r.ParseMultipartForm(0)
query := r.FormValue("q")
exactResults, otherResults, _ := Search(query)
jsonBytes, _ := json.Marshal(append(exactResults, otherResults...))
fmt.Fprint(w, string(jsonBytes))
return false
},
// template data
func(w http.ResponseWriter, r *http.Request) any {
r.ParseMultipartForm(0)
query := r.FormValue("q")
return initSearchTemplateData(Search(query))
// Only runs if handler returns true
query := mux.Vars(r)["query"]
return Search(query)
},
[]string{http.MethodGet},
))

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jidict</title>
<title>{{ with .Query }}{{ . }} search - {{ end }}jidict</title>
<link rel="stylesheet" href="https://unpkg.com/missing.css@1.0.9/dist/missing.min.css">
<style>
li {
@ -18,14 +18,13 @@
<img src="https://jichan.org/logo.svg" style="height: 4em; display: block; margin: 1em auto 1em auto">
</a>
<form
hx-get="/api/search"
hx-on::before-request="this.setAttribute('hx-replace-url', `/search/${this.querySelector('input').value}`)"
hx-target="#results"
hx-swap="innerHTML">
hx-get="/search"
hx-replace-url="true"
hx-target="#results">
<input type="text" name="q"{{ with .Query }} value="{{ . }}"{{ end }} placeholder="辞書をサーチする" class="width:100%" autocomplete="false">
</form>
<div id="results">
{{ with .Results }}{{ template "search" . }}{{ end }}
{{ if .Count }}{{ template "search" . }}{{ end }}
</div>
<br>
</main>

@ -1,9 +1,9 @@
{{- define "search" -}}
<p><i>{{ if .Truncated }}Truncated results, showing first {{ .Count }}{{ else }}{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}{{ end }}.</i></p>
{{- range .ExactResults -}}
{{ range .ExactResults -}}
{{- template "word" . -}}
{{- end }}
<hr>
{{ if and (ne (len .ExactResults) 0) (ne (len .OtherResults) 0) }}<hr>{{ end }}
{{ range .OtherResults -}}
{{ template "word" . }}
{{- end -}}

@ -56,7 +56,7 @@ func reloadTemplatesIfModified() {
const reloadTemplates = true
func GenerateHandler(
file string,
file interface{}, // either string or func() string
handler func(http.ResponseWriter, *http.Request) bool,
data func(http.ResponseWriter, *http.Request) any,
methods []string,
@ -76,8 +76,15 @@ func GenerateHandler(
ok:
renderTemplate := handler(w, r)
if renderTemplate && file != "" {
var file_path string
switch file.(type) {
case string:
file_path = file.(string)
case func(http.ResponseWriter, *http.Request) string:
file_path = file.(func(http.ResponseWriter, *http.Request) string)(w, r)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
templates.ExecuteTemplate(w, file, data(w, r))
templates.ExecuteTemplate(w, file_path, data(w, r))
}
}
}