Files
atlas/internal/httpapp/app.go
othman.suseno a6da313dfc
Some checks failed
CI / test-build (push) Failing after 59s
add api framework
2025-12-14 22:15:56 +07:00

70 lines
1.2 KiB
Go

package httpapp
import (
"fmt"
"html/template"
"net/http"
"path/filepath"
"time"
)
type Config struct {
Addr string
TemplatesDir string
StaticDir string
}
type App struct {
cfg Config
tmpl *template.Template
mux *http.ServeMux
}
func New(cfg Config) (*App, error) {
if cfg.TemplatesDir == "" {
cfg.TemplatesDir = "web/templates"
}
if cfg.StaticDir == "" {
cfg.StaticDir = "web/static"
}
tmpl, err := parseTemplates(cfg.TemplatesDir)
if err != nil {
return nil, err
}
a := &App{
cfg: cfg,
tmpl: tmpl,
mux: http.NewServeMux(),
}
a.routes()
return a, nil
}
func (a *App) Router() http.Handler {
// Wrap the mux with basic middleware chain
return requestID(logging(a.mux))
}
// routes() is now in routes.go
func parseTemplates(dir string) (*template.Template, error) {
pattern := filepath.Join(dir, "*.html")
files, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
if len(files) == 0 {
return nil, fmt.Errorf("no templates found at %s", pattern)
}
funcs := template.FuncMap{
"nowRFC3339": func() string { return time.Now().Format(time.RFC3339) },
}
t := template.New("root").Funcs(funcs)
return t.ParseFiles(files...)
}