74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import mysql from 'mysql2/promise';
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 5000;
|
|
|
|
// Middleware
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
// Database connection
|
|
const pool = mysql.createPool({
|
|
host: process.env.DB_HOST || 'mysql',
|
|
user: process.env.DB_USER || 'root',
|
|
password: process.env.DB_PASSWORD || 'password',
|
|
database: process.env.DB_NAME || 'rsvp_db',
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
queueLimit: 0
|
|
});
|
|
|
|
// Routes
|
|
app.get('/api/events', async (req, res) => {
|
|
try {
|
|
const [rows] = await pool.query('SELECT * FROM events');
|
|
res.json(rows);
|
|
} catch (error) {
|
|
console.error('Error fetching events:', error);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
});
|
|
|
|
app.post('/api/events', async (req, res) => {
|
|
try {
|
|
const { title, description, date, location } = req.body;
|
|
const [result] = await pool.query(
|
|
'INSERT INTO events (title, description, date, location) VALUES (?, ?, ?, ?)',
|
|
[title, description, date, location]
|
|
);
|
|
res.status(201).json(result);
|
|
} catch (error) {
|
|
console.error('Error creating event:', error);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
});
|
|
|
|
// Initialize database tables
|
|
async function initializeDatabase() {
|
|
try {
|
|
await pool.query(`
|
|
CREATE TABLE IF NOT EXISTS events (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
title VARCHAR(255) NOT NULL,
|
|
description TEXT,
|
|
date DATETIME NOT NULL,
|
|
location VARCHAR(255),
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
console.log('Database initialized successfully');
|
|
} catch (error) {
|
|
console.error('Error initializing database:', error);
|
|
}
|
|
}
|
|
|
|
// Start server
|
|
app.listen(port, () => {
|
|
console.log(`Server running on port ${port}`);
|
|
initializeDatabase();
|
|
});
|