creating stacks using a user request

This commit is contained in:
2025-08-20 21:38:55 +01:00
parent f5e65524aa
commit 10cea769bf
23 changed files with 598 additions and 76 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"github.com/charmbracelet/log"
"github.com/google/uuid"
)
@@ -15,7 +16,7 @@ func CorsMiddleware(next http.Handler) http.Handler {
w.Header().Add("Access-Control-Allow-Headers", "*")
// Access-Control-Allow-Methods is often needed for preflight OPTIONS requests
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
// The client makes an OPTIONS preflight request before a complex request.
// We must handle this and respond with the appropriate headers.
@@ -30,15 +31,19 @@ func CorsMiddleware(next http.Handler) http.Handler {
const USER_ID = "UserID"
func GetUserID(ctx context.Context) (uuid.UUID, error) {
func GetUserID(ctx context.Context, logger *log.Logger, w http.ResponseWriter) (uuid.UUID, error) {
userId := ctx.Value(USER_ID)
if userId == nil {
w.WriteHeader(http.StatusUnauthorized)
logger.Warn("UserID not present in request")
return uuid.Nil, errors.New("context does not contain a user id")
}
userIdUuid, ok := userId.(uuid.UUID)
if !ok {
w.WriteHeader(http.StatusUnauthorized)
logger.Warn("UserID not of correct type")
return uuid.Nil, fmt.Errorf("context user id is not of type uuid, got: %t", userId)
}
@@ -87,3 +92,25 @@ func GetUserIdFromUrl(next http.Handler) http.Handler {
next.ServeHTTP(w, newR)
})
}
func GetPathParamID(logger *log.Logger, param string, w http.ResponseWriter, r *http.Request) (uuid.UUID, error) {
pathParam := r.PathValue(param)
if len(pathParam) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := fmt.Errorf("%s was not present", param)
logger.Warn(err)
return uuid.Nil, err
}
uuidParam, err := uuid.Parse(pathParam)
if len(pathParam) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := fmt.Errorf("could not parse param: %w", err)
logger.Warn(err)
return uuid.Nil, err
}
return uuidParam, nil
}