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	"github.com/charmbracelet/soft-serve/server/db"
11	"github.com/charmbracelet/soft-serve/server/store"
12)
13
14// NewContextHandler returns a new context middleware.
15// This middleware adds the config, backend, and logger to the request context.
16func NewContextHandler(ctx context.Context) func(http.Handler) http.Handler {
17	cfg := config.FromContext(ctx)
18	be := backend.FromContext(ctx)
19	logger := log.FromContext(ctx).WithPrefix("http")
20	logger.Infof("data path %s", cfg.DataPath)
21	dbx := db.FromContext(ctx)
22	datastore := store.FromContext(ctx)
23	return func(next http.Handler) http.Handler {
24		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25			ctx := r.Context()
26			ctx = config.WithContext(ctx, cfg)
27			ctx = backend.WithContext(ctx, be)
28			ctx = log.WithContext(ctx, logger)
29			ctx = db.WithContext(ctx, dbx)
30			ctx = store.WithContext(ctx, datastore)
31			r = r.WithContext(ctx)
32			next.ServeHTTP(w, r)
33		})
34	}
35}