From 73a1c8bc0285414c987290c12df2fe901c7e8456 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Tue, 28 Jan 2025 22:08:01 +0000 Subject: [PATCH] Server static files --- cmd/server/main.go | 16 ++++++++++++++++ conf.toml | 10 ++++++++-- go.mod | 7 ++++--- go.sum | 12 ++++++++++-- internal/conf/conf.go | 16 +++++++++++----- internal/templ/templ.go | 4 ++-- static/.gitignore | 1 + 7 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 static/.gitignore diff --git a/cmd/server/main.go b/cmd/server/main.go index b07945d..df4c7ba 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -15,5 +15,21 @@ func main() { notesRouter := views.GetRoutes("/notes") router.Handle("/notes/", http.StripPrefix("/notes", notesRouter)) + router.Handle( + conf.Conf.Static.Root, + logger( + http.StripPrefix( + "/static", + http.FileServer(http.Dir(conf.Conf.Static.Dir)), + ), + ), + ) log.Fatal(http.ListenAndServe(":8080", router)) } + +func logger(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log.Print(r.URL.Path) + next.ServeHTTP(w, r) + }) +} diff --git a/conf.toml b/conf.toml index fac9bd3..e92dae1 100644 --- a/conf.toml +++ b/conf.toml @@ -1,4 +1,10 @@ extension = "md" notesdir = "saved_notes" -templatesdir = "templates" -basetemplate = "base.tmpl.html" + +[templates] +dir = "templates" +base = "base.tmpl.html" + +[static] +dir = "/home/max/src/gonotes/static" +root = "/static/" diff --git a/go.mod b/go.mod index e992aff..4317ecf 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.5 require github.com/yuin/goldmark v1.7.8 -require github.com/pelletier/go-toml v1.9.5 - -require gitea.gwairfelin.com/max/gispatcho v0.1.1 +require ( + gitea.gwairfelin.com/max/gispatcho v0.1.1 + github.com/pelletier/go-toml/v2 v2.2.3 +) diff --git a/go.sum b/go.sum index f27af30..acf21a6 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,14 @@ gitea.gwairfelin.com/max/gispatcho v0.1.1 h1:8Zv6UH046mSSLdf0PqpNRK6DACuWJfgBSUiWhMW0sMY= gitea.gwairfelin.com/max/gispatcho v0.1.1/go.mod h1:TRhxU+uNv+nIcFIxy2jl30DPbcM5Yib0S/cpLXws5iM= -github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= -github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic= github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 4cb43f1..1cbe524 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -4,14 +4,20 @@ import ( "log" "os" - "github.com/pelletier/go-toml" + "github.com/pelletier/go-toml/v2" ) type Config struct { - Extension string - NotesDir string - TemplatesDir string - BaseTemplate string + Extension string + NotesDir string + Templates struct { + Dir string + Base string + } + Static struct { + Dir string + Root string + } } var Conf Config diff --git a/internal/templ/templ.go b/internal/templ/templ.go index 0b9aa0d..7a5ca44 100644 --- a/internal/templ/templ.go +++ b/internal/templ/templ.go @@ -13,8 +13,8 @@ type Ctx map[string]any func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error { files := []string{ - filepath.Join(conf.Conf.TemplatesDir, conf.Conf.BaseTemplate), - filepath.Join(conf.Conf.TemplatesDir, tmpl), + filepath.Join(conf.Conf.Templates.Dir, conf.Conf.Templates.Base), + filepath.Join(conf.Conf.Templates.Dir, tmpl), } for _, f := range files { diff --git a/static/.gitignore b/static/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/static/.gitignore @@ -0,0 +1 @@ +*