-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
72 lines (61 loc) · 2.24 KB
/
app.ts
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
69
70
71
72
import fastifyJwt from "@fastify/jwt";
import fastify from "fastify";
import cors from "@fastify/cors";
import fastifyCookie from "@fastify/cookie";
import fastifyMultipart from "@fastify/multipart";
import { env } from "./env";
import { usersRoutes } from "./http/controllers/users/routes";
import { followsRoutes } from "./http/controllers/follows/routes";
import { userActivitiesRoutes } from "./http/controllers/user-activities/routes";
import { bookRoutes } from "./http/controllers/books/routes";
import { favoriteBooksRoutes } from "./http/controllers/favorite-books/routes";
import { readRoutes } from "./http/controllers/read/routes";
import { progressRoutes } from "./http/controllers/progress/routes";
import { likeRoutes } from "./http/controllers/likes/routes";
import { bookListRoutes } from "./http/controllers/book-lists/routes";
import { booksOnBookListsRoutes } from "./http/controllers/books-on-book-lists/routes";
import errorHandler from "./utils/error-handler";
export const app = fastify({
bodyLimit: 1024 * 1024 * 512, // 0.5GB
});
app.register(cors, {
origin: env.BASE_URL_FRONT_END,
methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true,
});
app.register(fastifyMultipart, {
addToBody: true,
});
app.register(fastifyJwt, {
secret: env.JWT_SECRET,
cookie: {
cookieName: "refreshToken",
signed: false,
},
sign: {
expiresIn: "30d",
},
});
app.register(fastifyCookie);
app.register(usersRoutes);
app.register(followsRoutes);
app.register(userActivitiesRoutes);
app.register(bookRoutes);
app.register(favoriteBooksRoutes);
app.register(readRoutes);
app.register(progressRoutes);
app.register(likeRoutes);
app.register(bookListRoutes);
app.register(booksOnBookListsRoutes);
app.addHook("onSend", (request, reply, payload, done) => {
reply.header("Cross-Origin-Embedder-Policy", "require-corp");
reply.header("Cross-Origin-Opener-Policy", "same-origin");
done(null, payload);
});
app.setErrorHandler((error, _request, reply) => {
if (env.NODE_ENV === "production") {
// TODO: Here we should log to and external tool like DataDog/NewRelic/Sentry
}
errorHandler(error, _request, reply);
});