ExtraGroupParam grabs a group from the "group" URL parameter.
(db database.Store)
| 60 | |
| 61 | // ExtraGroupParam grabs a group from the "group" URL parameter. |
| 62 | func ExtractGroupParam(db database.Store) func(http.Handler) http.Handler { |
| 63 | return func(next http.Handler) http.Handler { |
| 64 | return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 65 | ctx := r.Context() |
| 66 | |
| 67 | groupID, parsed := ParseUUIDParam(rw, r, "group") |
| 68 | if !parsed { |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | group, err := db.GetGroupByID(r.Context(), groupID) |
| 73 | if httpapi.Is404Error(err) { |
| 74 | httpapi.ResourceNotFound(rw) |
| 75 | return |
| 76 | } |
| 77 | if err != nil { |
| 78 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 79 | Message: "Internal error fetching group.", |
| 80 | Detail: err.Error(), |
| 81 | }) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | ctx = context.WithValue(ctx, groupParamContextKey{}, group) |
| 86 | chi.RouteContext(ctx).URLParams.Add("organization", group.OrganizationID.String()) |
| 87 | next.ServeHTTP(rw, r.WithContext(ctx)) |
| 88 | }) |
| 89 | } |
| 90 | } |