-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (60 loc) · 2.01 KB
/
Copy pathserver.js
File metadata and controls
68 lines (60 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import fastify from "fastify";
import cors from "@fastify/cors";
import { createRequire } from "node:module";
import routes from "./src/routes/index.js";
import environment from "./src/config/envs.js";
import { swaggerOptions, swaggerUiOptions } from "./src/config/swagger.js";
const require = createRequire(import.meta.url);
const { clerkPlugin } = require("@clerk/fastify");
const app = fastify({
pluginTimeout: 60_000,
bodyLimit: 10 * 1024 * 1024,
});
/* ----- CORS ----- */
app.register(cors, {
origin: (origin, cb) => {
const allow = [
"https://appcashwise.com.br",
"https://www.appcashwise.com.br",
"https://cashwiseapi-hav8m.kinsta.app",
"https://www.cashwiseapi-hav8m.kinsta.app",
"http://localhost:3000",
"http://127.0.0.1:3000",
];
if (!origin || allow.includes(origin)) cb(null, true);
else cb(new Error("Not allowed by CORS"), false);
},
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
allowedHeaders: "*",
credentials: true,
exposedHeaders: [
"x-total-count",
"x-current-page",
"x-per-page",
"x-total-pages",
],
});
/* ----- Swagger Documentation ----- */
await app.register(import("@fastify/swagger"), swaggerOptions);
await app.register(import("@fastify/swagger-ui"), swaggerUiOptions);
/* ----- Auth (Clerk) ----- */
app.register(clerkPlugin, {
secretKey: environment.secretKey,
publishableKey: environment.publishableKey,
});
/* ----- Custom plugins ----- */
await app.register(import("./src/plugins/userId.js"));
await app.register(import("./src/plugins/zodValidate.js"));
/* ----- Rotas ----- */
await app.register(routes);
/* ----- Start ----- */
app
.listen({ port: environment.port ?? 8080, host: "0.0.0.0" })
.then(() => {
console.log(`🚀 Server rodando na porta ${environment.port ?? 8080}`);
console.log(`📚 Documentação disponível em: http://localhost:${environment.port ?? 8080}/docs`);
})
.catch((err) => {
console.error("Erro ao iniciar o servidor:", err);
process.exit(1);
});