diff --git a/.gitignore b/.gitignore index 53ac819..1877c70 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ main saved_notes static conf.toml -gonotes diff --git a/Makefile b/Makefile deleted file mode 100644 index 5f145c6..0000000 --- a/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -all: build - -build: - go run cmd/fetch-static/main.go - go build -o gonotes cmd/server/main.go diff --git a/cmd/fetch-static/main.go b/cmd/fetch-static/main.go deleted file mode 100644 index 2d44b5c..0000000 --- a/cmd/fetch-static/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "forgejo.gwairfelin.com/max/gonotes/internal/conf" - -func main() { - conf.FetchAssets() -} diff --git a/cmd/server/main.go b/cmd/server/main.go index 13bd2cc..8d33e7f 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -26,7 +26,10 @@ func main() { router.Handle( "/static/", logger( - http.FileServer(http.FS(conf.Static)), + http.StripPrefix( + "/static", + http.FileServer(http.Dir(conf.Conf.Static.Dir)), + ), ), ) diff --git a/conf.example.toml b/conf.example.toml index 034c13e..1685e75 100644 --- a/conf.example.toml +++ b/conf.example.toml @@ -3,5 +3,8 @@ notesdir = "/var/lib/gonotes/saved_notes" address = ":8080" protocol = "tcp" +[templates] +dir = "/usr/share/gonotes/templates" + [static] dir = "/var/www/gonotes/static" diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 5124677..896c209 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -1,7 +1,6 @@ package conf import ( - "embed" "errors" "io" "log" @@ -18,7 +17,7 @@ type Asset struct { Url string } -func (asset *Asset) fetchIfNotExists(staticPath string) { +func (asset *Asset) FetchIfNotExists(staticPath string) { destPath := filepath.Join(staticPath, asset.Path) err := os.MkdirAll(path.Dir(destPath), os.FileMode(0750)) @@ -58,9 +57,13 @@ type Config struct { Protocol string Extension string NotesDir string - Static struct { + Templates struct { Dir string } + Static struct { + Dir string + Root string + } } var ( @@ -72,10 +75,6 @@ var ( {Path: "icons/eye.svg", Url: "https://raw.githubusercontent.com/twbs/icons/refs/heads/main/icons/eye.svg"}, } BaseTemplate string = "base.tmpl.html" - //go:embed static/css/* static/icons/* static/js/* - Static embed.FS - //go:embed templates/* - Templates embed.FS ) func LoadConfig(path string) { @@ -90,10 +89,8 @@ func LoadConfig(path string) { if err != nil { log.Fatal(err) } -} -func FetchAssets() { for _, asset := range assets { - asset.fetchIfNotExists("./internal/conf/static") + asset.FetchIfNotExists(Conf.Static.Dir) } } diff --git a/internal/conf/static/.gitignore b/internal/conf/static/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/internal/templ/templ.go b/internal/templ/templ.go index e8ef51d..deb1e0c 100644 --- a/internal/templ/templ.go +++ b/internal/templ/templ.go @@ -3,6 +3,7 @@ package templ import ( "html/template" "net/http" + "os" "path/filepath" "forgejo.gwairfelin.com/max/gonotes/internal/conf" @@ -12,19 +13,17 @@ type Ctx map[string]any func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error { files := []string{ - filepath.Join("templates", "base.tmpl.html"), - filepath.Join("templates", tmpl), + filepath.Join(conf.Conf.Templates.Dir, conf.BaseTemplate), + filepath.Join(conf.Conf.Templates.Dir, tmpl), } for _, f := range files { - file, err := conf.Templates.Open(f) + _, err := os.Stat(f) if err != nil { return err } - file.Close() } - - t, err := template.ParseFS(conf.Templates, files...) + t, err := template.ParseFiles(files...) t.ExecuteTemplate(w, "base", context) return err } diff --git a/internal/conf/templates/base.tmpl.html b/templates/base.tmpl.html similarity index 100% rename from internal/conf/templates/base.tmpl.html rename to templates/base.tmpl.html diff --git a/internal/conf/templates/edit.tmpl.html b/templates/edit.tmpl.html similarity index 100% rename from internal/conf/templates/edit.tmpl.html rename to templates/edit.tmpl.html diff --git a/internal/conf/templates/list.tmpl.html b/templates/list.tmpl.html similarity index 100% rename from internal/conf/templates/list.tmpl.html rename to templates/list.tmpl.html diff --git a/internal/conf/templates/view.tmpl.html b/templates/view.tmpl.html similarity index 100% rename from internal/conf/templates/view.tmpl.html rename to templates/view.tmpl.html