62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import express, { Request, Response } from 'express';
|
|
import path from 'path';
|
|
import dotenv from 'dotenv';
|
|
import authRoutes from './src/server/routes/authRoutes';
|
|
import roundsRoutes from './src/server/routes/roundsRoutes';
|
|
import communityRoutes from './src/server/routes/communityRoutes';
|
|
import { db } from './src/server/db';
|
|
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
|
|
// Body parsers
|
|
app.use(express.json({ limit: '10mb' }));
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// Health check endpoint
|
|
app.get('/healthz', (_req: Request, res: Response) => {
|
|
res.status(200).send('healthy\n');
|
|
});
|
|
|
|
// API Routes
|
|
app.use('/api/auth', authRoutes);
|
|
app.use('/api/rounds', roundsRoutes);
|
|
app.use('/api/community', communityRoutes);
|
|
|
|
async function startServer() {
|
|
// Initialize persistence backend (MariaDB Galera cluster if configured, or local store)
|
|
await db.init();
|
|
if (!isProduction) {
|
|
// Development mode: Vite middleware
|
|
const { createServer: createViteServer } = await import('vite');
|
|
const vite = await createViteServer({
|
|
server: {
|
|
middlewareMode: true,
|
|
hmr: process.env.DISABLE_HMR !== 'true',
|
|
watch: process.env.DISABLE_HMR === 'true' ? null : {},
|
|
},
|
|
appType: 'spa',
|
|
});
|
|
app.use(vite.middlewares);
|
|
} else {
|
|
// Production mode: Serve static build assets
|
|
const distPath = path.resolve(process.cwd(), 'dist');
|
|
app.use(express.static(distPath));
|
|
app.get('*', (_req: Request, res: Response) => {
|
|
res.sendFile(path.join(distPath, 'index.html'));
|
|
});
|
|
}
|
|
|
|
app.listen(PORT, '0.0.0.0', () => {
|
|
console.log(`🏌️♂️ PinPoint Golf full-stack server running on http://0.0.0.0:${PORT}`);
|
|
});
|
|
}
|
|
|
|
startServer().catch((err) => {
|
|
console.error('Failed to start server:', err);
|
|
process.exit(1);
|
|
});
|