-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
129 lines (114 loc) · 3.77 KB
/
index.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
122
123
124
125
126
127
128
129
//Fastify and plugins
import Fastify from "fastify";
import formbody from "@fastify/formbody";
import fastifyStatic from "@fastify/static";
import httpsRedirect from "fastify-https-redirect";
import authPlugin from "./util/authPlugin";
import cors from "@fastify/cors";
//Game routes
import accountsRouter from "./routes/as1/accounts";
import gameplayRouter from "./routes/as1/gameplay";
import informationRouter from "./routes/as1/information";
import radioRouter from "./routes/as1/radio";
//API routes
import apiAuthRouter from "./routes/api/auth";
import apiUsersRouter from "./routes/api/users";
import apiServerRouter from "./routes/api/server";
import apiScoresRouter from "./routes/api/scores";
import apiSongsRouter from "./routes/api/songs";
import apiShoutsRouter from "./routes/api/shouts";
import apiRankingsRouter from "./routes/api/rankings";
//Miscellaneous
import fs from "fs";
import path from "path";
import WavebreakerConfig from "./config/wavebreaker_config.json";
import { Prisma } from "@prisma/client";
globalThis.__basedir = __dirname; //Set global variable for the base directory
//weird hack to select logger based on environment
const logger = {
...(process.env.NODE_ENV == "development" && {
logger: {
transport: {
target: "pino-pretty",
options: {
translateTime: "HH:MM:ss Z",
ignore: "pid,hostname",
},
},
},
}),
...(process.env.NODE_ENV != "development" && {
logger: true,
}),
};
const fastify = Fastify({
trustProxy: WavebreakerConfig.reverseProxy,
...logger,
//For HTTPS in production, please use nginx or whatever
...(process.env.NODE_ENV == "development" &&
WavebreakerConfig.useHttps && {
https: {
key: fs.readFileSync(WavebreakerConfig.https.key),
cert: fs.readFileSync(WavebreakerConfig.https.cert),
passphrase: WavebreakerConfig.https.passphrase,
},
}),
});
fastify.listen(
{ port: WavebreakerConfig.port, host: WavebreakerConfig.host },
(err) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
}
);
if (process.env.NODE_ENV == "development" && WavebreakerConfig.useHttps)
fastify.register(httpsRedirect); //HTTPS redirect for development, PLEASE use nginx or whatever for this in prod, I *beg* you.
fastify.register(authPlugin); //Register authentication plugin
fastify.register(formbody); //So we can parse requests that use application/x-www-form-urlencoded
fastify.register(fastifyStatic, {
root: path.join(__dirname, "RadioSongs"),
prefix: "/as/asradio/",
});
fastify.register(cors, {
origin: WavebreakerConfig.corsOrigin,
methods: ["GET", "POST"],
allowedHeaders: ["Content-Type", "Authorization"],
exposedHeaders: ["Content-Type", "Authorization"],
credentials: true,
optionsSuccessStatus: 204,
});
fastify.setErrorHandler(function (error, request, reply) {
// Log error
this.log.error(error);
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === "P2025"
) {
// Prisma: not found
reply.status(404).send({ error: "Not found" });
}
if (error.statusCode === 401) {
reply.code(401).send({ error: "Unauthorized" });
return;
}
if (error.code === "FST_ERR_VALIDATION") {
// Fastify validation error
reply.status(400).send({ error: "Request validation failed." });
}
reply.status(500).send({ error: "An error has occurred." });
});
//Register game endpoints
fastify.register(accountsRouter);
fastify.register(gameplayRouter);
fastify.register(informationRouter);
fastify.register(radioRouter);
//Wavebreaker API
fastify.register(apiAuthRouter);
fastify.register(apiUsersRouter);
fastify.register(apiServerRouter);
fastify.register(apiScoresRouter);
fastify.register(apiSongsRouter);
fastify.register(apiShoutsRouter);
fastify.register(apiRankingsRouter);