-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (121 loc) · 3.91 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
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
const { json, urlencoded } = require("express");
const routerDatasetManipulation = require("./0http/datasetManipulations");
const routerSystem = require("./0http/system");
const routerDynamic = require("./0http/router");
const { $serviceKit } = require("./kits/ServiceKit");
const { $databaseKit } = require("./kits/DatabaseKit");
const { $loggerKit } = require("./kits/LoggerKit");
const _ = require("lodash");
const moment = require("moment");
const zero = require("0http");
const sequential = require("0http/lib/router/sequential");
const { $configuratorKit } = require("./kits/ConfiguratorKit");
let router;
let server;
if ($configuratorKit.get("http.engine") === "0http") {
const sequential = require("0http/lib/router/sequential");
const zero = require("0http");
const unpackZero = zero({
router: sequential({
cacheSize: 2000,
}),
});
router = unpackZero.router;
server = unpackZero.server;
} else if ($configuratorKit.get("http.engine") === "express") {
const express = require("express");
const { createServer } = require("http");
router = express();
server = createServer();
}
const host = $configuratorKit.get("http.host", "0.0.0.0");
const port = $configuratorKit.get("http.port", 3000);
const protocol = $configuratorKit.get("http.protocol", "tcp");
router.use(urlencoded());
router.use(json());
router.use(function(req, res, next) {
req.url = req.url.replace(/\/+/g, "/")
req.path = req.path.replace(/\/+/g, "/")
req.originalUrl = req.originalUrl.replace(/\/+/g, "/")
return next();
});
router.use((req, _, next) => {
$loggerKit.getLogger().debug(`${req.method} ${req.url}`)
return next()
});
routerDatasetManipulation(router);
routerSystem(router);
routerDynamic(router);
// router.use("/", function (req, res) {
// return json({ req, res }, {
// statusCode: 404,
// data: "Not found"
// })
// });
if ($configuratorKit.get("http.loggerEnabled")) {
/**
* Logs each HTTP request if logging is enabled in the configuration.
* @param {Object} req - The HTTP request object.
* @param {Object} res - The HTTP response object.
*/
server.on("request", function(req, res) {
$loggerKit.getLogger().debug({ req, res });
});
}
$loggerKit.getLogger().info(`Starting background services`);
$serviceKit.createIntervalService(
"jobCleanerNoIndex",
() => {
/**
* Finds and removes HTTP requests that have a 'noindex' flag within 'routerMeta'.
* Logs the result to the console.
*/
$databaseKit
.findAndRemoveHttpRequests(
{
"routerMeta.noindex": true,
},
{
startTime: moment(new Date(1999, 1, 1)).toISOString(),
endTime: moment(new Date(2999, 1, 1)).toISOString(),
},
)
.then(console.log);
/**
* Finds and removes HTTP requests that have a 'noindex' flag within 'meta'.
* Logs the result to the console.
*/
$databaseKit
.findAndRemoveHttpRequests(
{
"meta.noindex": true,
},
{
startTime: moment(new Date(1999, 1, 1)).toISOString(),
endTime: moment(new Date(2999, 1, 1)).toISOString(),
},
)
.then(console.log);
},
86400 * 1000,
);
/**
* This service is designed to periodically clean up traffic records marked with a 'noindex' flag.
* It targets both 'routerMeta.noindex' and 'meta.noindex' within the stored HTTP requests,
* ensuring that no indexed records are purged from the system on a daily basis.
*/
$serviceKit.spawnService("jobCleanerNoIndex");
/**
* Starts the server listening on the specified port and host.
* Logs the server's readiness and its address upon successful launch.
*/
server.listen(parseInt(port), String(host), function() {
$loggerKit
.getLogger()
.info("The TrafficLight is ready to route your traffic");
$loggerKit
.getLogger()
.info(
`Hosted on ${protocol}://${host}:${port} (click on it: http://${host}:${port})`,
);
});