Spaces:
Paused
Paused
| /** | |
| * Hugging Face Spaces Entrypoint | |
| * Ensures proper port binding and environment validation for HF Spaces deployment | |
| */ | |
| const {spawn} = require('child_process'); | |
| const http = require('http'); | |
| const PORT = process.env.PORT || 7860; | |
| const HOST = process.env.HOST || '0.0.0.0'; | |
| function validateEnvironment() { | |
| const required = ['APP_ID', 'WEBHOOK_SECRET', 'PRIVATE_KEY']; | |
| const missing = required.filter(key => !process.env[key]); | |
| if (missing.length > 0) { | |
| console.warn(`WARNING: Missing environment variables: ${missing.join(', ')}`); | |
| console.warn('The application may not function correctly without these.'); | |
| return false; | |
| } | |
| return true; | |
| } | |
| function checkServerHealth() { | |
| return new Promise((resolve) => { | |
| const req = http.request( | |
| { | |
| hostname: HOST, | |
| port: PORT, | |
| path: '/ping', | |
| method: 'GET', | |
| timeout: 5000 | |
| }, | |
| (res) => { | |
| resolve(res.statusCode === 200); | |
| } | |
| ); | |
| req.on('error', () => resolve(false)); | |
| req.on('timeout', () => { | |
| req.destroy(); | |
| resolve(false); | |
| }); | |
| req.end(); | |
| }); | |
| } | |
| async function startServer() { | |
| console.log(`PRIX starting on ${HOST}:${PORT}...`); | |
| const isValid = validateEnvironment(); | |
| if (!isValid) { | |
| console.warn('Proceeding with missing environment variables...'); | |
| } | |
| const server = spawn('npm', ['start'], { | |
| stdio: 'inherit', | |
| env: {...process.env, PORT, HOST} | |
| }); | |
| server.on('error', (err) => { | |
| console.error('Failed to start server:', err); | |
| process.exit(1); | |
| }); | |
| let healthCheckCount = 0; | |
| const maxHealthChecks = 12; | |
| const healthInterval = setInterval(async () => { | |
| const isHealthy = await checkServerHealth(); | |
| if (isHealthy) { | |
| console.log('PRIX server is healthy and ready!'); | |
| clearInterval(healthInterval); | |
| } else { | |
| healthCheckCount++; | |
| if (healthCheckCount >= maxHealthChecks) { | |
| console.warn('Server health check timeout, but continuing...'); | |
| clearInterval(healthInterval); | |
| } | |
| } | |
| }, 5000); | |
| process.on('SIGTERM', () => { | |
| console.log('Received SIGTERM, shutting down gracefully...'); | |
| clearInterval(healthInterval); | |
| server.kill('SIGTERM'); | |
| process.exit(0); | |
| }); | |
| process.on('SIGINT', () => { | |
| console.log('Received SIGINT, shutting down gracefully...'); | |
| clearInterval(healthInterval); | |
| server.kill('SIGINT'); | |
| process.exit(0); | |
| }); | |
| } | |
| startServer(); |