-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
55 lines (49 loc) · 1.84 KB
/
server.mjs
File metadata and controls
55 lines (49 loc) · 1.84 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
import https from 'https'
import http from 'http'
import configLoader from './config/index.mjs';
import express from 'express';
import loader from "./loaders/index.mjs"
import {createServer as createEventServer} from "./services/clientevents.mjs"
import Entity from 'entitystorage';
import 'dotenv/config'
import yargs from 'yargs';
import fs from 'fs';
import {resolve} from "path"
const cliArgs = yargs.argv;
async function startServer() {
const mode = yargs.argv.mode || process.env.MODE || "combined";
const config = configLoader(mode)
let storagePath = resolve(cliArgs.storage || process.env.STORAGE || "storage")
if(mode != "www"){
await Entity.init(storagePath);
}
const app = express();
try{
await loader({ app, mode, config, storagePath});
} catch(err){
console.log(err)
process.exit(-1)
}
let server;
if(process.env.HTTPS === "TRUE" || process.env.HTTPS === "YES"){
if(!process.env.PRIVATE_KEY_FILE || !process.env.CERT_FILE || !process.env.CA_FILE) throw "Missing certificate files (PRIVATE_KEY_FILE, CERT_FILE or CA_FILE) in .env"
const privateKey = fs.readFileSync(process.env.PRIVATE_KEY_FILE, 'utf8');
const certificate = fs.readFileSync(process.env.CERT_FILE, 'utf8');
const ca = fs.readFileSync(process.env.CA_FILE, 'utf8');
const credentials = {key: privateKey, cert: certificate, ca};
server = https.createServer(credentials, app)
} else {
server = http.createServer(app)
}
server.listen(config.port)
console.log(`Server listening on port: ${config.port}`);
if(mode != "www"){
const wsServer = createEventServer();
server.on('upgrade', (request, socket, head) => {
wsServer.handleUpgrade(request, socket, head, socket => {
wsServer.emit('connection', socket, request);
});
});
}
}
startServer();