diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a1efc1b --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +all: build + +build: + go run cmd/fetch-static/main.go + go build cmd/server/main.go + diff --git a/cmd/fetch-static/main.go b/cmd/fetch-static/main.go new file mode 100644 index 0000000..2d44b5c --- /dev/null +++ b/cmd/fetch-static/main.go @@ -0,0 +1,7 @@ +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 8d33e7f..e86faaf 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -27,8 +27,8 @@ func main() { "/static/", logger( http.StripPrefix( - "/static", - http.FileServer(http.Dir(conf.Conf.Static.Dir)), + "/static/", + http.FileServer(http.FS(conf.Static)), ), ), ) diff --git a/conf.example.toml b/conf.example.toml index 1685e75..034c13e 100644 --- a/conf.example.toml +++ b/conf.example.toml @@ -3,8 +3,5 @@ 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 896c209..aba9c12 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -1,6 +1,7 @@ package conf import ( + "embed" "errors" "io" "log" @@ -17,7 +18,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)) @@ -57,13 +58,9 @@ type Config struct { Protocol string Extension string NotesDir string - Templates struct { + Static struct { Dir string } - Static struct { - Dir string - Root string - } } var ( @@ -75,6 +72,10 @@ 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/* + Static embed.FS + //go:embed templates/* + Templates embed.FS ) func LoadConfig(path string) { @@ -89,8 +90,10 @@ func LoadConfig(path string) { if err != nil { log.Fatal(err) } +} +func FetchAssets() { for _, asset := range assets { - asset.FetchIfNotExists(Conf.Static.Dir) + asset.fetchIfNotExists("./internal/conf/static") } } diff --git a/internal/conf/static/.gitignore b/internal/conf/static/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/templates/base.tmpl.html b/internal/conf/templates/base.tmpl.html similarity index 100% rename from templates/base.tmpl.html rename to internal/conf/templates/base.tmpl.html diff --git a/templates/edit.tmpl.html b/internal/conf/templates/edit.tmpl.html similarity index 100% rename from templates/edit.tmpl.html rename to internal/conf/templates/edit.tmpl.html diff --git a/templates/list.tmpl.html b/internal/conf/templates/list.tmpl.html similarity index 100% rename from templates/list.tmpl.html rename to internal/conf/templates/list.tmpl.html diff --git a/templates/view.tmpl.html b/internal/conf/templates/view.tmpl.html similarity index 100% rename from templates/view.tmpl.html rename to internal/conf/templates/view.tmpl.html diff --git a/internal/templ/templ.go b/internal/templ/templ.go index deb1e0c..e8ef51d 100644 --- a/internal/templ/templ.go +++ b/internal/templ/templ.go @@ -3,7 +3,6 @@ package templ import ( "html/template" "net/http" - "os" "path/filepath" "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 { files := []string{ - filepath.Join(conf.Conf.Templates.Dir, conf.BaseTemplate), - filepath.Join(conf.Conf.Templates.Dir, tmpl), + filepath.Join("templates", "base.tmpl.html"), + filepath.Join("templates", tmpl), } for _, f := range files { - _, err := os.Stat(f) + file, err := conf.Templates.Open(f) if err != nil { return err } + file.Close() } - t, err := template.ParseFiles(files...) + + t, err := template.ParseFS(conf.Templates, files...) t.ExecuteTemplate(w, "base", context) return err }