@ -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,25 +106,25 @@ func Search(query string) (exactResults []Entry, otherResults []Entry, truncated
break
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
ExactResults [ ] Entry
OtherResults [ ] Entry
OtherResults [ ] Entry
Truncated bool
Truncated bool
Count int
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 ( ) {
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
}
http . Redirect ( w , r , redirect , http . StatusMovedPermanently )
}
}
r . HandleFunc ( "/search" , redirectToHome )
r . HandleFunc ( "/search" , r awSearchHandler )
r . HandleFunc ( "/search/" , redirectToHome )
r . HandleFunc ( "/search/" , r awSearchHandler )
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 } ,
) )
) )