11// Application routes for managing user applications, deployments, logs, and metrics
2- import { Router } from "express" ;
2+ import { Router , Request , Response , NextFunction } from "express" ;
33import ensureAuthenticated from "../utils/auth/ensureAuthenticated" ;
44import asyncHandler from "../utils/handlers/asyncHandler" ;
55
@@ -19,6 +19,16 @@ import scaleDeploymentZero from "../controllers/application/scaleDeploymentZero.
1919import rolloutRestartDeployment from "../controllers/application/rolloutRestartDeployment.controller" ;
2020import testImageAvailabilityController from "../controllers/application/testImageAvailability.controller" ;
2121
22+ const validateObjectId = ( req : Request , res : Response , next : NextFunction ) => {
23+ const id = req . params . id ;
24+
25+ if ( ! / ^ [ 0 - 9 a - f A - F ] { 24 } $ / . test ( id ) ) {
26+ res . status ( 400 ) . json ( { message : "Invalid application ID format" } ) ;
27+ return ;
28+ }
29+ next ( ) ;
30+ } ;
31+
2232const router = Router ( ) ;
2333
2434// All routes require authentication
@@ -28,32 +38,31 @@ router.use(ensureAuthenticated);
2838router . post ( "/" , createApplication ) ;
2939// Get all applications for the user
3040router . get ( "/" , getApplications ) ;
31- // Get a specific application by ID
32- router . get ( "/:id" , getApplication ) ;
41+ // Get live status for all applications - must be before /:id route
42+ router . get ( "/status" , getApplicationsStatus ) ;
43+ // Get a specific application by ID (with ObjectId validation)
44+ router . get ( "/:id" , validateObjectId , getApplication ) ;
3345// Update an application by ID
34- router . put ( "/:id" , updateApplication ) ;
46+ router . put ( "/:id" , validateObjectId , updateApplication ) ;
3547// Delete an application by ID
36- router . delete ( "/:id" , deleteApplication ) ;
48+ router . delete ( "/:id" , validateObjectId , deleteApplication ) ;
3749// Apply (deploy) an application
38- router . post ( "/:id/apply" , applyApplication ) ;
50+ router . post ( "/:id/apply" , validateObjectId , applyApplication ) ;
3951// Remove a deployment for an application
40- router . post ( "/:id/remove-deployment" , removeDeployment ) ;
52+ router . post ( "/:id/remove-deployment" , validateObjectId , removeDeployment ) ;
4153// Scale deployment to 0 replicas
42- router . post ( "/:id/scale-zero" , scaleDeploymentZero ) ;
54+ router . post ( "/:id/scale-zero" , validateObjectId , scaleDeploymentZero ) ;
4355// Rollout restart deployment
44- router . post ( "/:id/rollout-restart" , rolloutRestartDeployment ) ;
56+ router . post ( "/:id/rollout-restart" , validateObjectId , rolloutRestartDeployment ) ;
4557// Remove a namespace for an application
46- router . post ( "/:id/remove-namespace" , removeNamespace ) ;
58+ router . post ( "/:id/remove-namespace" , validateObjectId , removeNamespace ) ;
4759// Stream logs for an application
48- router . get ( "/:id/logs" , streamApplicationLogs ) ;
60+ router . get ( "/:id/logs" , validateObjectId , streamApplicationLogs ) ;
4961// Get metrics for an application
50- router . get ( "/:id/metrics" , getApplicationMetrics ) ;
62+ router . get ( "/:id/metrics" , validateObjectId , getApplicationMetrics ) ;
5163// Test Docker image availability
5264router . post ( "/test-image" , testImageAvailabilityController ) ;
5365// Run a Docker handler (async)
5466router . post ( "/run-docker" , asyncHandler ( runDockerHandler ) ) ;
5567
56- // Get live status for all applications
57- router . get ( "/status" , getApplicationsStatus ) ;
58-
5968export default router ;
0 commit comments