deemixer/server/dist/app.js

59 lines
2.4 KiB
JavaScript
Raw Normal View History

"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;
Object.defineProperty(exports, "__esModule", { value: true });
2021-05-23 20:42:02 +00:00
exports.wss = exports.app = void 0;
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"));
const debug_1 = __importDefault(require("debug"));
2021-06-05 11:58:50 +00:00
const helpers_1 = require("yargs/helpers");
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' },
host: { type: 'string', default: 'localhost' }
}).argv;
const DEEMIX_PORT = port_1.normalizePort((_a = process.env.PORT) !== null && _a !== void 0 ? _a : argv.port);
const DEEMIX_HOST = (_b = process.env.HOST) !== null && _b !== void 0 ? _b : argv.host;
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);
/* === Middlewares === */
2021-05-23 20:42:02 +00:00
middlewares_1.registerMiddlewares(exports.app);
/* === Routes === */
2021-05-23 20:42:02 +00:00
exports.app.use('/', routes_1.default);
/* === APIs === */
2021-05-23 20:42:02 +00:00
register_1.registerApis(exports.app);
/* === Config === */
2021-06-05 11:58:50 +00:00
exports.app.set('port', DEEMIX_PORT);
/* === 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);
/* === 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));
server.on('listening', server_callbacks_1.getListeningCb(server, debug));