package server import ( "log/slog" "net" "time" "html/template" "net/http" "codeberg.org/mihna123/openstore/internal/web" ) type Config struct { Name string } type Server struct { config *Config tmpl *template.Template 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 { httpSrv := &http.Server{ Handler: logRequests(s.mux), ReadHeaderTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, } ln, err := net.Listen("tcp", addr) if err != nil { return err } slog.Info("listening", "name", s.config.Name, "addr", addr) return httpSrv.Serve(ln) } func New(c *Config) *Server { s := &Server{ config: c, mux: http.NewServeMux(), tmpl: template.Must(template.ParseFS(web.Files, "templates/*.html")), } s.routes() return s }