package middleware import ( "context" "log" "net/http" "time" "github.com/limeleaf/diffdown/internal/auth" ) func Logger(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() next.ServeHTTP(w, r) log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start)) }) } // InjectUser adds user ID to context if logged in. Does not block. func InjectUser(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { userID := auth.GetUserID(r) if userID != "" { ctx := context.WithValue(r.Context(), auth.UserIDKey, userID) r = r.WithContext(ctx) } next.ServeHTTP(w, r) }) } // RequireAuth blocks unauthenticated requests. func RequireAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { userID := auth.UserIDFromContext(r.Context()) if userID == "" { http.Redirect(w, r, "/auth/login", http.StatusSeeOther) return } next.ServeHTTP(w, r) }) }