-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathindex.ts
More file actions
125 lines (111 loc) · 3.71 KB
/
index.ts
File metadata and controls
125 lines (111 loc) · 3.71 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import fastifyCors from '@fastify/cors'
import fastifyJwt from '@fastify/jwt'
import fastifyMultipart from '@fastify/multipart'
import fastifyRedis from '@fastify/redis'
import fastifySwaggerUi from '@fastify/swagger-ui'
import type { FastifyInstance } from 'fastify'
import { config } from '@/config'
import { setInitialCounterValue } from '@/domain/services/shared-urls/set-initial-counter-value'
import { sharedUrlCounterFactory } from '@/infra/factories/shared-url-counter-factory'
import { registerRateLimit } from '../rate-limit'
import { achievementsRoutes } from './achievements'
import { feedbackRoutes } from './feedback'
import { followsRoutes } from './follow'
import { healthCheck } from './healthcheck'
import { imagesRoutes } from './images'
import { importRoutes } from './import'
import { likesRoutes } from './likes'
import { listItemRoute } from './list-item'
import { listsRoute } from './lists'
import { loginRoute } from './login'
import { userRecommendationsRoutes } from './recommendations'
import { reviewRepliesRoute } from './review-replies'
import { reviewsRoute } from './reviews'
import { sharedUrlRoute } from './shared-url'
import { socialAuthRoutes } from './social-auth'
import { socialLinksRoute } from './social-links'
import { subscriptionsRoutes } from './subscriptions'
import { tmdbProxyRoutes } from './tmdb-proxy'
import { userActivitiesRoutes } from './user-activities'
import { userEpisodesRoutes } from './user-episodes'
import { userFavoritesRoutes } from './user-favorites'
import { userItemsRoutes } from './user-items'
import { userStatsRoutes } from './user-stats'
import { usersRoute } from './users'
import { watchEntriesRoutes } from './watch-entries'
import { webhookRoutes } from './webhook'
export function routes(app: FastifyInstance) {
if (config.app.APP_ENV === 'dev') {
app.register(fastifySwaggerUi, {
routePrefix: '/api-docs',
})
}
app.register(fastifyCors, {
origin: getCorsOrigin(),
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: [
'Content-Type',
'Authorization',
'X-Client-Token',
'X-Client',
],
credentials: true,
strictPreflight: false,
})
app.register(fastifyJwt, {
secret: config.app.JWT_SECRET,
})
app.register(fastifyMultipart)
app.register(fastifyRedis, {
url: config.redis.REDIS_URL,
})
app.addHook('onReady', async () => {
const counter = sharedUrlCounterFactory(
app.redis,
config.sharedUrls.SHARED_URLS_COUNTER_KEY
)
await setInitialCounterValue(
counter,
config.sharedUrls.SHARED_URLS_COUNTER_START_VAL - 1
)
})
registerRateLimit(app)
app.register(usersRoute)
app.register(listsRoute)
app.register(loginRoute)
app.register(socialAuthRoutes)
app.register(healthCheck)
app.register(reviewsRoute)
app.register(listItemRoute)
app.register(userItemsRoutes)
app.register(webhookRoutes)
app.register(reviewRepliesRoute)
app.register(socialLinksRoute)
app.register(userEpisodesRoutes)
app.register(likesRoutes)
app.register(userStatsRoutes)
app.register(imagesRoutes)
app.register(followsRoutes)
app.register(importRoutes)
app.register(userActivitiesRoutes)
app.register(sharedUrlRoute)
app.register(subscriptionsRoutes)
app.register(watchEntriesRoutes)
app.register(tmdbProxyRoutes)
app.register(feedbackRoutes)
app.register(userFavoritesRoutes)
app.register(userRecommendationsRoutes)
app.register(achievementsRoutes)
return
}
function getCorsOrigin(): true | string[] {
if (config.app.APP_ENV !== 'production') {
return true
}
const allowedOrigins = [
config.app.CLIENT_URL,
'https://plotwist.app',
'https://www.plotwist.app',
]
return [...new Set(allowedOrigins)]
}