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

Function ExtractTemplateParam

coderd/httpmw/templateparam.go:26–52  ·  view source on GitHub ↗

ExtractTemplateParam grabs a template from the "template" URL parameter.

(db database.Store)

Source from the content-addressed store, hash-verified

24
25// ExtractTemplateParam grabs a template from the "template" URL parameter.
26func ExtractTemplateParam(db database.Store) func(http.Handler) http.Handler {
27 return func(next http.Handler) http.Handler {
28 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
29 ctx := r.Context()
30 templateID, parsed := ParseUUIDParam(rw, r, "template")
31 if !parsed {
32 return
33 }
34 template, err := db.GetTemplateByID(r.Context(), templateID)
35 if httpapi.Is404Error(err) || (err == nil && template.Deleted) {
36 httpapi.ResourceNotFound(rw)
37 return
38 }
39 if err != nil {
40 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
41 Message: "Internal error fetching template.",
42 Detail: err.Error(),
43 })
44 return
45 }
46
47 ctx = context.WithValue(ctx, templateParamContextKey{}, template)
48 chi.RouteContext(ctx).URLParams.Add("organization", template.OrganizationID.String())
49 next.ServeHTTP(rw, r.WithContext(ctx))
50 })
51 }
52}

Callers 2

TestTemplateParamFunction · 0.92
NewFunction · 0.92

Calls 11

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
StringMethod · 0.45
ServeHTTPMethod · 0.45

Tested by 1

TestTemplateParamFunction · 0.74