2022-03-07 17:40:54 +00:00
|
|
|
package httpmw
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2022-03-07 17:40:54 +00:00
|
|
|
)
|
|
|
|
|
2023-07-19 16:11:11 +00:00
|
|
|
// ParseUUIDParam consumes a url parameter and parses it as a UUID.
|
|
|
|
func ParseUUIDParam(rw http.ResponseWriter, r *http.Request, param string) (uuid.UUID, bool) {
|
2022-03-07 17:40:54 +00:00
|
|
|
rawID := chi.URLParam(r, param)
|
|
|
|
if rawID == "" {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Missing UUID in URL.",
|
2022-06-03 21:48:09 +00:00
|
|
|
// Url params mean nothing to a user
|
|
|
|
Detail: fmt.Sprintf("%q URL param missing", param),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return uuid.UUID{}, false
|
|
|
|
}
|
2022-04-01 19:42:36 +00:00
|
|
|
|
2022-03-07 17:40:54 +00:00
|
|
|
parsed, err := uuid.Parse(rawID)
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(r.Context(), rw, http.StatusBadRequest, codersdk.Response{
|
2023-04-27 17:04:24 +00:00
|
|
|
Message: fmt.Sprintf("Invalid UUID %q.", rawID),
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return uuid.UUID{}, false
|
|
|
|
}
|
2022-04-01 19:42:36 +00:00
|
|
|
|
2022-03-07 17:40:54 +00:00
|
|
|
return parsed, true
|
|
|
|
}
|