ExtractUserParam extracts a user from an ID/username in the {user} URL parameter.
(db database.Store)
| 39 | // ExtractUserParam extracts a user from an ID/username in the {user} URL |
| 40 | // parameter. |
| 41 | func ExtractUserParam(db database.Store) func(http.Handler) http.Handler { |
| 42 | return func(next http.Handler) http.Handler { |
| 43 | return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 44 | ctx := r.Context() |
| 45 | user, ok := ExtractUserContext(ctx, db, rw, r) |
| 46 | if !ok { |
| 47 | // response already handled |
| 48 | return |
| 49 | } |
| 50 | ctx = context.WithValue(ctx, userParamContextKey{}, user) |
| 51 | next.ServeHTTP(rw, r.WithContext(ctx)) |
| 52 | }) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // ExtractUserParamOptional does not fail if no user is present. |
| 57 | func ExtractUserParamOptional(db database.Store) func(http.Handler) http.Handler { |