Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTMX at search #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func main() {
r.Use(securityHeaders)

r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { render("home", w, nil) })
r.HandleFunc("/search", searchHandler).Methods("GET")
r.HandleFunc("/search", fullSearchHandler).Methods("GET")
r.HandleFunc("/form_search", formSearchHandler).Methods("POST")
r.HandleFunc("/{id}-lyrics", lyricsHandler)
r.HandleFunc("/images/{filename}.{ext}", proxyHandler)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
Expand Down
17 changes: 14 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ type renderVars struct {
Sections sections
}

func searchHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query().Get("q")
func searchHandler(query string, w http.ResponseWriter, r *http.Request) renderVars {
url := fmt.Sprintf(`https://genius.com/api/search/multi?q=%s`, url.QueryEscape(query))

res, err := sendRequest(url)
Expand All @@ -55,7 +54,19 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
d := json.NewDecoder(res.Body)
d.Decode(&data)

vars := renderVars{query, data.Response.Sections}
return renderVars{query, data.Response.Sections}

// render("search", w, vars)
}

func fullSearchHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query().Get("q")
vars := searchHandler(query, w, r)
render("search", w, vars)
}

func formSearchHandler(w http.ResponseWriter, r *http.Request) {
query := r.FormValue("q")
vars := searchHandler(query, w, r)
render("search_partial", w, vars)
}
7 changes: 4 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func write(w http.ResponseWriter, status int, data []byte) {

func securityHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
csp := "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self'; object-src 'none'"
csp := "style-src 'self'; img-src 'self'; object-src 'none'"
// This is CSP interrupts the HTMX and other JS requests
// SO I am disabling it for now
w.Header().Add("content-security-policy", csp)
w.Header().Add("referrer-policy", "no-referrer")
w.Header().Add("x-content-type-options", "nosniff")
Expand All @@ -67,7 +69,7 @@ func getTemplates(templates ...string) []string {
func render(n string, w http.ResponseWriter, data interface{}) {
w.Header().Set("content-type", "text/html")
t := template.New(n + ".tmpl").Funcs(template.FuncMap{"extractURL": extractURL})
t, err := t.ParseFiles(getTemplates(n, "navbar", "footer")...)
t, err := t.ParseFiles(getTemplates(n, "navbar", "footer", "head")...)
if err != nil {
logger.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -105,7 +107,6 @@ func sendRequest(u string) (*http.Response, error) {
return nil, err
}


req := &http.Request{
Method: http.MethodGet,
URL: url,
Expand Down
9 changes: 9 additions & 0 deletions views/head.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{define "head"}}
<head>
<title>dumb</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/static/style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://unpkg.com/htmx.org@1.9.7"></script>
</head>
{{end}}
12 changes: 5 additions & 7 deletions views/home.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<!DOCTYPE html>
<html>
<head>
<title>dumb</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/static/style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
{{template "head"}}
<body>
<main id="app">
{{template "navbar"}}
Expand All @@ -15,8 +10,11 @@
<p>An alternative frontend for genius.com</p>
</div>
<form method="GET" action="/search">
<input type="text" name="q" id="search-input" placeholder="Search..." />
<input hx-post="/form_search" hx-target="#results" hx-trigger="keyup changed delay:0.1s" type="text" name="q" id="search-input" placeholder="Search..." />
</form>
<div id="results">

</div>

</div>
{{template "footer"}}
Expand Down
16 changes: 16 additions & 0 deletions views/search_partial.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id="search-results">
{{range .Sections}}
{{if eq .Type "song"}}
<h1>Songs</h1>
{{range .Hits}}
<a id="search-item" href="{{.Result.Path}}">
<img src="{{extractURL .Result.Thumbnail}}"/>
<div>
<span>{{.Result.ArtistNames}}</span>
<h2>{{.Result.Title}}</h2>
</div>
</a>
{{end}}
{{end}}
{{end}}
</div>