199 lines
5.2 KiB
Go
199 lines
5.2 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"flag"
|
|
"fmt"
|
|
"html/template"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"site/database"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed source
|
|
var sourceFS embed.FS
|
|
|
|
func main() {
|
|
port := flag.Int("port", 8081, "port to listen on")
|
|
flag.Parse()
|
|
|
|
sourceRoot, err := fs.Sub(sourceFS, "source")
|
|
if err != nil {
|
|
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)
|
|
}
|
|
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServerFS(staticRoot)))
|
|
|
|
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFileFS(w, r, sourceRoot, "favicon.ico")
|
|
})
|
|
|
|
addr := fmt.Sprintf(":%d", *port)
|
|
log.Printf("running on http://localhost%s/\n", addr)
|
|
log.Fatal(http.ListenAndServe(addr, mux))
|
|
|
|
}
|
|
|
|
func servePage(name string, templatefile string, sourcefile fs.FS, sub bool) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
tmpl, err := template.ParseFS(sourcefile, "templates/main.html", templatefile)
|
|
if err != nil {
|
|
log.Println("parse error:", err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := struct {
|
|
Title string
|
|
Sub bool
|
|
}{
|
|
Title: name,
|
|
Sub: sub,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
// execute the base file by name — main.html is the entrypoint
|
|
if err := tmpl.ExecuteTemplate(w, "main.html", data); err != nil {
|
|
log.Println("execute error:", err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|