diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/config.go | 48 | ||||
| -rw-r--r-- | backend/data.go | 10 | ||||
| -rw-r--r-- | backend/home.go | 4 | ||||
| -rw-r--r-- | backend/router.go | 9 |
4 files changed, 66 insertions, 5 deletions
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 } |
