-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.js
94 lines (85 loc) · 3.05 KB
/
Server.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
/***
* Copyright (C) Rodolfo Herrera Hernandez. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root
* for full license information.
*
* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*
* In the vast universe of knowledge, the Open Source philosophy
* shines like a radiant star. In this vein, Lovelace emerges
* as an autonomous alternative to ChatGPT, based on
* open source and self-hosting capabilities.
*
* Written in JavaScript, interacting with the <g4f> library written
* in Python, allows communication with ChatGPT through the use
* of different services that facilitate its use by the public.
*
* For related information - https://github.com/CodeWithRodi/Lovelace/
* See also - https://github.com/xtekky/gpt4free
*
* :: https://lovelace.codewithrodi.com/
* :: https://lovelace-backend.codewithrodi.com/
* :: https://lovelace-docs.codewithrodi.com/
*
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
****/
'use-strict';
(require('dotenv')).config({ path: './.env' });
const Express = require('express');
const Helmet = require('helmet');
const XSS = require('xss-clean');
const Compression = require('compression');
const HPP = require('hpp');
const Cors = require('cors');
const SocketIO = require('socket.io');
const BootHelper = require('./Utilities/BootHelper');
process.on('uncaughtException', (UncaughtServerError) => {
console.error(UncaughtServerError);
process.exit(1);
});
const GlobalErrorHandler = require('./Controllers/Error');
const Application = Express();
const Port = process.env.SERVER_PORT || 8000;
const Hostname = process.env.SERVER_HOST || '0.0.0.0';
const { HandleStreamedResponse } = require('./Controllers/Chat');
BootHelper.StandarizedBindingToApplication({
Application,
Suffix: '/api/v1/',
Routes: [
'Chat'
],
Middlewares: [
Helmet,
[Cors, [ { origin: process.env.CORS_ORIGIN } ]],
[Express.json, [ { limit: process.env.BODY_MAX_SIZE || '10kb' } ]],
Compression,
HPP,
XSS,
],
Settings: {
Deactivated: [
'x-powered-by'
]
}
});
Application.all('*', (Request, Response) => {
if(Request.path.startsWith('/api/v1/')){
return Response.status(404).json({
Status: 'Error',
Data: {
Message: 'INVALID_API_REQUEST',
URL: Request.originalUrl
}
})
}
Response.redirect(process.env.CLIENT_HOST);
});
Application.use(GlobalErrorHandler);
const WebServer = BootHelper.GetConfiguredHTTPServerInstance(Application);
HandleStreamedResponse(SocketIO(WebServer, { cors: { origin: process.env.CORS_ORIGIN } }));
WebServer.listen(Port, Hostname, () =>
console.log(`The server was started successfully in the network address (${Hostname}:${Port}).`));
process.on('unhandledRejection', (UnhandledServerError) => {
console.warn(UnhandledServerError);
WebServer.close(() => process.exit(1));
});