1package web
2
3import (
4 "context"
5 "net/http"
6
7 "github.com/charmbracelet/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 router.PathPrefix("/").HandlerFunc(renderNotFound)
25
26 // Context handler
27 // Adds context to the request
28 h := NewLoggingMiddleware(router, logger)
29 h = NewContextHandler(ctx)(h)
30 h = handlers.CompressHandler(h)
31 h = handlers.RecoveryHandler()(h)
32
33 cfg := config.FromContext(ctx)
34
35 h = handlers.CORS(handlers.AllowedHeaders(cfg.HTTP.CORS.AllowedHeaders),
36 handlers.AllowedOrigins(cfg.HTTP.CORS.AllowedOrigins),
37 handlers.AllowedMethods(cfg.HTTP.CORS.AllowedMethods),
38 )(h)
39
40 return h
41}