diff --git a/code/websites/pokedex.online/server/oauth-proxy.js b/code/websites/pokedex.online/server/oauth-proxy.js index 65922dd..3603a89 100644 --- a/code/websites/pokedex.online/server/oauth-proxy.js +++ b/code/websites/pokedex.online/server/oauth-proxy.js @@ -97,7 +97,8 @@ app.post('/oauth/token', async (req, res) => { * POST /oauth/refresh */ app.post('/oauth/refresh', async (req, res) => { - if (!hasChallongeAuth) { + if (!config.challonge.configured) { + logger.warn('OAuth refresh request received but Challonge not configured'); return res.status(503).json({ error: 'Challonge OAuth not configured', message: @@ -108,10 +109,12 @@ app.post('/oauth/refresh', async (req, res) => { const { refresh_token } = req.body; if (!refresh_token) { + logger.warn('OAuth refresh request missing refresh token'); return res.status(400).json({ error: 'Missing refresh token' }); } try { + logger.debug('Refreshing access token'); const response = await fetch('https://api.challonge.com/oauth/token', { method: 'POST', headers: { @@ -119,8 +122,8 @@ app.post('/oauth/refresh', async (req, res) => { }, body: new URLSearchParams({ grant_type: 'refresh_token', - client_id: CLIENT_ID, - client_secret: CLIENT_SECRET, + client_id: config.challonge.clientId, + client_secret: config.challonge.clientSecret, refresh_token: refresh_token }) }); @@ -128,14 +131,14 @@ app.post('/oauth/refresh', async (req, res) => { const data = await response.json(); if (!response.ok) { - console.error('Token refresh failed:', data); + logger.error('Token refresh failed', { status: response.status, data }); return res.status(response.status).json(data); } - console.log('āœ… Token refresh successful'); + logger.info('Token refresh successful'); res.json(data); } catch (error) { - console.error('Token refresh error:', error); + logger.error('Token refresh error', { error: error.message }); res.status(500).json({ error: 'Token refresh failed', message: error.message @@ -144,20 +147,35 @@ app.post('/oauth/refresh', async (req, res) => { }); /** - * Health check endpoint + * Health check endpoint (with graceful shutdown support) * GET /health */ -app.get('/health', (req, res) => { - res.json({ - status: 'ok', - service: 'oauth-proxy', - configured: !!(CLIENT_ID && CLIENT_SECRET) +app.get('/health', createHealthCheckMiddleware()); + +// Error logging middleware (must be after routes) +app.use(errorLogger); + +// Start server +const server = app.listen(config.port, () => { + logger.info('šŸ” OAuth Proxy Server started', { + port: config.port, + nodeEnv: config.nodeEnv, + challongeConfigured: config.challonge.configured }); + + if (!config.challonge.configured) { + logger.warn('āš ļø Challonge OAuth not configured - OAuth endpoints disabled'); + logger.warn(' Set CHALLONGE_CLIENT_ID and CHALLONGE_CLIENT_SECRET to enable'); + } + + logger.info('āœ… Ready to handle requests'); }); -app.listen(PORT, () => { - console.log(`šŸ” OAuth Proxy Server running on http://localhost:${PORT}`); - console.log(`šŸ“ Client ID: ${CLIENT_ID}`); - console.log(`šŸ”— Redirect URI: ${REDIRECT_URI}`); - console.log('\nāœ… Ready to handle OAuth requests'); +// Setup graceful shutdown +setupGracefulShutdown(server, { + timeout: 30000, + onShutdown: async () => { + logger.info('Running cleanup tasks...'); + // Add any cleanup tasks here (close DB connections, etc.) + } });