Skip to content

Commit d79d991

Browse files
committed
Fix Vercel compatibility
1 parent ddea954 commit d79d991

File tree

5 files changed

+56
-32
lines changed

5 files changed

+56
-32
lines changed

api/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import serverless from "serverless-http";
2+
import app from "../index.js";
3+
4+
// Wrap Fastify app with serverless-http for Vercel compatibility
5+
const handler = serverless(app, {
6+
binary: ["image/*", "application/pdf"],
7+
});
8+
9+
export default handler;

index.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import "dotenv/config";
21
import fastify from "fastify";
32
import helmet from "@fastify/helmet";
43
import rateLimit from "@fastify/rate-limit";
@@ -253,23 +252,31 @@ app.setErrorHandler((error, _request, reply) => {
253252
});
254253
});
255254

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();
261257

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;
268260

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+
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"private": true,
77
"type": "module",
88
"scripts": {
9-
"dev": "node --watch index.js",
10-
"start": "node index.js",
9+
"dev": "node --env-file=.env --watch index.js",
10+
"start": "node --env-file=.env index.js",
1111
"lint": "eslint .",
1212
"format": "prettier --write .",
1313
"format:check": "prettier --check ."
@@ -16,9 +16,9 @@
1616
"dependencies": {
1717
"@fastify/helmet": "^12.0.1",
1818
"@fastify/rate-limit": "^10.1.1",
19-
"dotenv": "^17.2.3",
2019
"fastify": "^5.2.0",
21-
"pg": "^8.13.1"
20+
"pg": "^8.13.1",
21+
"serverless-http": "^4.0.0"
2222
},
2323
"devDependencies": {
2424
"eslint": "^9.18.0",

pnpm-lock.yaml

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vercel.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rewrites": [
3+
{
4+
"source": "/(.*)",
5+
"destination": "/api"
6+
}
7+
]
8+
}

0 commit comments

Comments
 (0)