48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import authRoutes from './routes/auth.js';
|
|
import fileRoutes from './routes/files.js';
|
|
import { authenticate } from './middleware/auth.js';
|
|
|
|
dotenv.config();
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const app = express();
|
|
app.set('trust proxy', true);
|
|
|
|
// Middleware
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ limit: '50mb', extended: true }));
|
|
|
|
// Serve static files
|
|
app.use('/outputs', express.static(path.join(__dirname, 'outputs')));
|
|
app.use('/source', express.static(path.join(__dirname, 'source')));
|
|
|
|
// Routes
|
|
app.use('/api/auth', authRoutes);
|
|
app.use('/api/files', authenticate, fileRoutes);
|
|
|
|
// Health check
|
|
app.get('/health', (req, res) => {
|
|
res.json({ status: 'Server is running' });
|
|
});
|
|
|
|
// Error handling middleware
|
|
app.use((err, req, res, next) => {
|
|
console.error(err.stack);
|
|
res.status(err.status || 500).json({
|
|
error: err.message || 'Internal Server Error'
|
|
});
|
|
});
|
|
|
|
const PORT = process.env.PORT;
|
|
app.listen(PORT, () => {
|
|
console.log(`Server running on http://localhost:${PORT}`);
|
|
});
|