1package web
 2
 3import (
 4	"context"
 5	"net/http"
 6
 7	"github.com/charmbracelet/log"
 8	"github.com/charmbracelet/soft-serve/server/backend"
 9	"github.com/charmbracelet/soft-serve/server/config"
10)
11
12// NewContextMiddleware returns a new context middleware.
13// This middleware adds the config, backend, and logger to the request context.
14func NewContextMiddleware(ctx context.Context) func(http.Handler) http.Handler {
15	cfg := config.FromContext(ctx)
16	be := backend.FromContext(ctx)
17	logger := log.FromContext(ctx).WithPrefix("http")
18	return func(next http.Handler) http.Handler {
19		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20			ctx := r.Context()
21			ctx = config.WithContext(ctx, cfg)
22			ctx = backend.WithContext(ctx, be)
23			ctx = log.WithContext(ctx, logger)
24			r = r.WithContext(ctx)
25			next.ServeHTTP(w, r)
26		})
27	}
28}