add changes
This commit is contained in:
113
backend/config/config.js
Normal file
113
backend/config/config.js
Normal file
@@ -0,0 +1,113 @@
|
||||
require('dotenv').config();
|
||||
|
||||
/**
|
||||
* Application Configuration
|
||||
* Centralized configuration management for all environment variables
|
||||
*/
|
||||
|
||||
const config = {
|
||||
// Server Configuration
|
||||
server: {
|
||||
nodeEnv: process.env.NODE_ENV || 'development',
|
||||
port: parseInt(process.env.PORT) || 3000,
|
||||
apiPrefix: process.env.API_PREFIX || '/api',
|
||||
isDevelopment: (process.env.NODE_ENV || 'development') === 'development',
|
||||
isProduction: process.env.NODE_ENV === 'production',
|
||||
isTest: process.env.NODE_ENV === 'test'
|
||||
},
|
||||
|
||||
// Database Configuration
|
||||
database: {
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: parseInt(process.env.DB_PORT) || 3306,
|
||||
name: process.env.DB_NAME || 'interview_quiz_db',
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || '',
|
||||
dialect: process.env.DB_DIALECT || 'mysql',
|
||||
pool: {
|
||||
max: parseInt(process.env.DB_POOL_MAX) || 10,
|
||||
min: parseInt(process.env.DB_POOL_MIN) || 0,
|
||||
acquire: parseInt(process.env.DB_POOL_ACQUIRE) || 30000,
|
||||
idle: parseInt(process.env.DB_POOL_IDLE) || 10000
|
||||
}
|
||||
},
|
||||
|
||||
// JWT Configuration
|
||||
jwt: {
|
||||
secret: process.env.JWT_SECRET,
|
||||
expire: process.env.JWT_EXPIRE || '24h',
|
||||
algorithm: 'HS256'
|
||||
},
|
||||
|
||||
// Rate Limiting Configuration
|
||||
rateLimit: {
|
||||
windowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS) || 900000, // 15 minutes
|
||||
maxRequests: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS) || 100,
|
||||
message: 'Too many requests from this IP, please try again later.'
|
||||
},
|
||||
|
||||
// CORS Configuration
|
||||
cors: {
|
||||
origin: process.env.CORS_ORIGIN || 'http://localhost:4200',
|
||||
credentials: true
|
||||
},
|
||||
|
||||
// Guest Session Configuration
|
||||
guest: {
|
||||
sessionExpireHours: parseInt(process.env.GUEST_SESSION_EXPIRE_HOURS) || 24,
|
||||
maxQuizzes: parseInt(process.env.GUEST_MAX_QUIZZES) || 3
|
||||
},
|
||||
|
||||
// Logging Configuration
|
||||
logging: {
|
||||
level: process.env.LOG_LEVEL || 'info'
|
||||
},
|
||||
|
||||
// Pagination Defaults
|
||||
pagination: {
|
||||
defaultLimit: 10,
|
||||
maxLimit: 100
|
||||
},
|
||||
|
||||
// Security Configuration
|
||||
security: {
|
||||
bcryptRounds: 10,
|
||||
maxLoginAttempts: 5,
|
||||
lockoutDuration: 15 * 60 * 1000 // 15 minutes
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate critical configuration values
|
||||
*/
|
||||
function validateConfig() {
|
||||
const errors = [];
|
||||
|
||||
if (!config.jwt.secret) {
|
||||
errors.push('JWT_SECRET is not configured');
|
||||
}
|
||||
|
||||
if (!config.database.name) {
|
||||
errors.push('DB_NAME is not configured');
|
||||
}
|
||||
|
||||
if (config.server.isProduction && !config.database.password) {
|
||||
errors.push('DB_PASSWORD is required in production');
|
||||
}
|
||||
|
||||
if (errors.length > 0) {
|
||||
throw new Error(`Configuration errors:\n - ${errors.join('\n - ')}`);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Validate on module load
|
||||
try {
|
||||
validateConfig();
|
||||
} catch (error) {
|
||||
console.error('❌ Configuration Error:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
module.exports = config;
|
||||
Reference in New Issue
Block a user