|
import express from 'express';
|
|
import cors from 'cors';
|
|
import helmet from 'helmet';
|
|
import rateLimit from 'express-rate-limit';
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import authRoutes from './routes/auth.js';
|
|
import pptRoutes from './routes/ppt.js';
|
|
import publicRoutes from './routes/public.js';
|
|
import { authenticateToken } from './middleware/auth.js';
|
|
import { errorHandler } from './middleware/errorHandler.js';
|
|
|
|
dotenv.config();
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 7860;
|
|
|
|
|
|
app.use(helmet({
|
|
contentSecurityPolicy: false,
|
|
}));
|
|
|
|
|
|
const limiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000,
|
|
max: 100,
|
|
message: 'Too many requests from this IP, please try again later.'
|
|
});
|
|
app.use('/api', limiter);
|
|
|
|
|
|
app.use(cors({
|
|
origin: process.env.FRONTEND_URL || '*',
|
|
credentials: true
|
|
}));
|
|
|
|
app.use(express.json({ limit: '50mb' }));
|
|
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, '../../frontend/dist')));
|
|
|
|
|
|
app.use('/data', express.static(path.join(__dirname, '../../frontend/public/mocks')));
|
|
|
|
|
|
console.log('Registering API routes...');
|
|
|
|
|
|
app.get('/api/health', (req, res) => {
|
|
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
|
});
|
|
|
|
|
|
app.get('/api/github/status', async (req, res) => {
|
|
try {
|
|
const { default: githubService } = await import('./services/githubService.js');
|
|
const validation = await githubService.validateConnection();
|
|
res.json({
|
|
github: validation,
|
|
environment: {
|
|
tokenConfigured: !!process.env.GITHUB_TOKEN,
|
|
reposConfigured: !!process.env.GITHUB_REPOS,
|
|
nodeEnv: process.env.NODE_ENV
|
|
}
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
|
|
console.log('Importing route modules...');
|
|
console.log('Auth routes imported:', !!authRoutes);
|
|
console.log('PPT routes imported:', !!pptRoutes);
|
|
console.log('Public routes imported:', !!publicRoutes);
|
|
|
|
|
|
app.use('/api/auth', authRoutes);
|
|
|
|
|
|
app.use('/api/public', publicRoutes);
|
|
|
|
|
|
app.get('/api/ppt/test', (req, res) => {
|
|
res.json({ message: 'PPT routes are working', timestamp: new Date().toISOString() });
|
|
});
|
|
|
|
|
|
app.use('/api/ppt', (req, res, next) => {
|
|
console.log(`PPT route accessed: ${req.method} ${req.path}`);
|
|
next();
|
|
}, authenticateToken, pptRoutes);
|
|
|
|
console.log('All routes registered successfully');
|
|
|
|
|
|
app.use('/api/*', (req, res) => {
|
|
console.log(`Unmatched API route: ${req.method} ${req.path}`);
|
|
res.status(404).json({ error: 'API route not found', path: req.path });
|
|
});
|
|
|
|
|
|
app.get('*', (req, res) => {
|
|
res.sendFile(path.join(__dirname, '../../frontend/dist/index.html'));
|
|
});
|
|
|
|
|
|
app.use(errorHandler);
|
|
|
|
app.listen(PORT, '0.0.0.0', () => {
|
|
console.log(`Server is running on port ${PORT}`);
|
|
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
|
|
}); |