🛠️ Refactor OAuth proxy server to improve configuration validation, logging, and middleware setup
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
*
|
*
|
||||||
* Usage:
|
* Usage:
|
||||||
* Development: node server/oauth-proxy.js
|
* Development: node server/oauth-proxy.js
|
||||||
* Production: Deploy as serverless function or Express app
|
* Production: Deploy with Docker (see docker-compose.production.yml)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import 'dotenv/config';
|
import 'dotenv/config';
|
||||||
@@ -14,39 +14,24 @@ import express from 'express';
|
|||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import gamemasterRouter from './gamemaster-api.js';
|
import gamemasterRouter from './gamemaster-api.js';
|
||||||
|
import { validateOrExit, getConfig } from './utils/env-validator.js';
|
||||||
|
import logger, { requestLogger, errorLogger } from './utils/logger.js';
|
||||||
|
import { setupGracefulShutdown, createHealthCheckMiddleware } from './utils/graceful-shutdown.js';
|
||||||
|
|
||||||
|
// Validate environment variables
|
||||||
|
validateOrExit();
|
||||||
|
|
||||||
|
// Get validated configuration
|
||||||
|
const config = getConfig();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.OAUTH_PROXY_PORT || 3001;
|
|
||||||
|
|
||||||
// Environment variables (set in .env file)
|
// Middleware
|
||||||
const CLIENT_ID = process.env.CHALLONGE_CLIENT_ID;
|
app.use(cors({ origin: config.cors.origin }));
|
||||||
const CLIENT_SECRET = process.env.CHALLONGE_CLIENT_SECRET;
|
|
||||||
const REDIRECT_URI =
|
|
||||||
process.env.CHALLONGE_REDIRECT_URI || 'http://localhost:5173/oauth/callback';
|
|
||||||
|
|
||||||
// Validate required environment variables
|
|
||||||
const hasChallongeAuth = CLIENT_ID && CLIENT_SECRET;
|
|
||||||
if (!hasChallongeAuth) {
|
|
||||||
console.warn('⚠️ Challonge OAuth credentials not configured:');
|
|
||||||
console.warn(' CHALLONGE_CLIENT_ID');
|
|
||||||
console.warn(' CHALLONGE_CLIENT_SECRET');
|
|
||||||
console.warn('\nChallonge OAuth endpoints will be disabled.');
|
|
||||||
console.warn('Gamemaster API will still work.\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
app.use(
|
|
||||||
cors({
|
|
||||||
origin:
|
|
||||||
process.env.NODE_ENV === 'production'
|
|
||||||
? process.env.FRONTEND_URL
|
|
||||||
: [
|
|
||||||
'http://localhost:5173',
|
|
||||||
'http://localhost:5174',
|
|
||||||
'http://localhost:5175'
|
|
||||||
]
|
|
||||||
})
|
|
||||||
);
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
app.use(requestLogger);
|
||||||
|
|
||||||
|
// Mount API routes
|
||||||
app.use('/api/gamemaster', gamemasterRouter);
|
app.use('/api/gamemaster', gamemasterRouter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user