|
|
|
@ -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},
|
|
|
|
|
))
|
|
|
|
|