From 49766901293631aeabfc96e8d80aba305f420630 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 2 Oct 2025 18:36:06 +0200 Subject: feat(backend): config to set domain --- .gitignore | 1 + backend/config.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++- backend/data.go | 10 +++++++++- backend/home.go | 4 ++-- backend/router.go | 9 ++++++++- go.mod | 5 ++++- go.sum | 2 ++ main.go | 10 ++++++++-- 8 files changed, 81 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 5f15817..20abba6 100644 --- a/.gitignore +++ b/.gitignore @@ -157,3 +157,4 @@ go.work.sum .idea public +config.toml diff --git a/backend/config.go b/backend/config.go index b7624fd..98aeebe 100644 --- a/backend/config.go +++ b/backend/config.go @@ -1,3 +1,49 @@ package backend -type Config struct{} +import ( + "errors" + "log/slog" + "os" + + "github.com/pelletier/go-toml/v2" +) + +type Config struct { + Domain string `toml:"domain"` +} + +func (c *Config) DefaultValues() { + c.Domain = "example.org" +} + +func LoadConfig(path string) (*Config, bool) { + b, err := os.ReadFile(path) + var config Config + if err != nil { + if !errors.Is(err, os.ErrNotExist) { + slog.Error("reading config file", "error", err) + return nil, false + } + slog.Warn("config file not found", "path", path) + slog.Info("creating a new config file", "path", path) + config.DefaultValues() + b, err = toml.Marshal(&config) + if err != nil { + slog.Error("marshalling config file", "error", err) + return nil, false + } + err = os.WriteFile(path, b, 0660) + if err != nil { + slog.Error("writing config file", "error", err, "path", path) + } else { + slog.Info("config file created", "path", path) + } + return nil, false + } + err = toml.Unmarshal(b, &config) + if err != nil { + slog.Error("unmarshalling config file", "error", err) + return nil, false + } + return &config, true +} diff --git a/backend/data.go b/backend/data.go index bc74ba5..3bc93d1 100644 --- a/backend/data.go +++ b/backend/data.go @@ -4,6 +4,7 @@ import ( "fmt" "html/template" "net/http" + "strings" ) type data struct { @@ -15,7 +16,14 @@ type data struct { Description string } -func (d *data) handleGeneric(w http.ResponseWriter, name string) { +func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string) { + if d.Domain == "" { + cfg := r.Context().Value("config").(*Config) + d.Domain = cfg.Domain + } + if d.URL == "" { + d.URL = strings.TrimPrefix(r.URL.Path, "/") + } t, err := template.New("").Funcs(template.FuncMap{ "static": func(path string) string { return fmt.Sprintf("/static/%s", path) diff --git a/backend/home.go b/backend/home.go index a7f2ae1..83e04d9 100644 --- a/backend/home.go +++ b/backend/home.go @@ -7,7 +7,7 @@ import ( ) func HandleHome(r *chi.Mux) { - r.Get("/", func(w http.ResponseWriter, _ *http.Request) { + r.Get("/", func(w http.ResponseWriter, r *http.Request) { d := &data{ title: "", Article: false, @@ -16,6 +16,6 @@ func HandleHome(r *chi.Mux) { Image: "", Description: "", } - d.handleGeneric(w, "home") + d.handleGeneric(w, r, "home") }) } diff --git a/backend/router.go b/backend/router.go index 8ed63fa..374e28b 100644 --- a/backend/router.go +++ b/backend/router.go @@ -1,6 +1,7 @@ package backend import ( + "context" "embed" "io/fs" "log/slog" @@ -19,7 +20,7 @@ const Version = "0.1.0" //go:embed templates var templates embed.FS -func NewRouter(debug bool) *chi.Mux { +func NewRouter(debug bool, cfg *Config) *chi.Mux { logFormat := httplog.SchemaECS.Concise(!debug) logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ @@ -49,6 +50,12 @@ func NewRouter(debug bool) *chi.Mux { LogRequestHeaders: []string{"Origin"}, LogResponseHeaders: []string{}, })) + r.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := context.WithValue(r.Context(), "config", cfg) + next.ServeHTTP(w, r.WithContext(ctx)) + }) + }) return r } diff --git a/go.mod b/go.mod index 35e75ed..eb2f5fa 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,7 @@ require ( github.com/joho/godotenv v1.5.1 ) -require github.com/go-chi/httplog/v3 v3.2.2 // indirect +require ( + github.com/go-chi/httplog/v3 v3.2.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect +) diff --git a/go.sum b/go.sum index abad55a..e8af854 100644 --- a/go.sum +++ b/go.sum @@ -4,3 +4,5 @@ github.com/go-chi/httplog/v3 v3.2.2 h1:G0oYv3YYcikNjijArHFUlqfR78cQNh9fGT43i6Stq github.com/go-chi/httplog/v3 v3.2.2/go.mod h1:N/J1l5l1fozUrqIVuT8Z/HzNeSy8TF2EFyokPLe6y2w= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= diff --git a/main.go b/main.go index e1c13bb..abab2ea 100644 --- a/main.go +++ b/main.go @@ -56,7 +56,13 @@ func init() { func main() { flag.Parse() - r := backend.NewRouter(dev) + cfg, ok := backend.LoadConfig(configFile) + if !ok { + slog.Info("exiting") + os.Exit(1) + } + + r := backend.NewRouter(dev, cfg) backend.HandleHome(r) @@ -72,7 +78,7 @@ func main() { sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) - ok := true + ok = true for ok { select { case err := <-errChan: -- cgit v1.2.3