-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.ts
121 lines (102 loc) · 3.28 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
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
import v2 from "@/v2/routes/router";
import { logger, reportError } from "@/v2/utils/logger";
import caching from "@fastify/caching";
import fastifyCookie from "@fastify/cookie";
import cors from "@fastify/cors";
import Formbody from "@fastify/formbody";
import redis from "@fastify/redis";
import fastifySession from "@fastify/session";
import type { JsonSchemaToTsProvider } from "@fastify/type-provider-json-schema-to-ts";
import MongoStore from "connect-mongo";
import RedisStore from "connect-redis";
import crypto from "crypto";
import Fastify from "fastify";
import IORedis from "ioredis";
import { MongoClient } from "mongodb";
import mongoose from "mongoose";
const { env } = process;
const LOG_LEVEL: boolean = env.NODE_ENV !== "production";
const abcache = require("abstract-cache"); // eslint-disable-line @typescript-eslint/no-var-requires
// const v1 = require(`./v1/routes/router.js`);
const secret = env.SESSION_SECRET || crypto.randomBytes(128).toString(`base64`);
const fastify = Fastify({
logger: LOG_LEVEL,
}).withTypeProvider<JsonSchemaToTsProvider>();
async function run() {
try {
await Promise.allSettled([mongoose.connect(env.MONGO_URI), fastify.listen(env.BACKEND_PORT, `0.0.0.0`)]);
logger.info(`Connected to Atlas.`);
logger.info(`Server started, listening to ${env.BACKEND_PORT}`);
} catch (error) {
reportError(error, `Error starting server`);
process.exit(1);
}
}
async function register() {
try {
fastify.register(cors, {
origin: env.FRONTEND_URL.split(","),
methods: ["GET", "PUT", "POST"],
allowedHeaders: ["Content-Type", "Authorization", "x-csrf-token"],
credentials: true,
maxAge: 600,
exposedHeaders: ["*", "Authorization"],
});
fastify.register(fastifyCookie);
if (env.REDIS_URL) {
const redisClient = new IORedis({ host: `${env.REDIS_URL as string}` });
const abcacheClient = abcache({
useAwait: true,
driver: {
name: "abstract-cache-redis",
options: { client: redisClient },
},
});
fastify.register(redis, { client: redisClient });
fastify.register(caching, { cache: abcacheClient });
fastify.register(fastifySession, {
secret,
store: new RedisStore({
client: redisClient,
ttl: 14 * 24 * 60 * 60,
}),
cookie: {
sameSite: "none",
secure: true,
},
});
} else {
const client = new MongoClient(env.MONGO_URI);
await client.connect();
const abcacheClient = abcache({
useAwait: true,
driver: {
name: `abstract-cache-mongo`,
options: { client, dbName: null },
},
});
fastify.register(caching, { cache: abcacheClient });
fastify.register(fastifySession, {
secret,
store: MongoStore.create({
mongoUrl: env.MONGO_URI,
ttl: 14 * 24 * 60 * 60,
autoRemove: `native`,
}),
cookie: {
// sameSite: 'none',
secure: true,
},
});
}
fastify.register(Formbody);
// fastify.register(v1, { prefix: `v1` });
fastify.register(v2, { prefix: `v2` });
run();
} catch (error) {
reportError(error, `Error registering middleware`);
process.exit(1);
}
}
register();
export {};