2021-04-03 17:46:54 +00:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
2021-06-05 11:58:50 +00:00
|
|
|
var _a, _b;
|
2021-04-03 17:46:54 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-05-23 20:42:02 +00:00
|
|
|
exports.wss = exports.app = void 0;
|
2021-04-03 17:46:54 +00:00
|
|
|
const http_1 = __importDefault(require("http"));
|
|
|
|
const express_1 = __importDefault(require("express"));
|
2021-05-23 20:42:02 +00:00
|
|
|
const ws_1 = require("ws");
|
2021-06-05 11:58:50 +00:00
|
|
|
const yargs_1 = __importDefault(require("yargs"));
|
2021-04-03 17:46:54 +00:00
|
|
|
const debug_1 = __importDefault(require("debug"));
|
2021-06-05 11:58:50 +00:00
|
|
|
const helpers_1 = require("yargs/helpers");
|
2021-04-03 17:46:54 +00:00
|
|
|
const middlewares_1 = require("./middlewares");
|
|
|
|
const routes_1 = __importDefault(require("./routes"));
|
|
|
|
const port_1 = require("./helpers/port");
|
|
|
|
const server_callbacks_1 = require("./helpers/server-callbacks");
|
|
|
|
const register_1 = require("./routes/api/register");
|
2021-05-23 20:42:02 +00:00
|
|
|
const websocket_1 = require("./websocket");
|
2021-06-05 11:58:50 +00:00
|
|
|
const errors_1 = require("./helpers/errors");
|
|
|
|
// TODO: Remove type assertion while keeping correct types
|
|
|
|
const argv = yargs_1.default(helpers_1.hideBin(process.argv)).options({
|
|
|
|
port: { type: 'string', default: '6595' },
|
2021-06-29 13:58:25 +00:00
|
|
|
host: { type: 'string', default: '127.0.0.1' }
|
2021-06-05 11:58:50 +00:00
|
|
|
}).argv;
|
2021-06-29 13:58:25 +00:00
|
|
|
const DEEMIX_PORT = port_1.normalizePort((_a = process.env.DEEMIX_PORT) !== null && _a !== void 0 ? _a : argv.port);
|
|
|
|
const DEEMIX_HOST = (_b = process.env.DEEMIX_HOST) !== null && _b !== void 0 ? _b : argv.host;
|
2021-04-03 17:46:54 +00:00
|
|
|
const debug = debug_1.default('deemix-gui:server');
|
2021-05-23 20:42:02 +00:00
|
|
|
exports.app = express_1.default();
|
|
|
|
exports.wss = new ws_1.Server({ noServer: true });
|
|
|
|
const server = http_1.default.createServer(exports.app);
|
2021-04-03 17:46:54 +00:00
|
|
|
/* === Middlewares === */
|
2021-05-23 20:42:02 +00:00
|
|
|
middlewares_1.registerMiddlewares(exports.app);
|
2021-04-03 17:46:54 +00:00
|
|
|
/* === Routes === */
|
2021-05-23 20:42:02 +00:00
|
|
|
exports.app.use('/', routes_1.default);
|
2021-04-03 17:46:54 +00:00
|
|
|
/* === APIs === */
|
2021-05-23 20:42:02 +00:00
|
|
|
register_1.registerApis(exports.app);
|
2021-04-03 17:46:54 +00:00
|
|
|
/* === Config === */
|
2021-06-05 11:58:50 +00:00
|
|
|
exports.app.set('port', DEEMIX_PORT);
|
2021-04-03 17:46:54 +00:00
|
|
|
/* === Server port === */
|
2021-05-23 20:42:02 +00:00
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
2021-06-05 11:58:50 +00:00
|
|
|
server.listen({ port: DEEMIX_PORT, host: DEEMIX_HOST });
|
2021-05-23 20:42:02 +00:00
|
|
|
}
|
|
|
|
websocket_1.registerWebsocket(exports.wss);
|
2021-04-03 17:46:54 +00:00
|
|
|
/* === Server callbacks === */
|
2021-06-05 11:58:50 +00:00
|
|
|
exports.app.on('mount', a => {
|
|
|
|
console.log(a);
|
|
|
|
});
|
|
|
|
server.on('connect', () => {
|
|
|
|
errors_1.consoleInfo('Server connected');
|
|
|
|
});
|
2021-05-23 20:42:02 +00:00
|
|
|
server.on('upgrade', (request, socket, head) => {
|
|
|
|
exports.wss.handleUpgrade(request, socket, head, socket => {
|
|
|
|
exports.wss.emit('connection', socket, request);
|
|
|
|
});
|
|
|
|
});
|
2021-06-05 11:58:50 +00:00
|
|
|
server.on('error', server_callbacks_1.getErrorCb(DEEMIX_PORT));
|
2021-04-03 17:46:54 +00:00
|
|
|
server.on('listening', server_callbacks_1.getListeningCb(server, debug));
|