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