-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathATON.service.main.js
218 lines (166 loc) · 5.12 KB
/
ATON.service.main.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
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
/*!
@preserve
ATON Main Service (gateway)
@author Bruno Fanini
VHLab, CNR ISPC
==================================================================================*/
const fs = require('fs');
const express = require('express');
const http = require('http');
const https = require('https');
const url = require('url');
//const compression = require('compression');
const path = require('path');
const cors = require('cors');
const chalk = require('chalk');
const glob = require("glob");
const nanoid = require("nanoid");
const { createProxyMiddleware } = require('http-proxy-middleware');
const Core = require('./Core');
const Auth = require('./Auth');
const Render = require('./Render');
const API = require("./API/v2"); // v2
// Initialize & load config files
Core.init();
const CONF = Core.config;
// Standard PORTS
let PORT = 8080;
let PORT_SECURE = 8083;
let VRC_PORT = 8890;
let VRC_ADDR = "ws://localhost";
let PORT_WEBDAV = 8081;
if (CONF.services.main.PORT)
PORT = CONF.services.main.PORT;
if (process.env.PORT)
PORT = process.env.PORT;
if (CONF.services.main.PORT_S)
PORT_SECURE = CONF.services.main.PORT_S;
if (CONF.services.photon){
if (CONF.services.photon.PORT) VRC_PORT = CONF.services.photon.PORT;
if (CONF.services.photon.address) VRC_ADDR = CONF.services.photon.address;
}
// compatibility with previous configs
if (CONF.services.vroadcast){
if (CONF.services.vroadcast.PORT) VRC_PORT = CONF.services.vroadcast.PORT;
if (CONF.services.vroadcast.address) VRC_ADDR = CONF.services.vroadcast.address;
}
if (CONF.services.webdav && CONF.services.webdav.PORT)
PORT_WEBDAV = CONF.services.webdav.PORT;
const pathCert = Core.getCertPath();
const pathKey = Core.getKeyPath();
let bExamples = CONF.services.main.examples;
//let bAPIdoc = CONF.services.main.apidoc;
// Debug on req received (client)
let logger = function(req, res, next){
console.log('Request from: ' + req.ip + ' For: ' + req.path);
next(); // Run the next handler
};
let app = express();
//app.set('trust proxy', 1); // trust first proxy
//app.use(compression());
app.use(cors({
credentials: true,
origin: true
}));
/*
app.use((req, res, next)=>{
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
*/
app.use(express.json({ limit: '50mb' }));
// Scenes redirect /s/<sid>
/*
app.get(/^\/s\/(.*)$/, function(req,res,next){
let sid = req.params[0];
//req.url = "/fe";
//req.query.s = sid;
res.redirect(url.format({
pathname:"/fe",
query: { "s": sid }
}));
next();
});
*/
// Data routing (advanced)
//Core.setupDataRoute(app);
const CACHING_OPT = {
maxage: "3h"
};
app.use('/', express.static(Core.DIR_PUBLIC, CACHING_OPT ));
// Official front-end (Hathor)
//app.use('/fe', express.static(Core.DIR_FE));
// Common public resources (config/public/)
if (fs.existsSync(Core.DIR_CONFIGPUB)) app.use('/common', express.static(Core.DIR_CONFIGPUB));
// Web-apps
app.use('/a', express.static(Core.DIR_WAPPS));
// Data (static)
app.use('/', express.static(Core.DIR_DATA, CACHING_OPT));
// Setup authentication
Auth.init(app);
// REST API
Core.realizeBaseAPI(app); // v1 (for backward compatibility)
API.init( app ); // v2
// Rendering
Core.Render.setup(app);
// Micro-services proxies
//=================================================
// Photon (previously VRoadcast)
app.use('/vrc', createProxyMiddleware({
target: VRC_ADDR+":"+VRC_PORT,
ws: true,
pathRewrite: { '^/vrc': ''},
changeOrigin: true
}));
app.use('/svrc', createProxyMiddleware({
target: VRC_ADDR+":"+VRC_PORT,
ws: true,
pathRewrite: { '^/svrc': ''},
secure: true,
changeOrigin: true
}));
// WebDav
/*
app.use('/dav', createProxyMiddleware({
//target: CONF.services.webdav.address+":"+PORT_WEBDAV,
target: "http://localhost:"+PORT_WEBDAV,
pathRewrite: { '^/dav': ''},
changeOrigin: false, //true,
//xfwd: true,
//secure: true,
//router: { "/dav" : "http://localhost:"+PORT_WEBDAV }
}));
*/
// Collect & setup flares (if found)
//==================================
Core.setupFlares(app);
for (let fid in Core.flares){
//let fid = Core.flares[f];
app.use('/flares/'+fid, express.static(Core.DIR_FLARES+fid+"/public/"));
}
// START
//==================================
http.createServer(app).listen(PORT, ()=>{
Core.logGreen("\nATON up and running!");
console.log("- OFFLINE: http://localhost:"+PORT);
for (let n in Core.nets) console.log("- NETWORK ('"+n+"'): http://"+Core.nets[n][0]+":"+PORT);
console.log("\n");
});
// HTTPS service
if (fs.existsSync(pathCert) && fs.existsSync(pathKey)){
let httpsOptions = {
key: fs.readFileSync(pathKey, 'utf8'),
cert: fs.readFileSync(pathCert, 'utf8')
};
https.createServer(httpsOptions, app).listen(PORT_SECURE, ()=>{
Core.logGreen("\nHTTPS ATON up and running!");
console.log("- OFFLINE: https://localhost:"+PORT_SECURE);
for (let n in Core.nets) console.log("- NETWORK ('"+n+"'): https://"+Core.nets[n][0]+":"+PORT_SECURE);
console.log("\n");
});
}
else {
console.log("\nSSL certs not found:\n"+pathKey+"\n"+pathCert);
console.log("\n");
}