1package web
2
3import (
4 "context"
5 "net/http"
6
7 "charm.land/log/v2"
8 "github.com/charmbracelet/soft-serve/pkg/config"
9 "github.com/gorilla/handlers"
10 "github.com/gorilla/mux"
11)
12
13// NewRouter returns a new HTTP router.
14func NewRouter(ctx context.Context) http.Handler {
15 logger := log.FromContext(ctx).WithPrefix("http")
16 router := mux.NewRouter()
17
18 // Health routes
19 HealthController(ctx, router)
20
21 // Git routes
22 GitController(ctx, router)
23
24 // Web UI routes (must be after Git routes)
25 WebUIController(ctx, router)
26
27 // Catch-all route (must be last)
28 router.PathPrefix("/").HandlerFunc(renderNotFound)
29
30 // Context handler
31 // Adds context to the request
32 h := NewLoggingMiddleware(router, logger)
33 h = NewContextHandler(ctx)(h)
34 h = handlers.CompressHandler(h)
35 h = handlers.RecoveryHandler()(h)
36
37 cfg := config.FromContext(ctx)
38
39 h = handlers.CORS(handlers.AllowedHeaders(cfg.HTTP.CORS.AllowedHeaders),
40 handlers.AllowedOrigins(cfg.HTTP.CORS.AllowedOrigins),
41 handlers.AllowedMethods(cfg.HTTP.CORS.AllowedMethods),
42 )(h)
43
44 return h
45}