-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
309 lines (273 loc) · 10.9 KB
/
Copy pathindex.js
File metadata and controls
309 lines (273 loc) · 10.9 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Process environment variables from parent process if available
process.on('message', (message) => {
if (message.type === 'start') {
// Start the server with the provided configuration
server = ServerlessAPI(message.config);
} else if (message.type === 'shutdown') {
// Gracefully shut down the server
shutdown();
}
});
process.on('uncaughtException', err => {
console.error('There was an uncaught error', err);
// Notify parent process of the error
if (process.connected) {
try {
process.send({ type: 'error', error: err.message });
} catch (e) {
console.error("Failed to send error message to parent process", e);
}
}
// Exit with a non-zero code to indicate an error
process.exit(1);
});
process.on('SIGTERM', (signal) => {
process.shuttingDown = true;
console.info('Received signal:', signal, ". Activating the gracefulTerminationWatcher.");
shutdown();
});
let server = null;
function shutdown() {
console.info('Shutting down server...');
if (server) {
server.close(() => {
console.info('Server has been gracefully shut down');
process.exit(0);
});
} else {
process.exit(0);
}
}
function ServerlessAPI(config) {
let { storage, port, dynamicPort = true, host, urlPrefix } = config;
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
console.log("SERVERLESS_ID before", process.env.SERVERLESS_ID)
process.env.SERVERLESS_ID = config.urlPrefix;
process.env.SERVERLESS_ROOT_FOLDER = storage;
console.log("SERVERLESS_ID after", process.env.SERVERLESS_ID)
console.log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
// Validate that storage is defined
if (!storage) {
throw new Error("Storage path must be defined for ServerlessAPI initialization");
}
urlPrefix = `/${urlPrefix}`;
const httpWrapper = require("../http-wrapper");
const Server = httpWrapper.Server;
const bodyReaderMiddleware = require("../http-wrapper/utils/middlewares").bodyReaderMiddleware;
const PluginManager = require("./lib/PluginManager");
// Create the plugin manager with storage path for plugin discovery
const pluginManager = new PluginManager(storage);
// Initialize plugin manager to discover and load plugins
(async () => {
try {
console.log(`Initializing PluginManager with storage path: ${storage}`);
await pluginManager.init();
console.log('PluginManager initialization completed');
} catch (error) {
console.error('Error initializing PluginManager:', error);
}
})();
const CHECK_FOR_RESTART_COMMAND_FILE_INTERVAL = 500;
host = host || "127.0.0.1";
port = port || 8082;
const server = new Server();
server.config = config.serverConfig || config;
if (!config.storage) {
config.storage = storage;
}
let accessControlAllowHeaders = new Set();
accessControlAllowHeaders.add("Content-Type");
accessControlAllowHeaders.add("Content-Length");
accessControlAllowHeaders.add("X-Content-Length");
accessControlAllowHeaders.add("Access-Control-Allow-Origin");
accessControlAllowHeaders.add("User-Agent");
accessControlAllowHeaders.add("Authorization");
let listenCallback = (err) => {
if (err) {
if (dynamicPort && err.code === 'EADDRINUSE') {
console.debug("Failed to listen on port <" + port + ">", err);
function getRandomPort() {
const min = 9000;
const max = 65535;
return Math.floor(Math.random() * (max - min) + min);
}
// Try to find a free port recursively
const net = require('net');
const testServer = net.createServer();
function tryNextPort() {
port = getRandomPort();
if (Number.isInteger(dynamicPort)) {
dynamicPort -= 1;
}
testServer.once('error', (err) => {
if (err.code === 'EADDRINUSE') {
// Port is in use, try another one
testServer.close();
tryNextPort();
} else {
// Only send non-port-related errors to parent
if (process.connected) {
process.send({ type: 'error', error: err.message || 'Failed to start server' });
}
}
});
testServer.once('listening', () => {
testServer.close();
boot();
});
testServer.listen(port);
}
tryNextPort();
return;
}
// Only send non-port-related errors to parent
console.error(err);
if (process.connected) {
process.send({ type: 'error', error: err.message || 'Failed to start server' });
}
}
};
function bindFinished(err) {
if (err) {
console.error(err);
// Notify parent process of the error
if (process.connected) {
process.send({ type: 'error', error: err.message || 'Failed to bind server' });
}
return;
}
console.info(`LightDB server running at port: ${port}`);
registerEndpoints();
// Notify parent process that server is ready with the URL
if (process.connected) {
const serverUrl = server.getUrl();
try {
process.send({
type: 'ready', url: serverUrl, port: port
});
console.info(`Server URL: ${serverUrl} sent to parent process`);
} catch (e) {
console.error("Failed to send ready message to parent process", e);
// If we can't send ready, we should probably exit
process.exit(1);
}
} else {
console.error("No parent process connection, shutting down.");
process.exit(1);
}
}
function boot() {
console.debug(`Trying to listen on port ${port}`);
server.listen(port, host, listenCallback);
}
boot();
server.on('listening', bindFinished);
server.on('error', listenCallback);
function registerEndpoints() {
server.getAccessControlAllowHeadersAsString = function () {
let headers = "";
let notFirst = false;
for (let header of accessControlAllowHeaders) {
if (notFirst) {
headers += ", ";
}
notFirst = true;
headers += header;
}
return headers;
}
server.use(function gracefulTerminationWatcher(req, res, next) {
if (process.shuttingDown) {
//uncaught exception was caught so server is shutting down gracefully and not accepting any requests
res.statusCode = 503;
console.log(0x02, `Rejecting ${req.url} with status code ${res.statusCode} because process is shutting down.`);
res.end();
return;
}
//if the shuttingDown flag not present, we let the request go on...
next();
});
server.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin || req.headers.host || "*");
res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, OPTIONS');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
server.put(`${urlPrefix}/executeCommand`, bodyReaderMiddleware);
const executeCommand = async (req, res) => {
let resObj = { statusCode: undefined, result: undefined, operationType: undefined };
let command = req.body;
try {
command = JSON.parse(command);
} catch (e) {
console.error("Invalid body", command);
res.statusCode = 400;
resObj.statusCode = 400;
resObj.result = "Invalid body";
return res.end(JSON.stringify(resObj));
}
try {
let pluginResult = await pluginManager.executeCommand(command);
resObj.statusCode = 200;
resObj.operationType = pluginResult.operationType;
resObj.result = pluginResult.result;
res.statusCode = 200;
} catch (e) {
res.statusCode = 500;
resObj.statusCode = 500;
console.error(e);
resObj.result = {
message: e.message,
stack: e.stack
};
}
res.end(JSON.stringify(resObj));
}
server.put(`${urlPrefix}/executeCommand`, executeCommand);
server.get(`${urlPrefix}/ready`, async (req, res) => {
let resObj = { statusCode: undefined, result: undefined };
let isInitialized = false;
try {
isInitialized = pluginManager.isInitialized();
if (isInitialized) {
resObj.statusCode = 200;
resObj.result = {
status: 'ready',
timestamp: Date.now()
};
} else {
resObj.statusCode = 200;
resObj.result = 'not-ready';
}
} catch (e) {
console.error('Error checking if plugins are initialized:', e);
res.statusCode = 500;
resObj.statusCode = 500;
resObj.result = e.message;
}
res.end(JSON.stringify(resObj));
});
server.get(`${urlPrefix}/getPublicMethods/:pluginName`, async (req, res) => {
let resObj = { statusCode: undefined, result: undefined };
let pluginName = req.params.pluginName;
if (!pluginName) {
resObj.statusCode = 400;
resObj.result = "Plugin name is required";
return res.end(JSON.stringify(resObj));
}
try {
let publicMethods = pluginManager.getPublicMethods(pluginName);
resObj.statusCode = 200;
resObj.result = publicMethods;
} catch (error) {
resObj.statusCode = 404;
resObj.result = error.message;
}
res.end(JSON.stringify(resObj));
});
}
server.getUrl = () => {
return `http://${host}:${port}${urlPrefix}`;
}
return server;
}