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

Function ExtractTemplateVersionParam

coderd/httpmw/templateversionparam.go:28–66  ·  view source on GitHub ↗

ExtractTemplateVersionParam grabs template version from the "templateversion" URL parameter.

(db database.Store)

Source from the content-addressed store, hash-verified

26
27// ExtractTemplateVersionParam grabs template version from the "templateversion" URL parameter.
28func 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}

Callers 1

TestTemplateVersionParamFunction · 0.92

Calls 13

Is404ErrorFunction · 0.92
ResourceNotFoundFunction · 0.92
WriteFunction · 0.92
ParseUUIDParamFunction · 0.85
WithContextMethod · 0.80
ContextMethod · 0.65
GetTemplateByIDMethod · 0.65
AddMethod · 0.65
ErrorMethod · 0.45
IsMethod · 0.45
StringMethod · 0.45

Tested by 1

TestTemplateVersionParamFunction · 0.74