-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search): add search suggestions and selection of engines
- Loading branch information
1 parent
a4d72d8
commit 07c2b26
Showing
11 changed files
with
340 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/scottmckendry/mnemstart/views" | ||
) | ||
|
||
func (h *Handler) HandleSearchSuggest(w http.ResponseWriter, r *http.Request) { | ||
engine := r.FormValue("search_engine") | ||
query := r.FormValue("q") | ||
query = url.QueryEscape(query) | ||
|
||
body, err := getGoogleSuggestions(query) | ||
if err != nil { | ||
http.Error( | ||
w, | ||
fmt.Sprintf("Error getting suggestions: %v", err), | ||
http.StatusInternalServerError, | ||
) | ||
return | ||
} | ||
|
||
var suggestions []any | ||
err = json.Unmarshal(body, &suggestions) | ||
if err != nil { | ||
http.Error( | ||
w, | ||
fmt.Sprintf("Error unmarshalling suggestions: %v", err), | ||
http.StatusInternalServerError, | ||
) | ||
return | ||
} | ||
|
||
parsedSuggestions := parseSuggestions(suggestions[1].([]any)) | ||
views.Suggestions(parsedSuggestions, engine).Render(r.Context(), w) | ||
} | ||
|
||
func getGoogleSuggestions(query string) ([]byte, error) { | ||
resp, err := http.Get( | ||
"https://suggestqueries.google.com/complete/search?client=firefox&q=" + query, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return body, nil | ||
} | ||
|
||
func parseSuggestions(suggestions []any) []string { | ||
var parsedSuggestions []string | ||
for _, suggestion := range suggestions { | ||
parsedSuggestions = append(parsedSuggestions, suggestion.(string)) | ||
} | ||
|
||
return parsedSuggestions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package views | ||
|
||
import ( | ||
"github.com/scottmckendry/mnemstart/data" | ||
) | ||
|
||
type SearchEngine struct { | ||
URL string | ||
QueryParam string | ||
} | ||
|
||
var searchEngines = map[string]SearchEngine{ | ||
"Google": { | ||
URL: "https://www.google.com/search", | ||
QueryParam: "q", | ||
}, | ||
"DuckDuckGo": { | ||
URL: "https://duckduckgo.com/", | ||
QueryParam: "q", | ||
}, | ||
"Bing": { | ||
URL: "https://www.bing.com/search", | ||
QueryParam: "q", | ||
}, | ||
"Brave Search": { | ||
URL: "https://search.brave.com/search", | ||
QueryParam: "q", | ||
}, | ||
} | ||
|
||
templ Search(settings *data.UserSettings) { | ||
<form action={ templ.SafeURL(searchEngines[settings.SearchEngine].URL) } method="get"> | ||
<input | ||
type="text" | ||
id="search" | ||
name={ searchEngines[settings.SearchEngine].QueryParam } | ||
placeholder="Search..." | ||
hx-post="/search/suggest" | ||
hx-target="#suggestions" | ||
hx-trigger="input changed delay:500ms" | ||
autocomplete="off" | ||
/> | ||
// hidden form value that indicates the search engine | ||
<input type="hidden" name="search_engine" value={ settings.SearchEngine }/> | ||
</form> | ||
<div id="suggestions"></div> | ||
} | ||
|
||
templ Suggestions(suggestions []string, engine string) { | ||
for _, suggestion := range suggestions { | ||
<a href={ templ.SafeURL(searchEngines[engine].URL + "?" + searchEngines[engine].QueryParam + "=" + suggestion) }> | ||
<div class="suggestion"> | ||
{ suggestion } | ||
</div> | ||
</a> | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.