From 97045512306ed91b690a0d4c20bbb8fe84d7dbfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Fri, 3 Oct 2025 20:47:55 +0200 Subject: feat(backend): set default image and public folder in config --- backend/config.go | 21 +++++++++++++-------- backend/data.go | 9 +++++++++ backend/templates/base.html | 12 ++++-------- main.go | 8 +------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/backend/config.go b/backend/config.go index 33665ea..8c111ae 100644 --- a/backend/config.go +++ b/backend/config.go @@ -18,14 +18,18 @@ type Logo struct { } type Config struct { - Domain string `toml:"domain"` - Name string `toml:"name"` - Description string `toml:"description"` - Links []Link `toml:"links"` - Logo Logo `toml:"logo"` - LogFolder string `toml:"log_folder"` - RootFolder string `toml:"root_folder"` - Quotes []string `toml:"quotes"` + Domain string `toml:"domain"` + Name string `toml:"name"` + Description string `toml:"description"` + DefaultImage string `toml:"default_image"` + Quotes []string `toml:"quotes"` + + LogFolder string `toml:"log_folder"` + RootFolder string `toml:"root_folder"` + PublicFolder string `toml:"public_folder"` + + Links []Link `toml:"links"` + Logo Logo `toml:"logo"` } func (c *Config) DefaultValues() { @@ -48,6 +52,7 @@ func (c *Config) DefaultValues() { } c.LogFolder = "data/logs" c.RootFolder = "data" + c.PublicFolder = "public" c.Quotes = []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do."} } diff --git a/backend/data.go b/backend/data.go index 741a5c4..3635a7e 100644 --- a/backend/data.go +++ b/backend/data.go @@ -58,6 +58,9 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string d.Quote = cfg.Quotes[rand.Intn(len(cfg.Quotes))] } } + if d.Image == "" { + d.Image = cfg.DefaultImage + } if d.URL == "" { if !strings.HasPrefix(r.URL.Path, "/") { r.URL.Path = "/" + r.URL.Path @@ -71,6 +74,12 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string } return fmt.Sprintf("/static/%s", path) }, + "fullStatic": func(path string) string { + if regexIsHttp.MatchString(path) { + return path + } + return fmt.Sprintf("https://%s/static/%s", cfg.Domain, path) + }, "assets": func(path string) string { if regexIsHttp.MatchString(path) { return path diff --git a/backend/templates/base.html b/backend/templates/base.html index a79f4c1..f0ae617 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -11,7 +11,7 @@ - + @@ -19,20 +19,16 @@ - + - +
Logo
{{ template "body" . }} diff --git a/main.go b/main.go index 47a9e22..dc955bc 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,6 @@ var embeds embed.FS var ( configFile = "config.toml" port = 8000 - publicDir = "public" dev = false ) @@ -45,11 +44,6 @@ func init() { } } flag.IntVar(&port, "port", port, "server port") - - if v := os.Getenv("PUBLIC_DIR"); v != "" { - publicDir = v - } - flag.StringVar(&publicDir, "public", publicDir, "public directory") flag.BoolVar(&dev, "dev", false, "development mode") } @@ -81,7 +75,7 @@ func main() { } else { backend.HandleStaticFiles(r, "/assets", backend.UsableEmbedFS("dist", embeds)) } - backend.HandleStaticFiles(r, "/static", os.DirFS(publicDir)) + backend.HandleStaticFiles(r, "/static", os.DirFS(cfg.PublicFolder)) slog.Info("starting http server") server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: r} -- cgit v1.2.3