|
1 | | -import "dotenv/config"; |
2 | 1 | import fastify from "fastify"; |
3 | 2 | import helmet from "@fastify/helmet"; |
4 | 3 | import rateLimit from "@fastify/rate-limit"; |
@@ -253,23 +252,31 @@ app.setErrorHandler((error, _request, reply) => { |
253 | 252 | }); |
254 | 253 | }); |
255 | 254 |
|
256 | | -// Start server |
257 | | -try { |
258 | | - // Test database connection |
259 | | - await pool.query("SELECT 1"); |
260 | | - app.log.info("Database connection established"); |
| 255 | +// Initialize app (ensure it's ready) |
| 256 | +await app.ready(); |
261 | 257 |
|
262 | | - await app.listen({ port: PORT, host: "0.0.0.0" }); |
263 | | - app.log.info(`Server listening on http://0.0.0.0:${PORT}`); |
264 | | -} catch (error) { |
265 | | - app.log.error(error); |
266 | | - process.exit(1); |
267 | | -} |
| 258 | +// Export for Vercel serverless |
| 259 | +export default app; |
268 | 260 |
|
269 | | -// Graceful shutdown |
270 | | -process.on("SIGTERM", async () => { |
271 | | - app.log.info("SIGTERM received, shutting down gracefully"); |
272 | | - await pool.end(); |
273 | | - await app.close(); |
274 | | - process.exit(0); |
275 | | -}); |
| 261 | +// Start server only when not on Vercel |
| 262 | +if (!process.env.VERCEL) { |
| 263 | + try { |
| 264 | + // Test database connection |
| 265 | + await pool.query("SELECT 1"); |
| 266 | + app.log.info("Database connection established"); |
| 267 | + |
| 268 | + await app.listen({ port: PORT, host: "0.0.0.0" }); |
| 269 | + app.log.info(`Server listening on http://0.0.0.0:${PORT}`); |
| 270 | + } catch (error) { |
| 271 | + app.log.error(error); |
| 272 | + process.exit(1); |
| 273 | + } |
| 274 | + |
| 275 | + // Graceful shutdown |
| 276 | + process.on("SIGTERM", async () => { |
| 277 | + app.log.info("SIGTERM received, shutting down gracefully"); |
| 278 | + await pool.end(); |
| 279 | + await app.close(); |
| 280 | + process.exit(0); |
| 281 | + }); |
| 282 | +} |
0 commit comments