embed web files for build

This commit is contained in:
nahakubuilde
2025-08-26 19:55:28 +01:00
parent 4cafd9848f
commit 6fb6054803
4 changed files with 77 additions and 29 deletions

View File

@@ -3,12 +3,15 @@ package server
import (
"fmt"
"html/template"
"io/fs"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/sessions"
webassets "gobsidian/web"
"gobsidian/internal/auth"
"gobsidian/internal/config"
"gobsidian/internal/handlers"
@@ -173,8 +176,14 @@ func (s *Server) setupRoutes() {
}
func (s *Server) setupStaticFiles() {
s.router.Static("/static", "./web/static")
s.router.StaticFile("/favicon.ico", "./web/static/favicon.ico")
// Serve /static from embedded web/static
sub, err := fs.Sub(webassets.StaticFS, "static")
if err != nil {
panic(err)
}
s.router.StaticFS("/static", http.FS(sub))
// Favicon from same sub FS
s.router.StaticFileFS("/favicon.ico", "favicon.ico", http.FS(sub))
}
func (s *Server) setupTemplates() {
@@ -244,8 +253,12 @@ func (s *Server) setupTemplates() {
},
}
// Load templates - make sure base.html is loaded with all the other templates
templates := template.Must(template.New("").Funcs(funcMap).ParseGlob("web/templates/*.html"))
// Load templates from embedded FS
tplFS, err := fs.Sub(webassets.TemplatesFS, "templates")
if err != nil {
panic(err)
}
templates := template.Must(template.New("").Funcs(funcMap).ParseFS(tplFS, "*.html"))
s.router.SetHTMLTemplate(templates)
fmt.Printf("DEBUG: Templates loaded successfully\n")
@@ -253,10 +266,10 @@ func (s *Server) setupTemplates() {
// startAccessLogCleanup deletes access logs older than 7 days once at startup and then daily.
func (s *Server) startAccessLogCleanup() {
// initial cleanup
_, _ = s.auth.DB.Exec(`DELETE FROM access_logs WHERE created_at < DATETIME('now', '-7 days')`)
ticker := time.NewTicker(24 * time.Hour)
for range ticker.C {
_, _ = s.auth.DB.Exec(`DELETE FROM access_logs WHERE created_at < DATETIME('now', '-7 days')`)
}
// initial cleanup
_, _ = s.auth.DB.Exec(`DELETE FROM access_logs WHERE created_at < DATETIME('now', '-7 days')`)
ticker := time.NewTicker(24 * time.Hour)
for range ticker.C {
_, _ = s.auth.DB.Exec(`DELETE FROM access_logs WHERE created_at < DATETIME('now', '-7 days')`)
}
}