Files
donniemarko/internal/web/handler.go
adminoo b571588b15 feat(release): v0.2.0
commit 78d6c27c8940da32a6de8e64327c86f74fdaa2eb
Author: adminoo <git@kadath.corp>
Date:   Wed Feb 4 12:59:22 2026 +0100

    feat: freebsd log rotation config thingie

commit 55af4e6c70122e679272ed247c26e04b1247f694
Author: adminoo <git@kadath.corp>
Date:   Wed Feb 4 12:58:43 2026 +0100

    feat: embed templates, static resolution

commit 29c917f929a7378ec29c54315ee2e9f420747787
Author: adminoo <git@kadath.corp>
Date:   Wed Feb 4 10:44:34 2026 +0100

    feat: set log file path

commit 294fd3d1549979eab63587ceec6ff5d0978e9afc
Author: adminoo <git@kadath.corp>
Date:   Wed Feb 4 10:23:53 2026 +0100

    feat: logging HTTP request

commit c9ae80b240d58e1abed7ae3b7b2c3b283a31f1a1
Author: adminoo <git@kadath.corp>
Date:   Wed Feb 4 09:54:05 2026 +0100

    feat: freebsd-specific compile target and scripts

commit 86ca154dedd19aa1fe5f571c445dcf17a8396bfa
Author: adminoo <git@kadath.corp>
Date:   Wed Feb 4 09:25:16 2026 +0100

    feat: mobile friendly CSS

commit 199f4319e0b08a4b6d595d7eb3effb6db6c7beec
Author: adminoo <git@kadath.corp>
Date:   Wed Feb 4 09:25:03 2026 +0100

    feat: persisting rendered note

commit 865e258237e45d7c542685a4653bcad3c5af259d
Author: adminoo <git@kadath.corp>
Date:   Wed Feb 4 08:06:38 2026 +0100

    fix: grouping notes by folder

commit 242d1d074c92461f38212b033c7a9e383f9dc550
Author: adminoo <git@kadath.corp>
Date:   Tue Feb 3 16:52:50 2026 +0100

    feat: storage layer logic

    - Prune notes from db not matching current folder structure at start
    - Detect file system deletion on start by comparing in-db notes
    - Prevent updating of in-db notes at start if modification time is not
      newer
    - Delete by path

commit d75d46bc1ab22bd990d0fdc307e571fe52f0dd99
Author: adminoo <git@kadath.corp>
Date:   Tue Feb 3 15:27:07 2026 +0100

    feat: group notes by root folders

commit e1e25a938e717599332f7b40a449d9bb854b673a
Author: adminoo <git@kadath.corp>
Date:   Tue Feb 3 14:24:37 2026 +0100

    feat: size in kilobytes

commit 61220272a2df2b66c2b8e356ba359ed01de3bd12
Author: adminoo <git@kadath.corp>
Date:   Tue Feb 3 14:19:40 2026 +0100

    feat: styling inputs
2026-02-04 13:17:16 +01:00

360 lines
8.0 KiB
Go

