-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
104 lines (88 loc) · 3.29 KB
/
index.js
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
require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const app = express();
const requestId = require("./utils/requestId");
const chatRoutes = require("./openAI/chat");
const textRoutes = require("./openAI/text");
const imgRoutes = require("./openAI/image");
const embeddingRoutes = require("./openAI/embeddings");
const modelRoutes = require("./openAI/models");
const moderationRoutes = require("./openAI/moderation");
const audioRoutes = require("./openAI/audio");
const fineTuningRoutes = require("./openAI/fineTuning");
const batchRoutes = require("./openAI/batch");
const filesRoutes = require("./openAI/files");
const uploadsRoutes = require("./openAI/uploads");
const { load: loadRandomContents } = require("./utils/randomContents");
const delay = require("./utils/delay")
const { register, requestCounter, requestLatency, payloadSize } = require("./utils/metrics")
const setupApp = async () => {
await loadRandomContents();
const req_limit = process.env.REQUEST_SIZE_LIMIT || "10kb";
app.use(express.json({"limit": req_limit}));
// Request Logger Configuration
app.use(requestId);
morgan.token('id', function getId(req) {
return req.id
});
const loggerFormat = ':id [:date[web]]" :method :url" :status :response-time ms'
app.use(morgan(loggerFormat, {
skip: function (req, res) {
return res.statusCode < 400
},
stream: process.stderr
}));
app.use(morgan(loggerFormat, {
skip: function (req, res) {
return res.statusCode >= 400
},
stream: process.stderr
}));
app.use(chatRoutes);
app.use(textRoutes);
app.use(imgRoutes);
app.use(embeddingRoutes);
app.use(modelRoutes);
app.use(moderationRoutes);
app.use(audioRoutes);
app.use(fineTuningRoutes);
app.use(batchRoutes);
app.use(filesRoutes);
app.use(uploadsRoutes);
app.get("/", async (req, res) => {
const then = Date.now();
const delayHeader = req.headers["x-set-response-delay-ms"]
let delayTime = parseInt(delayHeader) || parseInt(process.env.RESPONSE_DELAY_MS) || 0
await delay(delayTime)
requestCounter.inc({ method: "GET", path: "/", status: res.statusCode });
requestLatency.observe({ method: "GET", path: "/", status: 200 }, (Date.now() - then));
payloadSize.observe({ method: "GET", path: "/", status: 200 }, req.socket.bytesRead);
res.send("Hello World! This is MockAI");
});
app.get("/metrics", async (req, res) => {
res.set("Content-Type", register.contentType);
res.end(await register.metrics());
});
app.use(function (req, res) {
const then = Date.now();
requestCounter.inc({ method: req.method, path: req.path, status: 404 });
requestLatency.observe({ method: req.method, path: req.path, status: 404 }, (Date.now() - then));
payloadSize.observe({ method: req.method, path: req.path, status: 404 }, req.socket.bytesRead);
res.status(404).send("Page not found");
});
return app;
};
const startServer = async () => {
await setupApp();
const port = process.env.SERVER_PORT || 5001;
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
};
// Export both the app and the setup function
module.exports = { app, setupApp };
// Start the server if this file is run directly
if (require.main === module) {
startServer();
}