-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
144 lines (114 loc) · 3.82 KB
/
server.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import express from "express";
import cors from "cors";
import cluster from "cluster";
import os from "os";
import { join } from "path";
import "reflect-metadata";
import { container } from "./src/config/inversify.config";
import swaggerJsdoc from "swagger-jsdoc";
import swaggerUiExpress from "swagger-ui-express";
import { fsReadFile } from "ts-loader/dist/utils";
import * as process from "process";
import { Config } from "./src/config/config";
import { responseWrapper } from "./src/middleware/response.middleware";
import { SwaggerHelper } from "./src/helpers/swaggerHelper";
import { InversifyExpressServer } from "inversify-express-utils";
import { SchedulerService } from "./src/services/scheduler.service";
const configs = new Config().getConfig();
const port = process.env.PORT ? process.env.PORT : configs.port;
const cCPUs =
process.env.NODE_ENV && process.env.NODE_ENV === "production"
? os.cpus().length
: 1;
const startDate = new Date();
function setupStatusPage(app) {
app.get("/", (req, res) => {
let file = fsReadFile(join(__dirname, "public", "index.html"));
if (file) {
file = file.replace("{{applicationName}}", "Clarys Express API Server");
file = file.replace("{{serverStartDate}}", startDate.toISOString());
file = file.replace(
"{{serverUptime}}",
`${(new Date().getTime() - startDate.getTime()) / 1000} seconds`
);
file = file.replace("{{swaggerURL}}", "/swagger");
}
res.send(file);
});
}
function setupSwagger(app) {
const swaggerOptions = {
definition: {
openapi: "3.0.0",
info: {
title: "Clarys AI Backend",
version: "1.0.0",
},
components: {},
},
apis: ["./src/controllers/*.ts", __dirname + "/src/controllers/*.ts"],
};
const swaggerDocs: any = swaggerJsdoc(swaggerOptions);
const helper = new SwaggerHelper();
helper.addSwaggerResponseSchema(swaggerDocs);
app.use(
"/swagger",
swaggerUiExpress.serve,
swaggerUiExpress.setup(swaggerDocs)
);
app.get("/test", (req, res) => {
res.send(__dirname);
});
}
const server = new InversifyExpressServer(container);
if (cluster.isPrimary) {
for (let i = 0; i < cCPUs; i++) {
cluster.fork();
}
cluster.on("online", function (worker) {
console.log("Worker " + worker.process.pid + " is online");
});
cluster.on("exit", function (worker, code, signal) {
console.log(
`Worker ${worker.process.pid} died with code : ${code}, and signal ${signal}`
);
if (code !== 0 && signal != "SIGTERM" && signal != "SIGINT") {
console.log("Starting a new worker to replace the dead one");
cluster.fork();
}
});
} else {
server.setConfig((app) => {
setupSwagger(app);
app.use(
cors({
origin: [`http://0.0.0.0:${port}`, `https://0.0.0.0:${port}`],
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: false,
})
);
app.use(responseWrapper);
app.use(express.json());
});
const app: express.Application = server.build();
app.listen(port);
setupStatusPage(app);
const serverInfo = {
"API Port": port,
"Localhost URL": `http://0.0.0.0:${port}`,
"Swagger URL": `http://0.0.0.0:${port}/swagger`,
};
console.table(serverInfo);
const scheduler = container.get<SchedulerService>(SchedulerService.name);
// scheduler.updateOnChainPosts();
// scheduler.updateOnChainPostFolder();
// scheduler.updateOffChainDiscussionsPosts();
// scheduler.updateOffChainDiscussionsPostFolder();
// scheduler.updateOffChainEventsPosts();
// scheduler.updateOffChainEventsAndSubEventsPostFolder();
// scheduler.updateOffChainMeetUpEventsPosts();
// scheduler.updateOffChainMeetUpEventsPostFolder();
// scheduler.updateOnChainDataToVectorStore();
// scheduler.updateOffChainDataToVectorStore();
// scheduler.updateDynamoDb();
}