package middleware import ( "context" "farga/templating" "net/http" "github.com/google/uuid" "github.com/gorilla/mux" ) var UserHandleKey = "userHandle" // manages retrieving user DB data. func UserHandle(store Store) mux.MiddlewareFunc { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) userHandle := vars["user"] user, err := store.UserHandle(userHandle) if err != nil || user.ID == uuid.Nil { templating.Render(w, r, "base", "not_found") return } ctx := context.WithValue(r.Context(), UserHandleKey, user) r = r.WithContext(ctx) next.ServeHTTP(w, r) }) } }