From 3ad3666002cb5d29dd184f2a7c8a6646bba13291 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Tue, 4 Feb 2025 20:27:32 +0000 Subject: [PATCH] Download templates on boot --- README.md | 3 --- cmd/server/main.go | 4 +++- conf.toml | 6 +++++ internal/conf/conf.go | 48 ++++++++++++++++++++++++++++++++++++++-- static/.gitignore | 1 - templates/base.tmpl.html | 2 +- templates/edit.tmpl.html | 6 ++--- 7 files changed, 59 insertions(+), 11 deletions(-) delete mode 100644 static/.gitignore diff --git a/README.md b/README.md index 5e58c99..95f0d78 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,4 @@ It's not shared in the sense of simultaneous editing, don't do that! ## TODO * handle static urls in the django url mapping style -* Write a delete view * Style up the templates better -* Serve frontend dependencies from /static/ -* Populate frontend dependencies automagically? diff --git a/cmd/server/main.go b/cmd/server/main.go index df4c7ba..c17d08b 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "net/http" @@ -24,7 +25,8 @@ func main() { ), ), ) - log.Fatal(http.ListenAndServe(":8080", router)) + addr := fmt.Sprintf(":%d", conf.Conf.Port) + log.Fatal(http.ListenAndServe(addr, router)) } func logger(next http.Handler) http.Handler { diff --git a/conf.toml b/conf.toml index e92dae1..913dfa3 100644 --- a/conf.toml +++ b/conf.toml @@ -1,5 +1,6 @@ extension = "md" notesdir = "saved_notes" +port = 8080 [templates] dir = "templates" @@ -8,3 +9,8 @@ base = "base.tmpl.html" [static] dir = "/home/max/src/gonotes/static" root = "/static/" +assets = [ + { path = "css/bootstrap.min.css", url = "https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" }, + { path = "css/tiny-mde.min.css", url = "https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.css" }, + { path = "js/tiny-mde.min.js", url = "https://unpkg.com/tiny-markdown-editor/dist/tiny-mde.min.js" }, +] diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 1cbe524..0fb8c60 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -1,13 +1,52 @@ package conf import ( + "errors" + "io" "log" + "net/http" "os" + "path/filepath" "github.com/pelletier/go-toml/v2" ) +type Asset struct { + Path string + Url string +} + +func (asset *Asset) FetchIfNotExists(staticPath string) { + destPath := filepath.Join(staticPath, asset.Path) + + out, err := os.OpenFile( + destPath, + os.O_WRONLY|os.O_CREATE|os.O_EXCL, + 0666, + ) + if err != nil { + if errors.Is(err, os.ErrExist) { + log.Printf("%s already exists\n", destPath) + return + } + panic(err) + } + defer out.Close() + + resp, err := http.Get(asset.Url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + _, err = io.Copy(out, resp.Body) + if err != nil { + panic(err) + } +} + type Config struct { + Port int Extension string NotesDir string Templates struct { @@ -15,8 +54,9 @@ type Config struct { Base string } Static struct { - Dir string - Root string + Dir string + Root string + Assets []Asset } } @@ -34,4 +74,8 @@ func LoadConfig(path string) { if err != nil { log.Fatal(err) } + + for _, asset := range Conf.Static.Assets { + asset.FetchIfNotExists(Conf.Static.Dir) + } } diff --git a/static/.gitignore b/static/.gitignore deleted file mode 100644 index 72e8ffc..0000000 --- a/static/.gitignore +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/templates/base.tmpl.html b/templates/base.tmpl.html index 901a96d..479636a 100644 --- a/templates/base.tmpl.html +++ b/templates/base.tmpl.html @@ -5,7 +5,7 @@ {{template "title" .}} - diff --git a/templates/edit.tmpl.html b/templates/edit.tmpl.html index 27b7b17..ae5033b 100644 --- a/templates/edit.tmpl.html +++ b/templates/edit.tmpl.html @@ -1,7 +1,7 @@ {{define "title"}}Edit {{.note.Title}}{{end}} {{define "main"}} -
+
@@ -13,11 +13,11 @@ - +