ExtractTemplateVersionParam grabs template version from the "templateversion" URL parameter.
(db database.Store)
| 26 | |
| 27 | // ExtractTemplateVersionParam grabs template version from the "templateversion" URL parameter. |
| 28 | func ExtractTemplateVersionParam(db database.Store) func(http.Handler) http.Handler { |
| 29 | return func(next http.Handler) http.Handler { |
| 30 | return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 31 | ctx := r.Context() |
| 32 | templateVersionID, parsed := ParseUUIDParam(rw, r, "templateversion") |
| 33 | if !parsed { |
| 34 | return |
| 35 | } |
| 36 | templateVersion, err := db.GetTemplateVersionByID(ctx, templateVersionID) |
| 37 | if httpapi.Is404Error(err) { |
| 38 | httpapi.ResourceNotFound(rw) |
| 39 | return |
| 40 | } |
| 41 | if err != nil { |
| 42 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 43 | Message: "Internal error fetching template version.", |
| 44 | Detail: err.Error(), |
| 45 | }) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | template, err := db.GetTemplateByID(r.Context(), templateVersion.TemplateID.UUID) |
| 50 | if err != nil && !xerrors.Is(err, sql.ErrNoRows) { |
| 51 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 52 | Message: "Internal error fetching template.", |
| 53 | Detail: err.Error(), |
| 54 | }) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | ctx = context.WithValue(ctx, templateVersionParamContextKey{}, templateVersion) |
| 59 | chi.RouteContext(ctx).URLParams.Add("organization", templateVersion.OrganizationID.String()) |
| 60 | |
| 61 | ctx = context.WithValue(ctx, templateParamContextKey{}, template) |
| 62 | |
| 63 | next.ServeHTTP(rw, r.WithContext(ctx)) |
| 64 | }) |
| 65 | } |
| 66 | } |