-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
108 lines (96 loc) · 3.69 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
import "gridfire/models/Activity.js";
import "gridfire/models/Artist.js";
import "gridfire/models/Edition.js";
import "gridfire/models/Favourite.js";
import "gridfire/models/Follower.js";
import "gridfire/models/Play.js";
import "gridfire/models/Release.js";
import "gridfire/models/Sale.js";
import "gridfire/models/StreamSession.js";
import "gridfire/models/User.js";
import "gridfire/models/WishList.js";
import "gridfire/controllers/passport.js";
import { amqpClose, amqpConnect } from "./controllers/amqp/index.js";
import { clientErrorHandler, errorHandler, logErrors } from "./middlewares/errorHandlers.js";
import artists from "./routes/artistRoutes.js";
import artwork from "./routes/artworkRoutes.js";
import auth from "./routes/authRoutes.js";
import catalogue from "./routes/catalogueRoutes.js";
import cookieParser from "cookie-parser";
import cookieSession from "cookie-session";
import { createServer } from "http";
import download from "./routes/downloadRoutes.js";
import express from "express";
import logger from "./controllers/logger.js";
import mongoose from "mongoose";
import passport from "passport";
import editions from "./routes/editions/index.js";
import release from "./routes/releaseRoutes.js";
import sse from "./routes/sseRoutes.js";
import track from "./routes/trackRoutes.js";
import user from "./routes/userRoutes.js";
import web3 from "./routes/web3Routes.js";
import assert from "assert/strict";
const { COOKIE_KEY, MONGODB_URI, PORT = 5000 } = process.env;
let isReady = false;
assert(COOKIE_KEY, "COOKIE_KEY env var missing.");
assert(MONGODB_URI, "MONGODB_URI env var missing.");
process
.on("uncaughtException", error => console.error("[API] Uncaught exception:", error))
.on("unhandledRejection", error => console.error("[API] Unhandled promise rejection:", error));
const app = express();
const server = createServer(app);
// RabbitMQ
await amqpConnect().catch(logger.error);
// Mongoose
mongoose.set("strictQuery", true);
const db = mongoose.connection;
db.once("open", async () => logger.info("Mongoose connected."));
db.on("close", () => logger.info("Mongoose connection closed."));
db.on("disconnected", () => logger.warn("Mongoose disconnected."));
db.on("reconnected", () => logger.info("Mongoose reconnected."));
db.on("error", error => logger.info(`Mongoose error: ${error.message}`));
await mongoose.connect(MONGODB_URI).catch(logger.error);
// Express
app.use(express.json());
app.use(cookieParser(COOKIE_KEY));
app.use(cookieSession({ name: "gridFireSession", keys: [COOKIE_KEY], maxAge: 28 * 24 * 60 * 60 * 1000 }));
app.use(clientErrorHandler);
app.use(errorHandler);
app.use(logErrors);
app.use(passport.initialize());
app.use(passport.session());
app.use("/api/artist", artists);
app.use("/api/artwork", artwork);
app.use("/api/auth", auth);
app.use("/api/catalogue", catalogue);
app.use("/api/download", download);
app.use("/api/editions", editions);
app.use("/api/release", release);
app.use("/api/sse", sse);
app.use("/api/track", track);
app.use("/api/user", user);
app.use("/api/web3", web3);
app.use("/livez", (req, res) => res.sendStatus(200));
app.use("/readyz", (req, res) => (isReady ? res.sendStatus(200) : res.sendStatus(400)));
const handleShutdown = async () => {
isReady = false;
logger.info("Gracefully shutting down…");
try {
await amqpClose();
await mongoose.connection.close(false);
logger.info("Mongoose closed.");
server.close(() => {
logger.info("Express server closed.");
process.exit(0);
});
} catch (error) {
logger.info(error);
process.exitCode = 1;
}
};
process.on("SIGINT", handleShutdown).on("SIGTERM", handleShutdown);
server.listen(PORT, () => {
logger.info(`Express server running on port ${PORT}.`);
isReady = true;
});