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
|
||||
}
|
||||
|
||||
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() {
|
||||
err := LoadDict()
|
||||
if err != nil {
|
||||
|
@ -110,6 +124,29 @@ func main() {
|
|||
}
|
||||
fmt.Println("JMdict loaded!")
|
||||
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(
|
||||
"search.html",
|
||||
func(w http.ResponseWriter, r *http.Request) bool {
|
||||
|
@ -127,16 +164,7 @@ func main() {
|
|||
func(w http.ResponseWriter, r *http.Request) any {
|
||||
r.ParseMultipartForm(0)
|
||||
query := r.FormValue("q")
|
||||
exactResults, otherResults := Search(query)
|
||||
return struct {
|
||||
ExactResults []Entry
|
||||
OtherResults []Entry
|
||||
Count int
|
||||
}{
|
||||
ExactResults: exactResults,
|
||||
OtherResults: otherResults,
|
||||
Count: len(exactResults) + len(otherResults),
|
||||
}
|
||||
return initSearchTemplateData(Search(query))
|
||||
},
|
||||
[]string{http.MethodGet, http.MethodPost},
|
||||
))
|
||||
|
|
|
@ -19,11 +19,14 @@
|
|||
</a>
|
||||
<form
|
||||
hx-post="/api/search"
|
||||
hx-on::before-request="this.setAttribute('hx-replace-url', `/search/${this.querySelector('input').value}`)"
|
||||
hx-target="#results"
|
||||
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>
|
||||
<div id="results"></div>
|
||||
<div id="results">
|
||||
{{ with .Results }}{{ template "search" . }}{{ end }}
|
||||
</div>
|
||||
<br>
|
||||
</main>
|
||||
</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>
|
||||
{{- range .ExactResults -}}
|
||||
{{- template "word" . -}}
|
||||
|
@ -5,4 +6,6 @@
|
|||
<hr>
|
||||
{{ range .OtherResults -}}
|
||||
{{ template "word" . }}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- template "search" . -}}
|
Reference in a new issue