MCPcopy Index your code
hub / github.com/coder/coder / ExtractUserParamOptional

Function ExtractUserParamOptional

coderd/httpmw/userparam.go:57–70  ·  view source on GitHub ↗

ExtractUserParamOptional does not fail if no user is present.

(db database.Store)

Source from the content-addressed store, hash-verified

55
56// ExtractUserParamOptional does not fail if no user is present.
57func ExtractUserParamOptional(db database.Store) func(http.Handler) http.Handler {
58 return func(next http.Handler) http.Handler {
59 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
60 ctx := r.Context()
61
62 user, ok := ExtractUserContext(ctx, db, &httpapi.NoopResponseWriter{}, r)
63 if ok {
64 ctx = context.WithValue(ctx, userParamContextKey{}, user)
65 }
66
67 next.ServeHTTP(rw, r.WithContext(ctx))
68 })
69 }
70}
71
72// ExtractUserContext queries the database for the parameterized `{user}` from the request URL.
73func ExtractUserContext(ctx context.Context, db database.Store, rw http.ResponseWriter, r *http.Request) (user database.User, ok bool) {

Callers

nothing calls this directly

Calls 4

ExtractUserContextFunction · 0.85
WithContextMethod · 0.80
ContextMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected