generated from ElnuDev/go-project
Better API handling
This commit is contained in:
parent
61c1ce5502
commit
f558d7f0c1
4 changed files with 62 additions and 52 deletions
88
dict/main.go
88
dict/main.go
|
@ -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)
|
query = strings.TrimSpace(query)
|
||||||
exactResults = make([]Entry, 0)
|
exactResults := make([]Entry, 0)
|
||||||
otherResults = make([]Entry, 0)
|
otherResults := make([]Entry, 0)
|
||||||
|
truncated := false
|
||||||
count := 0
|
count := 0
|
||||||
for _, jmdictEntry := range dict.Entries {
|
for _, jmdictEntry := range dict.Entries {
|
||||||
exactMatch := false
|
exactMatch := false
|
||||||
|
@ -105,18 +106,8 @@ func Search(query string) (exactResults []Entry, otherResults []Entry, truncated
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return queryResult{
|
||||||
}
|
Query: query,
|
||||||
|
|
||||||
type searchTemplateData struct {
|
|
||||||
ExactResults []Entry
|
|
||||||
OtherResults []Entry
|
|
||||||
Truncated bool
|
|
||||||
Count int
|
|
||||||
}
|
|
||||||
|
|
||||||
func initSearchTemplateData(exactResults []Entry, otherResults []Entry, truncated bool) searchTemplateData {
|
|
||||||
return searchTemplateData{
|
|
||||||
ExactResults: exactResults,
|
ExactResults: exactResults,
|
||||||
OtherResults: otherResults,
|
OtherResults: otherResults,
|
||||||
Truncated: truncated,
|
Truncated: truncated,
|
||||||
|
@ -124,6 +115,16 @@ func initSearchTemplateData(exactResults []Entry, otherResults []Entry, truncate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type queryResult struct {
|
||||||
|
// Fields must be capitalized
|
||||||
|
// to be accessible in templates
|
||||||
|
Query string
|
||||||
|
ExactResults []Entry
|
||||||
|
OtherResults []Entry
|
||||||
|
Truncated bool
|
||||||
|
Count int
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := LoadDict()
|
err := LoadDict()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -138,46 +139,49 @@ func main() {
|
||||||
func(w http.ResponseWriter, r *http.Request) any { return nil },
|
func(w http.ResponseWriter, r *http.Request) any { return nil },
|
||||||
[]string{http.MethodGet},
|
[]string{http.MethodGet},
|
||||||
))
|
))
|
||||||
redirectToHome := func(w http.ResponseWriter, r *http.Request) {
|
rawSearchHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, "/", http.StatusPermanentRedirect)
|
r.ParseMultipartForm(0)
|
||||||
|
q := r.FormValue("q")
|
||||||
|
var redirect string
|
||||||
|
if q == "" {
|
||||||
|
redirect = "/"
|
||||||
|
} else {
|
||||||
|
redirect = "/search/" + q
|
||||||
}
|
}
|
||||||
r.HandleFunc("/search", redirectToHome)
|
http.Redirect(w, r, redirect, http.StatusMovedPermanently)
|
||||||
r.HandleFunc("/search/", redirectToHome)
|
}
|
||||||
|
r.HandleFunc("/search", rawSearchHandler)
|
||||||
|
r.HandleFunc("/search/", rawSearchHandler)
|
||||||
r.HandleFunc("/search/{query}", httputils.GenerateHandler(
|
r.HandleFunc("/search/{query}", httputils.GenerateHandler(
|
||||||
"index.html",
|
// template file
|
||||||
func(w http.ResponseWriter, r *http.Request) bool {
|
func(w http.ResponseWriter, r *http.Request) string {
|
||||||
return true
|
if r.Header.Get("HX-Request") == "" {
|
||||||
},
|
return "index.html"
|
||||||
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)),
|
|
||||||
}
|
}
|
||||||
|
return "search.html"
|
||||||
},
|
},
|
||||||
[]string{http.MethodGet},
|
// handler whether or not to use template
|
||||||
))
|
|
||||||
r.HandleFunc("/api/search", httputils.GenerateHandler(
|
|
||||||
"search.html",
|
|
||||||
func(w http.ResponseWriter, r *http.Request) bool {
|
func(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
// If Accept: applicaiton/json we'll use the template
|
||||||
if r.Header.Get("Accept") != "application/json" {
|
if r.Header.Get("Accept") != "application/json" {
|
||||||
return true
|
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")
|
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))
|
fmt.Fprint(w, string(jsonBytes))
|
||||||
|
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
|
// template data
|
||||||
func(w http.ResponseWriter, r *http.Request) any {
|
func(w http.ResponseWriter, r *http.Request) any {
|
||||||
r.ParseMultipartForm(0)
|
// Only runs if handler returns true
|
||||||
query := r.FormValue("q")
|
query := mux.Vars(r)["query"]
|
||||||
return initSearchTemplateData(Search(query))
|
return Search(query)
|
||||||
},
|
},
|
||||||
[]string{http.MethodGet},
|
[]string{http.MethodGet},
|
||||||
))
|
))
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<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">
|
<link rel="stylesheet" href="https://unpkg.com/missing.css@1.0.9/dist/missing.min.css">
|
||||||
<style>
|
<style>
|
||||||
li {
|
li {
|
||||||
|
@ -18,14 +18,13 @@
|
||||||
<img src="https://jichan.org/logo.svg" style="height: 4em; display: block; margin: 1em auto 1em auto">
|
<img src="https://jichan.org/logo.svg" style="height: 4em; display: block; margin: 1em auto 1em auto">
|
||||||
</a>
|
</a>
|
||||||
<form
|
<form
|
||||||
hx-get="/api/search"
|
hx-get="/search"
|
||||||
hx-on::before-request="this.setAttribute('hx-replace-url', `/search/${this.querySelector('input').value}`)"
|
hx-replace-url="true"
|
||||||
hx-target="#results"
|
hx-target="#results">
|
||||||
hx-swap="innerHTML">
|
|
||||||
<input type="text" name="q"{{ with .Query }} value="{{ . }}"{{ end }} placeholder="辞書をサーチする" class="width:100%" autocomplete="false">
|
<input type="text" name="q"{{ with .Query }} value="{{ . }}"{{ end }} placeholder="辞書をサーチする" class="width:100%" autocomplete="false">
|
||||||
</form>
|
</form>
|
||||||
<div id="results">
|
<div id="results">
|
||||||
{{ with .Results }}{{ template "search" . }}{{ end }}
|
{{ if .Count }}{{ template "search" . }}{{ end }}
|
||||||
</div>
|
</div>
|
||||||
<br>
|
<br>
|
||||||
</main>
|
</main>
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
{{- define "search" -}}
|
{{- 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>
|
<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" . -}}
|
{{- template "word" . -}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
<hr>
|
{{ if and (ne (len .ExactResults) 0) (ne (len .OtherResults) 0) }}<hr>{{ end }}
|
||||||
{{ range .OtherResults -}}
|
{{ range .OtherResults -}}
|
||||||
{{ template "word" . }}
|
{{ template "word" . }}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|
|
@ -56,7 +56,7 @@ func reloadTemplatesIfModified() {
|
||||||
const reloadTemplates = true
|
const reloadTemplates = true
|
||||||
|
|
||||||
func GenerateHandler(
|
func GenerateHandler(
|
||||||
file string,
|
file interface{}, // either string or func() string
|
||||||
handler func(http.ResponseWriter, *http.Request) bool,
|
handler func(http.ResponseWriter, *http.Request) bool,
|
||||||
data func(http.ResponseWriter, *http.Request) any,
|
data func(http.ResponseWriter, *http.Request) any,
|
||||||
methods []string,
|
methods []string,
|
||||||
|
@ -76,8 +76,15 @@ func GenerateHandler(
|
||||||
ok:
|
ok:
|
||||||
renderTemplate := handler(w, r)
|
renderTemplate := handler(w, r)
|
||||||
if renderTemplate && file != "" {
|
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")
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue