mcopy/app/lib/server/index.js

81 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-02-25 05:24:07 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2023-02-25 05:24:07 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
2023-02-25 05:24:07 +00:00
const promises_1 = require("fs/promises");
const Log = require("log");
class Server {
2023-02-25 05:24:07 +00:00
constructor() {
this.id = 'server';
this.isActive = false;
this.templates = [
{
name: 'index',
path: 'server.html'
},
{
name: 'script',
path: 'lib/client/index.js'
}
];
this.port = 9900;
this.wsPort = 9901;
this.init();
}
async init() {
this.log = await Log({ label: this.id });
await this.load();
await this.start();
2023-02-25 05:24:07 +00:00
}
async load() {
this.http = express_1.default();
2023-02-25 05:24:07 +00:00
for (let tmpl of this.templates) {
tmpl.data = await promises_1.readFile(tmpl.path, 'utf8');
}
this.http.get('/', this.index.bind(this));
this.http.get('/client.js', this.script.bind(this));
2023-02-25 05:24:07 +00:00
this.log.info("Server assets loaded");
}
template(name, data) {
let html = this.templates.find(el => el.name === name).data;
for (let key of Object.keys(data)) {
html = html.replace(`{{${key}}}`, data[key]);
}
return html;
}
async start() {
return new Promise(function (resolve, reject) {
this.httpd = this.http.listen(this.port, function () {
this.isActive = true;
this.log.info(`Server started!`);
this.log.info(`URL [ http://localhost:${this.port} ]`);
return resolve(true);
}.bind(this));
2023-02-25 05:24:07 +00:00
}.bind(this));
}
stop() {
return new Promise(function (resolve, reject) {
return this.httpd.close(function () {
this.isActive = false;
this.log.info(`Server stopped :(`);
}.bind(this));
}.bind(this));
2023-02-25 05:24:07 +00:00
}
index(req, res, next) {
const html = this.template('index', { PORT: `${this.port}` });
this.log.info('GET /');
2023-02-25 05:24:07 +00:00
return res.send(html);
}
script(req, res, next) {
const js = this.template('script', { PORT: `${this.wsPort}` });
res.contentType('text/javascript');
this.log.info('GET /script.js');
return res.send(js);
2023-02-25 05:24:07 +00:00
}
}
module.exports = function () {
return new Server();
};
2023-02-25 05:24:07 +00:00
//# sourceMappingURL=index.js.map