65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import helmet from 'helmet';
|
|
import dotenv from 'dotenv';
|
|
import rateLimit from 'express-rate-limit';
|
|
import authRoutes from './routes/auth';
|
|
import userRoutes from './routes/user';
|
|
import { errorHandler } from './middleware/errorHandler';
|
|
import { logger } from './utils/logger';
|
|
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// Security middleware
|
|
app.use(helmet());
|
|
app.use(cors({
|
|
origin: process.env.FRONTEND_URL || 'http://localhost:9002',
|
|
credentials: true
|
|
}));
|
|
|
|
// Rate limiting
|
|
const limiter = rateLimit({
|
|
windowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS || '900000'), // 15 minutes
|
|
max: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS || '100')
|
|
});
|
|
app.use('/api/', limiter);
|
|
|
|
// Body parsing
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Health check
|
|
app.get('/health', (req, res) => {
|
|
res.json({
|
|
status: 'healthy',
|
|
service: 'aethex-auth-service',
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
});
|
|
|
|
// Routes
|
|
app.use('/api/v1/auth', authRoutes);
|
|
app.use('/api/v1/users', userRoutes);
|
|
|
|
// Error handling
|
|
app.use(errorHandler);
|
|
|
|
// 404 handler
|
|
app.use((req, res) => {
|
|
res.status(404).json({
|
|
error: 'Not Found',
|
|
message: `Route ${req.method} ${req.path} not found`
|
|
});
|
|
});
|
|
|
|
// Start server
|
|
app.listen(PORT, () => {
|
|
logger.info(`🚀 AeThex Auth Service running on port ${PORT}`);
|
|
logger.info(`📝 Environment: ${process.env.NODE_ENV}`);
|
|
logger.info(`🔗 Health check: http://localhost:${PORT}/health`);
|
|
});
|
|
|
|
export default app;
|