d-robotics 994fa59982 ♻️ Split API route registration into per-domain routes_*.go files
routes.go keeps only the global middleware chain, the shared business-role
sets, and one mount call per domain: system, dashboard, acceptance, human
loop, annotation, projects, organization, workflows, runs, delivery, data
platform, and object sync. Role-only groups become withRoles/With on the
routes they guard, so each route's permission sits next to it instead of in
a role-shaped group far from the rest of its domain.

No route, handler, or access change: testdata/routes.golden (generated from
the pre-split router) is byte-identical, the Go known-failure gate passes,
and a live API smoke against a throwaway DB matched the access matrix for
every parameter-free GET route across six identities.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 18:50:40 +08:00

62 lines
2.5 KiB
Go

package api
import (
"droboticflow/internal/domain"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
// Business-role sets shared by route registrations. A route's access is the
// global middleware chain plus whatever it adds through withRoles or With;
// testdata/routes.golden pins the result for every route and role.
var (
rolesAdmin = []domain.UserRole{domain.UserRoleAdmin}
// rolesReviewWork covers everyone who claims and works human-loop items.
rolesReviewWork = []domain.UserRole{domain.UserRoleAdmin, domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer}
// rolesAnnotationWork covers collection and annotation authoring.
rolesAnnotationWork = []domain.UserRole{domain.UserRoleAdmin, domain.UserRoleAnnotator}
// rolesQualityReview covers sampling review and audit decisions.
rolesQualityReview = []domain.UserRole{domain.UserRoleAdmin, domain.UserRoleQualityReviewer}
// rolesDeliveryReview covers delivery-package acceptance work.
rolesDeliveryReview = []domain.UserRole{domain.UserRoleAdmin, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer}
// rolesSupplierAdmin covers supplier, member and account management.
rolesSupplierAdmin = []domain.UserRole{domain.UserRoleAdmin, domain.UserRoleSupplierManager}
)
// routes builds the API router: global middlewares first, then each domain's
// routes from its routes_<domain>.go file.
func (server *server) routes() chi.Router {
router := chi.NewRouter()
router.Use(diagnosticTrace)
router.Use(middleware.Compress(5))
router.Use(server.invalidateDashboardCachesOnWrite)
router.Use(server.apiTokenAuth)
router.Use(server.sessionAuth)
router.Use(server.csrfProtection)
router.Use(server.projectDeletionWriteGuard)
router.Use(server.supplierDeletionWriteGuard)
router.Use(server.acceptanceAccessCache)
router.Use(annotationReadCache)
server.mountSystemRoutes(router)
server.mountDashboardRoutes(router)
server.mountAcceptanceRoutes(router)
server.mountHumanLoopRoutes(router)
server.mountAnnotationRoutes(router)
server.mountProjectRoutes(router)
server.mountOrganizationRoutes(router)
server.mountWorkflowRoutes(router)
server.mountRunRoutes(router)
server.mountDeliveryRoutes(router)
server.mountDataPlatformRoutes(router)
server.mountObjectSyncRoutes(router)
return router
}
// withRoles restricts the routes registered on the returned router to the
// given business roles.
func (server *server) withRoles(router chi.Router, roles ...domain.UserRole) chi.Router {
return router.With(server.requireBusinessRole(roles...))
}