|
| 1 | +import 'dotenv/config'; |
| 2 | +import express from 'express'; |
| 3 | +import path from 'path'; |
| 4 | +import fs from 'fs'; |
| 5 | +import { createServer } from 'http'; |
| 6 | + |
| 7 | +const app = express(); |
| 8 | +app.use(express.json()); |
| 9 | +app.use(express.urlencoded({ extended: false })); |
| 10 | + |
| 11 | +// Simple logging middleware |
| 12 | +app.use((req, res, next) => { |
| 13 | + console.log(`${new Date().toISOString()} ${req.method} ${req.path}`); |
| 14 | + next(); |
| 15 | +}); |
| 16 | + |
| 17 | +// Basic API routes that don't depend on complex database operations |
| 18 | +app.get('/api/health', (req, res) => { |
| 19 | + res.json({ status: 'ok', timestamp: new Date().toISOString() }); |
| 20 | +}); |
| 21 | + |
| 22 | +app.get('/api/test', (req, res) => { |
| 23 | + res.json({ message: 'BotSmith API is running!' }); |
| 24 | +}); |
| 25 | + |
| 26 | +// Serve static files from the dist/public directory |
| 27 | +const distPath = path.resolve(process.cwd(), 'dist', 'public'); |
| 28 | + |
| 29 | +if (!fs.existsSync(distPath)) { |
| 30 | + console.error(`Build directory not found: ${distPath}`); |
| 31 | + process.exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +app.use(express.static(distPath)); |
| 35 | + |
| 36 | +// Catch-all handler: serve index.html for any non-API routes |
| 37 | +app.use('*', (req, res) => { |
| 38 | + res.sendFile(path.resolve(distPath, 'index.html')); |
| 39 | +}); |
| 40 | + |
| 41 | +// Error handler |
| 42 | +app.use((err: any, req: any, res: any, next: any) => { |
| 43 | + console.error('Error:', err); |
| 44 | + res.status(500).json({ error: 'Internal server error' }); |
| 45 | +}); |
| 46 | + |
| 47 | +const server = createServer(app); |
| 48 | +const port = process.env.PORT ? parseInt(process.env.PORT) : 3000; |
| 49 | +const host = '0.0.0.0'; |
| 50 | + |
| 51 | +server.listen(port, host, () => { |
| 52 | + console.log(`🚀 BotSmith server running on ${host}:${port}`); |
| 53 | + console.log(`📁 Serving static files from: ${distPath}`); |
| 54 | + console.log(`🔗 Visit: http://${host}:${port}`); |
| 55 | +}); |
| 56 | + |
0 commit comments