56 lines
No EOL
1.6 KiB
JavaScript
Executable file
56 lines
No EOL
1.6 KiB
JavaScript
Executable file
import express from 'express';
|
|
import cors from 'cors';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import { fileURLToPath } from 'url';
|
|
import initDatabase from './utils/initDatabase.js';
|
|
import matchesRouter from './routes/matches.js';
|
|
import ticketsRouter from './routes/tickets.js';
|
|
import adminRouter from './routes/admin.js';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 3000;
|
|
|
|
// Create uploads directory if it doesn't exist
|
|
const uploadsDir = path.join(__dirname, '..', 'public', 'uploads');
|
|
if (!fs.existsSync(uploadsDir)) {
|
|
fs.mkdirSync(uploadsDir, { recursive: true });
|
|
}
|
|
|
|
// Middleware
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Serve static files from the uploads directory
|
|
app.use('/uploads', express.static(path.join(__dirname, '..', 'public', 'uploads')));
|
|
|
|
// Routes
|
|
app.use('/api/matches', matchesRouter);
|
|
app.use('/api/tickets', ticketsRouter);
|
|
app.use('/api/admin', adminRouter);
|
|
|
|
// Error handling middleware
|
|
app.use((err, req, res, next) => {
|
|
console.error('Error:', err);
|
|
res.status(500).json({
|
|
message: 'Something broke!',
|
|
error: err.message,
|
|
stack: process.env.NODE_ENV === 'development' ? err.stack : undefined
|
|
});
|
|
});
|
|
|
|
// Initialize database and start server
|
|
initDatabase()
|
|
.then(() => {
|
|
app.listen(port, () => {
|
|
console.log(`Server running on port ${port}`);
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.error('Failed to initialize database:', error);
|
|
process.exit(1);
|
|
}); |