From 5907450d569c997809674a2abeec97ddf0afe4aa Mon Sep 17 00:00:00 2001 From: mihna123 Date: Fri, 4 Sep 2026 01:13:17 +0200 Subject: config: add config package and wire it into server and handlers Replace the hardcoded server.Config with an ini-backed config package that reads /etc/openstore/openstore.conf then the user's config file, so user settings override system ones and both override the built-in defaults. Missing files are ignored rather than fatal. Move route registration into a handlers package that owns the templates and builds a Page view model, which removes the server -> handlers import cycle along with the setter-based context injection. --- internal/server/server.go | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) (limited to 'internal/server/server.go') diff --git a/internal/server/server.go b/internal/server/server.go index a444958..a2f2d45 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -5,55 +5,39 @@ import ( "net" "time" - "html/template" "net/http" - "codeberg.org/mihna123/openstore/internal/web" + "codeberg.org/mihna123/openstore/internal/config" + "codeberg.org/mihna123/openstore/internal/server/handlers" ) -type Config struct { - Name string -} - type Server struct { - config *Config - tmpl *template.Template + config *config.Config mux *http.ServeMux } -func (s *Server) routes() { - s.mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { - err := s.tmpl.ExecuteTemplate(w, "index.html", s.config) - - if err != nil { - slog.Error("render template", "err", err.Error()) - return - } - }) -} - -func (s *Server) Serve(addr string) error { +func (s *Server) Serve() error { httpSrv := &http.Server{ Handler: logRequests(s.mux), ReadHeaderTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, } - ln, err := net.Listen("tcp", addr) + ln, err := net.Listen("tcp", s.config.Serve.Addr) if err != nil { return err } - slog.Info("listening", "name", s.config.Name, "addr", addr) + slog.Info("listening", "name", s.config.Instance.Name, "addr", s.config.Serve.Addr) return httpSrv.Serve(ln) } -func New(c *Config) *Server { +func New(c *config.Config) *Server { + mux := handlers.Routes(c) s := &Server{ config: c, - mux: http.NewServeMux(), - tmpl: template.Must(template.ParseFS(web.Files, "templates/*.html")), + mux: mux, } - s.routes() + return s } -- cgit v1.3.1