package web
import (
"donniemarko/internal/note"
"donniemarko/internal/render"
"donniemarko/internal/service"
"html/template"
"log"
"net/http"
"net/url"
"path/filepath"
"sort"
"strings"
)
type Handler struct {
notesService *service.NotesService
templates *render.TemplateManager
mux *http.ServeMux
}
func NewHandler(ns *service.NotesService, tm *render.TemplateManager) *Handler {
return &Handler{
notesService: ns,
templates: tm,
mux: http.NewServeMux(),
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
log.Printf("request: method=%s path=%s host=%s remote=%s", r.Method, path, r.Host, r.RemoteAddr)
// Handle root and note list
if path == "/" {
h.handleRoot(w, r)
return
}
// Handle individual notes
if strings.HasPrefix(path, "/notes/") {
if strings.Contains(path, "/tags") {
h.handleTags(w, r)
return
}
h.handleNotes(w, r)
return
}
// Handle 404 for other paths
log.Printf("not found: method=%s path=%s host=%s remote=%s", r.Method, path, r.Host, r.RemoteAddr)
http.NotFound(w, r)
}
// ViewState is built per-request, not shared
type ViewState struct {
Notes []*note.Note
Groups []NoteGroup
Note *note.Note
RenderedNote template.HTML
SortBy string
SearchTerm string
TagFilter string
LastActive string
// BasePath is an optional URL prefix (e.g. "/donnie") when served behind a reverse proxy.
BasePath string
}
type NoteGroup struct {
Name string
Notes []*note.Note
}
func (h *Handler) handleRoot(w http.ResponseWriter, r *http.Request) {
// Build view state from query params
state, err := h.buildViewState(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
active := r.URL.Query().Get("active")
if active != "" {
_ = h.setActiveNote(state, active)
}
// Render with state
if err := h.templates.Render(w, "index", state); err != nil {
log.Printf("render error: %v", err)
http.Error(w, "Render error", http.StatusInternalServerError)
}
}
func (h *Handler) buildViewState(r *http.Request) (*ViewState, error) {
query := r.URL.Query()
// Extract params
sortBy := query.Get("sort")
if sortBy == "" {
sortBy = "recent"
}
searchTerm := query.Get("search")
tagFilter := query.Get("tag")
active := query.Get("active")
// Preserve proxy prefix so templates can build correct links and asset URLs.
basePath := basePathFromRequest(r)
// Get notes from service
var notes []*note.Note
var err error
if searchTerm != "" {
opts := service.QueryOptions{
SearchTerm: searchTerm,
SortBy: sortBy,
}
notes, err = h.notesService.QueryNotes(opts)
if err != nil {
return nil, err
}
} else {
notes = h.notesService.GetNotes()
// Apply sorting
switch sortBy {
case "recent":
service.SortByDate(notes)
case "oldest":
service.SortByDateAsc(notes)
case "alpha":
service.SortByTitle(notes)
case "ralpha":
service.SortByTitleAsc(notes)
default:
service.SortByDate(notes)
}
}
if tagFilter != "" {
notes = filterNotesByTag(notes, tagFilter)
}
return &ViewState{
Notes: notes,
Groups: groupNotesByFolder(notes),
SortBy: sortBy,
SearchTerm: searchTerm,
TagFilter: tagFilter,
LastActive: active,
BasePath: basePath,
}, nil
}
func (h *Handler) SetupRoutes() {
// Set the handler as the main handler for http.DefaultServeMux
http.Handle("/", h)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(StaticFS()))))
}
func extractHash(path string) string {
// Extract hash from /notes/{hash}
parts := strings.Split(strings.Trim(path, "/"), "/")
if len(parts) < 2 || parts[0] != "notes" {
return ""
}
return parts[1]
}
func (h *Handler) handleNotes(w http.ResponseWriter, r *http.Request) {
// Build base state
state, err := h.buildViewState(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Extract hash from URL
hash := extractHash(r.URL.Path)
// Get specific note
note, err := h.notesService.GetNoteByHash(hash)
if err != nil {
http.Error(w, "Note not found", http.StatusNotFound)
return
}
// Convert markdown to HTML
htmlContent, err := render.RenderMarkdown([]byte(note.Content))
if err != nil {
http.Error(w, "Failed to render markdown", http.StatusInternalServerError)
return
}
// Add to state
state.Note = note
state.RenderedNote = htmlContent
state.LastActive = hash
// Ensure note view carries proxy prefix for links/forms.
state.BasePath = basePathFromRequest(r)
if err := h.templates.Render(w, "index", state); err != nil {
log.Printf("render error: %v", err)
http.Error(w, "Render error", http.StatusInternalServerError)
}
}
func (h *Handler) setActiveNote(state *ViewState, noteID string) error {
note, err := h.notesService.GetNoteByHash(noteID)
if err != nil {
return err
}
htmlContent, err := render.RenderMarkdown([]byte(note.Content))
if err != nil {
return err
}
state.Note = note
state.RenderedNote = htmlContent
state.LastActive = noteID
return nil
}
func filterNotesByTag(notes []*note.Note, tag string) []*note.Note {
tag = strings.ToLower(strings.TrimSpace(tag))
if tag == "" {
return notes
}
filtered := make([]*note.Note, 0, len(notes))
for _, n := range notes {
for _, t := range n.Tags {
if strings.EqualFold(t, tag) {
filtered = append(filtered, n)
break
}
}
}
return filtered
}
func groupNotesByFolder(notes []*note.Note) []NoteGroup {
groups := make(map[string][]*note.Note)
for _, n := range notes {
name := rootFolderName(n.Path)
groups[name] = append(groups[name], n)
}
if len(groups) == 0 {
return nil
}
names := make([]string, 0, len(groups))
for name := range groups {
names = append(names, name)
}
sort.Strings(names)
result := make([]NoteGroup, 0, len(names))
for _, name := range names {
result = append(result, NoteGroup{
Name: name,
Notes: groups[name],
})
}
return result
}
func rootFolderName(path string) string {
if path == "" {
return "root"
}
clean := filepath.ToSlash(filepath.Clean(path))
if clean == "." || clean == "/" {
return "root"
}
clean = strings.TrimPrefix(clean, "/")
parts := strings.Split(clean, "/")
if len(parts) == 0 || parts[0] == "." {
return "root"
}
if len(parts) == 1 {
return "root"
}
return parts[0]
}
func basePathFromRequest(r *http.Request) string {
// X-Forwarded-Prefix is commonly set by reverse proxies for subpath mounts.
prefix := strings.TrimSpace(r.Header.Get("X-Forwarded-Prefix"))
if prefix == "" {
return ""
}
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
return strings.TrimRight(prefix, "/")
}
func (h *Handler) handleTags(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
noteID, tag, isRemove := parseTagRoute(r.URL.Path)
if noteID == "" {
http.NotFound(w, r)
return
}
if isRemove {
if tag == "" {
http.Error(w, "Missing tag", http.StatusBadRequest)
return
}
if err := h.notesService.RemoveTag(noteID, tag); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form", http.StatusBadRequest)
return
}
tag := r.FormValue("tag")
if tag == "" {
http.Error(w, "Missing tag", http.StatusBadRequest)
return
}
if err := h.notesService.AddTag(noteID, tag); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// Redirect using proxy prefix when present.
basePath := basePathFromRequest(r)
http.Redirect(w, r, basePath+"/notes/"+noteID, http.StatusSeeOther)
}
func parseTagRoute(path string) (noteID string, tag string, isRemove bool) {
parts := strings.Split(strings.Trim(path, "/"), "/")
if len(parts) < 3 || parts[0] != "notes" || parts[2] != "tags" {
return "", "", false
}
noteID = parts[1]
if len(parts) >= 4 {
decoded, err := url.PathUnescape(parts[3])
if err != nil {
return noteID, "", true
}
return noteID, decoded, true
}
return noteID, "", false
}