69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
require('dotenv').config();
|
|
const express = require('express');
|
|
const cors = require('cors');
|
|
const cookieParser = require('cookie-parser');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const businessesRoutes = require('./routes/businesses');
|
|
const { fdkExtension, isFdkConfigured } = require('./fdk');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3001;
|
|
const CLIENT_DIST_DIR = [
|
|
path.join(__dirname, 'public'),
|
|
path.join(__dirname, '..', 'client', 'dist'),
|
|
].find((dir) => fs.existsSync(path.join(dir, 'index.html')));
|
|
|
|
app.use(cors({ origin: true, credentials: true }));
|
|
app.use(cookieParser('ext.session'));
|
|
app.use(express.json({ limit: '10mb' }));
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Health check
|
|
app.get('/api/health', (req, res) => res.json({
|
|
ok: true,
|
|
timestamp: new Date().toISOString(),
|
|
fdkConfigured: isFdkConfigured,
|
|
}));
|
|
|
|
if (fdkExtension) {
|
|
app.use(fdkExtension.fdkHandler);
|
|
}
|
|
|
|
// Routes
|
|
app.use('/api/businesses', businessesRoutes);
|
|
|
|
// Serve the built client for same-origin deployment.
|
|
if (CLIENT_DIST_DIR) {
|
|
app.use(express.static(CLIENT_DIST_DIR));
|
|
}
|
|
|
|
// Preserve JSON 404s for unknown API routes.
|
|
app.use('/api', (req, res) => res.status(404).json({ error: 'Route not found' }));
|
|
|
|
// SPA fallback for non-API browser routes.
|
|
app.use((req, res, next) => {
|
|
if (req.method !== 'GET') {
|
|
return res.status(404).json({ error: 'Route not found' });
|
|
}
|
|
|
|
if (!CLIENT_DIST_DIR) {
|
|
return res.status(404).json({ error: 'Route not found' });
|
|
}
|
|
|
|
res.sendFile(path.join(CLIENT_DIST_DIR, 'index.html'), (err) => {
|
|
if (err) next(err);
|
|
});
|
|
});
|
|
|
|
// Error handler
|
|
app.use((err, req, res, next) => {
|
|
console.error(err.stack);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`SMS Extension server running on port ${PORT}`);
|
|
});
|