47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
const express = require('express');
|
||
const cors = require('cors');
|
||
const passport = require('passport');
|
||
require('./config/passport'); // Load JWT strategy
|
||
const User = require('./models/User');
|
||
const Block = require('./models/Block');
|
||
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());
|
||
|
||
// Serve static frontend
|
||
app.use(express.static('public'));
|
||
|
||
// 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' }));
|
||
|
||
// SPA fallback – serve index.html for all non-API routes
|
||
app.get(/^(?!\/api\/).*/, (req, res) => {
|
||
res.sendFile('public/index.html', { root: __dirname });
|
||
});
|
||
|
||
// Connect DB and start
|
||
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');
|
||
}
|
||
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
||
}).catch(err => {
|
||
console.error('Unable to start server:', err);
|
||
}); |