From cc728d732441804157ca2d1a0030f29d83e42e35 Mon Sep 17 00:00:00 2001 From: Kyle Belanger Date: Wed, 5 Mar 2025 17:11:32 -0500 Subject: [PATCH] add app.js --- backend/src/app.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 backend/src/app.js diff --git a/backend/src/app.js b/backend/src/app.js new file mode 100644 index 0000000..e683e4a --- /dev/null +++ b/backend/src/app.js @@ -0,0 +1,57 @@ +const express = require('express'); +const cors = require('cors'); +const dotenv = require('dotenv'); +const { sequelize, testConnection } = require('./config/db'); +const { Op } = require('sequelize'); + +// Import routes +const authRoutes = require('./routes/authRoutes'); +const whiskeyRoutes = require('./routes/whiskeyRoutes'); +const collectionRoutes = require('./routes/collectionRoutes'); +const ratingRoutes = require('./routes/ratingRoutes'); + +// Load environment variables +dotenv.config(); + +// Initialize app +const app = express(); +const PORT = process.env.PORT || 3000; + +// Middleware +app.use(cors()); +app.use(express.json()); + +// Routes +app.use('/api/auth', authRoutes); +app.use('/api/whiskies', whiskeyRoutes); +app.use('/api/collection', collectionRoutes); +app.use('/api/ratings', ratingRoutes); + +// Root route +app.get('/', (req, res) => { + res.send('Whiskey Collection API is running'); +}); + +// Sync database and start server +const startServer = async () => { + try { + // Test database connection + await testConnection(); + + // Sync models with database + await sequelize.sync({ alter: true }); + console.log('Database synchronized'); + + // Start server + app.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); + }); + } catch (error) { + console.error('Failed to start server:', error); + process.exit(1); + } +}; + +startServer(); + +module.exports = app; \ No newline at end of file