mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
chore: Rename Projects to Templates (#880)
Customer feedback indicated projects was a confusing name.
After querying the team internally, it seemed unanimous
that it is indeed a confusing name.
Here's for a lil less confusion @ashmeer7 🥂
This commit is contained in:
53
coderd/httpmw/templateparam.go
Normal file
53
coderd/httpmw/templateparam.go
Normal file
@ -0,0 +1,53 @@
|
||||
package httpmw
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/coder/coder/coderd/database"
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
)
|
||||
|
||||
type templateParamContextKey struct{}
|
||||
|
||||
// TemplateParam returns the template from the ExtractTemplateParam handler.
|
||||
func TemplateParam(r *http.Request) database.Template {
|
||||
template, ok := r.Context().Value(templateParamContextKey{}).(database.Template)
|
||||
if !ok {
|
||||
panic("developer error: template param middleware not provided")
|
||||
}
|
||||
return template
|
||||
}
|
||||
|
||||
// ExtractTemplateParam grabs a template from the "template" URL parameter.
|
||||
func ExtractTemplateParam(db database.Store) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
templateID, parsed := parseUUID(rw, r, "template")
|
||||
if !parsed {
|
||||
return
|
||||
}
|
||||
template, err := db.GetTemplateByID(r.Context(), templateID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
|
||||
Message: fmt.Sprintf("template %q does not exist", templateID),
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
||||
Message: fmt.Sprintf("get template: %s", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), templateParamContextKey{}, template)
|
||||
chi.RouteContext(ctx).URLParams.Add("organization", template.OrganizationID.String())
|
||||
next.ServeHTTP(rw, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user