package api import ( "context" "encoding/json" "net/http" "github.com/mjensen/weimar/internal/auth" "github.com/mjensen/weimar/internal/config" "github.com/mjensen/weimar/internal/db" "github.com/mjensen/weimar/internal/storage" ) type ctxKey string const CtxKeyUser ctxKey = "user" // UserFromContext extracts the authenticated user from the request context. func UserFromContext(ctx context.Context) *db.User { user, _ := ctx.Value(CtxKeyUser).(*db.User) return user } // API holds dependencies shared by all HTTP handlers. type API struct { DB *db.DB Auth *auth.SessionManager Storage *storage.Storage Cfg *config.Config } func New(database *db.DB, sm *auth.SessionManager, st *storage.Storage, cfg *config.Config) *API { return &API{ DB: database, Auth: sm, Storage: st, Cfg: cfg, } } func writeJSON(w http.ResponseWriter, status int, v any) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) json.NewEncoder(w).Encode(v) } func writeError(w http.ResponseWriter, status int, msg string) { writeJSON(w, status, map[string]string{"error": msg}) }