Embed templates and static files

This commit is contained in:
Maximilian Friedersdorff 2025-06-25 22:43:34 +01:00
parent 868d2a7bbd
commit 8edd857d22
11 changed files with 31 additions and 17 deletions

6
Makefile Normal file
View file

@ -0,0 +1,6 @@
all: build
build:
go run cmd/fetch-static/main.go
go build cmd/server/main.go

7
cmd/fetch-static/main.go Normal file
View file

@ -0,0 +1,7 @@
package main
import "forgejo.gwairfelin.com/max/gonotes/internal/conf"
func main() {
conf.FetchAssets()
}

View file

@ -27,8 +27,8 @@ func main() {
"/static/", "/static/",
logger( logger(
http.StripPrefix( http.StripPrefix(
"/static", "/static/",
http.FileServer(http.Dir(conf.Conf.Static.Dir)), http.FileServer(http.FS(conf.Static)),
), ),
), ),
) )

View file

@ -3,8 +3,5 @@ notesdir = "/var/lib/gonotes/saved_notes"
address = ":8080" address = ":8080"
protocol = "tcp" protocol = "tcp"
[templates]
dir = "/usr/share/gonotes/templates"
[static] [static]
dir = "/var/www/gonotes/static" dir = "/var/www/gonotes/static"

View file

@ -1,6 +1,7 @@
package conf package conf
import ( import (
"embed"
"errors" "errors"
"io" "io"
"log" "log"
@ -17,7 +18,7 @@ type Asset struct {
Url string Url string
} }
func (asset *Asset) FetchIfNotExists(staticPath string) { func (asset *Asset) fetchIfNotExists(staticPath string) {
destPath := filepath.Join(staticPath, asset.Path) destPath := filepath.Join(staticPath, asset.Path)
err := os.MkdirAll(path.Dir(destPath), os.FileMode(0750)) err := os.MkdirAll(path.Dir(destPath), os.FileMode(0750))
@ -57,12 +58,8 @@ type Config struct {
Protocol string Protocol string
Extension string Extension string
NotesDir string NotesDir string
Templates struct {
Dir string
}
Static struct { Static struct {
Dir string Dir string
Root string
} }
} }
@ -75,6 +72,10 @@ var (
{Path: "icons/eye.svg", Url: "https://raw.githubusercontent.com/twbs/icons/refs/heads/main/icons/eye.svg"}, {Path: "icons/eye.svg", Url: "https://raw.githubusercontent.com/twbs/icons/refs/heads/main/icons/eye.svg"},
} }
BaseTemplate string = "base.tmpl.html" BaseTemplate string = "base.tmpl.html"
//go:embed static/*
Static embed.FS
//go:embed templates/*
Templates embed.FS
) )
func LoadConfig(path string) { func LoadConfig(path string) {
@ -89,8 +90,10 @@ func LoadConfig(path string) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
func FetchAssets() {
for _, asset := range assets { for _, asset := range assets {
asset.FetchIfNotExists(Conf.Static.Dir) asset.fetchIfNotExists("./internal/conf/static")
} }
} }

0
internal/conf/static/.gitignore vendored Normal file
View file

View file

@ -3,7 +3,6 @@ package templ
import ( import (
"html/template" "html/template"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"forgejo.gwairfelin.com/max/gonotes/internal/conf" "forgejo.gwairfelin.com/max/gonotes/internal/conf"
@ -13,17 +12,19 @@ type Ctx map[string]any
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error { func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
files := []string{ files := []string{
filepath.Join(conf.Conf.Templates.Dir, conf.BaseTemplate), filepath.Join("templates", "base.tmpl.html"),
filepath.Join(conf.Conf.Templates.Dir, tmpl), filepath.Join("templates", tmpl),
} }
for _, f := range files { for _, f := range files {
_, err := os.Stat(f) file, err := conf.Templates.Open(f)
if err != nil { if err != nil {
return err return err
} }
file.Close()
} }
t, err := template.ParseFiles(files...)
t, err := template.ParseFS(conf.Templates, files...)
t.ExecuteTemplate(w, "base", context) t.ExecuteTemplate(w, "base", context)
return err return err
} }