bookmark search
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"site/database"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed source
|
||||
@@ -22,12 +24,18 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
database.InitDB("data.db")
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /{$}", servePage("home", "templates/pages/index.html", sourceRoot, false))
|
||||
mux.HandleFunc("GET /research", servePage("research", "templates/pages/research.html", sourceRoot, true))
|
||||
mux.HandleFunc("GET /bookkeeping", servePage("bookkeeping", "templates/pages/bookkeeping.html", sourceRoot, true))
|
||||
mux.HandleFunc("GET /personal", servePage("personal", "templates/pages/interests.html", sourceRoot, true))
|
||||
|
||||
mux.HandleFunc("GET /search", search(sourceRoot))
|
||||
mux.HandleFunc("GET /api/bookmarks/search", bookmarksearch())
|
||||
mux.HandleFunc("POST /api/bookmarks", bookmarkAdd())
|
||||
|
||||
staticRoot, err := fs.Sub(sourceFS, "source/static")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -70,3 +78,121 @@ func servePage(name string, templatefile string, sourcefile fs.FS, sub bool) htt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func search(sourcefile fs.FS) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := fs.ReadFile(sourcefile, "templates/pages/search.html")
|
||||
if err != nil {
|
||||
log.Println("read error:", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Write(data)
|
||||
}
|
||||
}
|
||||
|
||||
var resultsTmpl = template.Must(template.New("results").Parse(`
|
||||
{{if .}}
|
||||
{{range .}}
|
||||
<a class="result-card" href="{{.URL}}">
|
||||
<p class="result-card__title">{{.Title}}</p>
|
||||
<p class="result-card__meta">{{.CategoryLabel}}</p>
|
||||
</a>
|
||||
{{end}}
|
||||
{{else}}
|
||||
<p class="empty-state">No bookmarks match yet.</p>
|
||||
{{end}}
|
||||
`))
|
||||
|
||||
var addTmpl = template.Must(template.New("results").Parse(`
|
||||
<a class="result-card" href="{{.URL}}">
|
||||
<p class="result-card__title">{{.Title}}</p>
|
||||
<p class="result-card__meta">{{.CategoryLabel}}</p>
|
||||
</a>
|
||||
`))
|
||||
|
||||
// CategoryLabel gives the template a human-readable label without
|
||||
// needing a helper func registered on the template itself.
|
||||
type bookmarkView struct {
|
||||
database.Bookmark
|
||||
CategoryLabel string
|
||||
}
|
||||
|
||||
func bookmarksearch() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
q := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("q")))
|
||||
category := r.URL.Query().Get("category")
|
||||
|
||||
var matches []bookmarkView
|
||||
|
||||
bookmarks, err := database.RetrieveBookmarks()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
|
||||
for _, b := range bookmarks {
|
||||
if category != "" && category != "all" && b.Category != category {
|
||||
continue
|
||||
}
|
||||
if q != "" && !strings.Contains(strings.ToLower(b.Title), q) {
|
||||
continue
|
||||
}
|
||||
matches = append(matches, bookmarkView{
|
||||
Bookmark: b,
|
||||
CategoryLabel: strings.ReplaceAll(b.Category, "-", " "),
|
||||
})
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := resultsTmpl.Execute(w, matches); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func bookmarkAdd() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "bad form data", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
title := strings.TrimSpace(r.FormValue("title"))
|
||||
url := strings.TrimSpace(r.FormValue("url"))
|
||||
category := r.FormValue("category")
|
||||
|
||||
if title == "" || url == "" || category == "" {
|
||||
http.Error(w, "title, url and category are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
b := database.Bookmark{
|
||||
Title: title,
|
||||
URL: url,
|
||||
Category: category,
|
||||
}
|
||||
|
||||
id, err := database.AddBookmark(database.Bookmark{
|
||||
Title: title,
|
||||
URL: url,
|
||||
Category: category,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, "could not save bookmark", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
b = database.Bookmark{ID: id, Title: title, URL: url, Category: category}
|
||||
|
||||
view := bookmarkView{
|
||||
Bookmark: b,
|
||||
CategoryLabel: strings.ReplaceAll(b.Category, "-", " "),
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := addTmpl.Execute(w, view); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user