generated from ElnuDev/go-project
Implement route handling
This commit is contained in:
parent
fb045b9276
commit
36710478f2
3 changed files with 47 additions and 13 deletions
48
dict/main.go
48
dict/main.go
|
@ -102,6 +102,20 @@ func Search(query string) (exactResults []Entry, otherResults []Entry) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type searchTemplateData struct {
|
||||||
|
ExactResults []Entry
|
||||||
|
OtherResults []Entry
|
||||||
|
Count int
|
||||||
|
}
|
||||||
|
|
||||||
|
func initSearchTemplateData(exactResults []Entry, otherResults []Entry) searchTemplateData {
|
||||||
|
return searchTemplateData{
|
||||||
|
ExactResults: exactResults,
|
||||||
|
OtherResults: otherResults,
|
||||||
|
Count: len(exactResults) + len(otherResults),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := LoadDict()
|
err := LoadDict()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -110,6 +124,29 @@ func main() {
|
||||||
}
|
}
|
||||||
fmt.Println("JMdict loaded!")
|
fmt.Println("JMdict loaded!")
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/", httputils.GenerateHandler(
|
||||||
|
"index.html",
|
||||||
|
func(w http.ResponseWriter, r *http.Request) bool { return true },
|
||||||
|
func(w http.ResponseWriter, r *http.Request) any { return nil },
|
||||||
|
[]string{http.MethodGet},
|
||||||
|
))
|
||||||
|
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)),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[]string{http.MethodGet},
|
||||||
|
))
|
||||||
r.HandleFunc("/api/search", httputils.GenerateHandler(
|
r.HandleFunc("/api/search", httputils.GenerateHandler(
|
||||||
"search.html",
|
"search.html",
|
||||||
func(w http.ResponseWriter, r *http.Request) bool {
|
func(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
@ -127,16 +164,7 @@ func main() {
|
||||||
func(w http.ResponseWriter, r *http.Request) any {
|
func(w http.ResponseWriter, r *http.Request) any {
|
||||||
r.ParseMultipartForm(0)
|
r.ParseMultipartForm(0)
|
||||||
query := r.FormValue("q")
|
query := r.FormValue("q")
|
||||||
exactResults, otherResults := Search(query)
|
return initSearchTemplateData(Search(query))
|
||||||
return struct {
|
|
||||||
ExactResults []Entry
|
|
||||||
OtherResults []Entry
|
|
||||||
Count int
|
|
||||||
}{
|
|
||||||
ExactResults: exactResults,
|
|
||||||
OtherResults: otherResults,
|
|
||||||
Count: len(exactResults) + len(otherResults),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
[]string{http.MethodGet, http.MethodPost},
|
[]string{http.MethodGet, http.MethodPost},
|
||||||
))
|
))
|
||||||
|
|
|
@ -19,11 +19,14 @@
|
||||||
</a>
|
</a>
|
||||||
<form
|
<form
|
||||||
hx-post="/api/search"
|
hx-post="/api/search"
|
||||||
|
hx-on::before-request="this.setAttribute('hx-replace-url', `/search/${this.querySelector('input').value}`)"
|
||||||
hx-target="#results"
|
hx-target="#results"
|
||||||
hx-swap="innerHTML">
|
hx-swap="innerHTML">
|
||||||
<input type="text" name="q" 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>
|
<div id="results">
|
||||||
|
{{ with .Results }}{{ template "search" . }}{{ end }}
|
||||||
|
</div>
|
||||||
<br>
|
<br>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
|
@ -1,3 +1,4 @@
|
||||||
|
{{- define "search" -}}
|
||||||
<p><i>{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}.</i></p>
|
<p><i>{{ if eq .Count 0 }}No results{{ else }}{{ .Count }} result{{ if ne .Count 1}}s{{ end }}{{ end }}.</i></p>
|
||||||
{{- range .ExactResults -}}
|
{{- range .ExactResults -}}
|
||||||
{{- template "word" . -}}
|
{{- template "word" . -}}
|
||||||
|
@ -5,4 +6,6 @@
|
||||||
<hr>
|
<hr>
|
||||||
{{ range .OtherResults -}}
|
{{ range .OtherResults -}}
|
||||||
{{ template "word" . }}
|
{{ template "word" . }}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
{{- end -}}
|
||||||
|
{{- template "search" . -}}
|
Reference in a new issue