Files
soroush.asadi 81912cac66
Build backend images / build content-svc (push) Failing after 14s
Build backend images / build file-svc (push) Failing after 1m28s
Build backend images / build gateway (push) Failing after 1m43s
Build backend images / build identity-svc (push) Failing after 3m0s
Build backend images / build notification-svc (push) Failing after 51s
Build backend images / build render-svc (push) Failing after 1m3s
Build backend images / build studio-svc (push) Failing after 1m1s
feat(render): full-screen render page, one-active-render limit, app-wide progress
Concurrent-render ceiling (a user runs 1 render at a time unless granted more):
- Identity: TokenService emits max_renders claim from User.ParallelRenderingCeiling
- Identity: admin POST /v1/users/{id}/render-slots (AdminService.SetRenderSlotsAsync,
  clamped 1..50) — gamification or admin raises a user's ceiling
- render-svc: middleware reads max_renders (default 1); CreateJob rejects with 409
  active_render_limit when active jobs >= ceiling
- render-svc: db.CountActiveJobs + ListActiveJobs; GET /v1/renders/active returns
  in-flight renders + can_start_new

Full-screen render page (replaces the modal):
- /studio/render/[projectId]: config (resolution/fps) → live preview + progress →
  download; resumes this project's in-flight render on mount; blocks when another
  render is active; reads ?preset=
- StudioTopBar export menu now navigates to the page; RenderModal deleted (dead)

App-wide minimal progress:
- GlobalRenderProgress pill mounted in the locale layout for authed users; polls
  /api/render/active every 4s, shows thumbnail + step + % on every page, click →
  the render page; hidden on the render page and when idle

Admin: UserActions gains a "concurrent render slots" control.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 16:48:05 +03:30

144 lines
3.7 KiB
Go

package middleware
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/flatrender/render-svc/internal/models"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
)
const (
CtxUserID = "user_id"
CtxTenantID = "tenant_id"
CtxIsAdmin = "is_admin"
CtxRole = "role"
CtxMaxRenders = "max_renders"
)
func JWTAuth(secret string) gin.HandlerFunc {
return func(c *gin.Context) {
hdr := c.GetHeader("Authorization")
if !strings.HasPrefix(hdr, "Bearer ") {
c.AbortWithStatusJSON(http.StatusUnauthorized, models.APIError{Code: "unauthorized", Message: "missing bearer token"})
return
}
tokenStr := hdr[7:]
token, err := jwt.Parse(tokenStr, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
}
return []byte(secret), nil
})
if err != nil || !token.Valid {
c.AbortWithStatusJSON(http.StatusUnauthorized, models.APIError{Code: "unauthorized", Message: "invalid token"})
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, models.APIError{Code: "unauthorized", Message: "bad claims"})
return
}
userID, _ := uuid.Parse(fmt.Sprintf("%v", claims["sub"]))
tenantID, _ := uuid.Parse(fmt.Sprintf("%v", claims["tenant_id"]))
// is_admin may arrive as a JSON bool or as the string "true" (identity emits a
// string). Accept both so [RequireAdmin] works regardless of token encoding.
isAdmin := false
switch v := claims["is_admin"].(type) {
case bool:
isAdmin = v
case string:
isAdmin = v == "true"
}
role, _ := claims["role"].(string)
// max_renders: concurrent-render ceiling. Identity emits it as a string;
// also accept a JSON number. Default 1 when absent/unparseable.
maxRenders := 1
switch v := claims["max_renders"].(type) {
case string:
if n, err := strconv.Atoi(v); err == nil && n > 0 {
maxRenders = n
}
case float64:
if v >= 1 {
maxRenders = int(v)
}
}
c.Set(CtxUserID, userID)
c.Set(CtxTenantID, tenantID)
c.Set(CtxIsAdmin, isAdmin)
c.Set(CtxRole, role)
c.Set(CtxMaxRenders, maxRenders)
c.Next()
}
}
func RequireAdmin() gin.HandlerFunc {
return func(c *gin.Context) {
isAdmin, _ := c.Get(CtxIsAdmin)
b, _ := isAdmin.(bool)
if !b {
c.AbortWithStatusJSON(http.StatusForbidden, models.APIError{Code: "forbidden", Message: "admin required"})
return
}
c.Next()
}
}
// RequireServiceRole allows callers presenting a token with role="Service"
func RequireServiceRole() gin.HandlerFunc {
return func(c *gin.Context) {
role, _ := c.Get(CtxRole)
isAdmin, _ := c.Get(CtxIsAdmin)
b, _ := isAdmin.(bool)
if role != "Service" && !b {
c.AbortWithStatusJSON(http.StatusForbidden, models.APIError{Code: "forbidden", Message: "service role required"})
return
}
c.Next()
}
}
// NodeHMAC verifies the X-Node-Signature header for node-agent calls
func NodeHMAC(nodeSecret string) gin.HandlerFunc {
return func(c *gin.Context) {
sig := c.GetHeader("X-Node-Signature")
if sig == "" || sig != nodeSecret {
c.AbortWithStatusJSON(http.StatusUnauthorized, models.APIError{Code: "unauthorized", Message: "invalid node signature"})
return
}
c.Next()
}
}
func GetUserID(c *gin.Context) uuid.UUID {
v, _ := c.Get(CtxUserID)
id, _ := v.(uuid.UUID)
return id
}
func GetTenantID(c *gin.Context) uuid.UUID {
v, _ := c.Get(CtxTenantID)
id, _ := v.(uuid.UUID)
return id
}
// GetMaxRenders returns the user's concurrent-render ceiling (default 1).
func GetMaxRenders(c *gin.Context) int {
v, ok := c.Get(CtxMaxRenders)
if !ok {
return 1
}
n, _ := v.(int)
if n < 1 {
return 1
}
return n
}