From c82e9ea5ec5dc5f429d1450e32dc62739ee27894 Mon Sep 17 00:00:00 2001 From: FragginWagon Date: Thu, 29 Jan 2026 13:20:00 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=20Improve=20OAuth=20proxy=20with?= =?UTF-8?q?=20enhanced=20logging,=20configuration=20handling,=20health=20c?= =?UTF-8?q?heck=20middleware,=20and=20graceful=20shutdown=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pokedex.online/server/oauth-proxy.js | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) 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.) + } });