57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
const express = require('express');
|
|
const http = require('http');
|
|
const cors = require('cors');
|
|
const helmet = require('helmet');
|
|
const morgan = require('morgan');
|
|
const dotenv = require('dotenv');
|
|
const { app: appConfig } = require('./src/config/config');
|
|
const db = require('./src/models');
|
|
const socketio = require('socket.io');
|
|
const path = require('path');
|
|
|
|
// Load env
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const io = socketio(server, {
|
|
cors: {
|
|
origin: appConfig.clientUrl,
|
|
methods: ['GET', 'POST'],
|
|
credentials: true,
|
|
},
|
|
});
|
|
require('./src/sockets')(io);
|
|
|
|
// Middleware
|
|
app.use(cors({ origin: appConfig.clientUrl, credentials: true }));
|
|
app.use(helmet());
|
|
app.use(morgan('dev'));
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: false }));
|
|
|
|
// Serve static files from public
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
// Attach io to app for use in routes/controllers
|
|
app.set('io', io);
|
|
|
|
// Health check
|
|
app.get('/api/health', (req, res) => res.json({ status: 'ok' }));
|
|
|
|
// Mount all API routes
|
|
app.use('/api/v1', require('./src/routes'));
|
|
|
|
// Error handler
|
|
app.use((err, req, res, next) => {
|
|
console.error(err);
|
|
res.status(err.status || 500).json({ error: err.message || 'Internal Server Error' });
|
|
});
|
|
|
|
// Start server
|
|
const PORT = appConfig.port;
|
|
db.sequelize.sync().then(() => {
|
|
server.listen(PORT, () => {
|
|
console.log(`Server running on port ${PORT}`);
|
|
});
|
|
});
|