@@ -17,22 +17,26 @@ import { initializeWebSocket } from './services/websocketService';
1717
1818const app = express ( ) ;
1919const secret = await getSecretKeys ( ) ; // Load secrets from Google Secret Manager
20- const port = secret . PORT ;
20+ // Use Cloud Run's PORT env var if available, otherwise fall back to secret or default
21+ const port = process . env . PORT || secret . PORT || 3000 ;
2122const server = createServer ( app ) ;
2223
2324// Initialize WebSocket
2425initializeWebSocket ( server ) ;
2526
2627// ------------------------------------------------------------------------------------------------
27- // * K8s EVENT WATCHER & PROCESSOR
28+ // * K8s EVENT WATCHER & PROCESSOR (Started after server is running)
2829// ------------------------------------------------------------------------------------------------
29- startK8sEventWatcher ( ) . catch ( ( err ) =>
30- console . error ( chalk . redBright ( 'Failed to start K8s watcher:' , err ) ) ,
31- ) ; // Start Kubernetes event watcher
32-
33- startK8sEventProcessor ( ) . catch ( ( err ) =>
34- console . error ( chalk . redBright ( 'Failed to start K8s processor:' , err ) ) ,
35- ) ; // Start Kubernetes event processor
30+ // Start these services asynchronously after server starts to prevent blocking
31+ setTimeout ( ( ) => {
32+ startK8sEventWatcher ( ) . catch ( ( err ) =>
33+ console . error ( chalk . redBright ( 'Failed to start K8s watcher:' , err ) ) ,
34+ ) ; // Start Kubernetes event watcher
35+
36+ startK8sEventProcessor ( ) . catch ( ( err ) =>
37+ console . error ( chalk . redBright ( 'Failed to start K8s processor:' , err ) ) ,
38+ ) ; // Start Kubernetes event processor
39+ } , 2000 ) ; // Wait 2 seconds after server starts
3640
3741// ------------------------------------------------------------------------------------------------
3842// * MIDDLEWARE
@@ -49,6 +53,15 @@ app.get('/status', (req, res) => {
4953 res . status ( 200 ) . json ( { status : 'online' , message : 'Server is online!' } ) ;
5054} ) ;
5155
56+ // Health check endpoints for Cloud Run
57+ app . get ( '/health' , ( req , res ) => {
58+ res . status ( 200 ) . json ( { status : 'healthy' , timestamp : new Date ( ) . toISOString ( ) } ) ;
59+ } ) ;
60+
61+ app . get ( '/ready' , ( req , res ) => {
62+ res . status ( 200 ) . json ( { status : 'ready' , timestamp : new Date ( ) . toISOString ( ) } ) ;
63+ } ) ;
64+
5265app . use ( express . json ( ) ) ; // EXPRESS
5366app . use ( '/api' , routes ) ; // ROUTES
5467app . use ( '/api/gke' , clusterRouter ) ;
0 commit comments