2026-05-24 21:54:58 +00:00
|
|
|
|
const express = require('express');
|
|
|
|
|
|
const cors = require('cors');
|
|
|
|
|
|
const passport = require('passport');
|
|
|
|
|
|
require('./config/passport'); // Load JWT strategy
|
2026-05-25 17:03:06 +00:00
|
|
|
|
const User = require('./models/User');
|
|
|
|
|
|
const Block = require('./models/Block');
|
2026-05-24 21:54:58 +00:00
|
|
|
|
const { sequelize, connectDB } = require('./config/database');
|
|
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
const PORT = process.env.PORT || 5000;
|
|
|
|
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
|
|
app.use(cors());
|
|
|
|
|
|
app.use(express.json({ limit: '50mb' }));
|
|
|
|
|
|
app.use(passport.initialize());
|
|
|
|
|
|
|
2026-05-25 17:03:06 +00:00
|
|
|
|
// Serve static frontend
|
|
|
|
|
|
app.use(express.static('public'));
|
|
|
|
|
|
|
2026-05-24 21:54:58 +00:00
|
|
|
|
// Routes
|
|
|
|
|
|
app.use('/api/auth', require('./routes/auth'));
|
|
|
|
|
|
app.use('/api/drawings', require('./routes/drawings'));
|
|
|
|
|
|
app.use('/api/blocks', require('./routes/blocks'));
|
|
|
|
|
|
app.use('/api/dxf', require('./routes/dxf'));
|
|
|
|
|
|
app.use('/api/plugins', require('./routes/plugins'));
|
|
|
|
|
|
|
|
|
|
|
|
// Health check
|
|
|
|
|
|
app.get('/api/health', (req, res) => res.json({ status: 'ok' }));
|
|
|
|
|
|
|
2026-05-25 17:03:06 +00:00
|
|
|
|
// SPA fallback – serve index.html for all non-API routes
|
|
|
|
|
|
app.get(/^(?!\/api\/).*/, (req, res) => {
|
|
|
|
|
|
res.sendFile('public/index.html', { root: __dirname });
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-05-24 21:54:58 +00:00
|
|
|
|
// Connect DB and start
|
2026-05-25 17:03:06 +00:00
|
|
|
|
connectDB().then(() => sequelize.sync({ force: false })).then(async () => {
|
|
|
|
|
|
// Seed admin user if not exists
|
|
|
|
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
|
|
const existing = await User.findOne({ where: { email: 'admin@cad.local' } });
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
|
await User.create({ email: 'admin@cad.local', password: 'admin123' });
|
|
|
|
|
|
console.log('Admin user created: admin@cad.local / admin123');
|
|
|
|
|
|
}
|
2026-05-24 21:54:58 +00:00
|
|
|
|
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
|
console.error('Unable to start server:', err);
|
2026-05-25 17:03:06 +00:00
|
|
|
|
});
|