event-local
Version:
Event client
123 lines • 4.85 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config();
const express = require("express");
const app = express();
const http = require("http");
const EventLocalInit_1 = require("./EventLocalInit");
const MessageLocalInit_1 = require("./MessageLocalInit");
const ApiLocalInit_1 = require("./ApiLocalInit");
const HealthLib_1 = require("./HealthLib");
const MySQLModule_1 = require("./MySQLModule");
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var os = require("os");
var ifaces = os.networkInterfaces();
class EventLocal {
constructor() {
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ("IPv4" !== iface.family || iface.internal !== false) {
return;
}
if (alias >= 1) {
console.log(ifname + ":" + alias, iface.address);
}
else {
EventLocal.localHost = iface.address;
}
++alias;
});
});
}
async onInit() {
const mysql = new MySQLModule_1.default();
await mysql.onInit();
return new Promise(async (resolve, rej) => {
let hl = new HealthLib_1.HealthLib(EventLocal.localHost, +process.env.SERVICE_PORT, +process.env.SERVICE_VERSION_API, process.env.SERVICE_NAME);
app.set("port", process.env.SERVICE_PORT);
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, access");
next();
});
app.use(express.json());
app.get("/v1/health", (req, res) => {
hl.health();
res.send("0");
});
app.get("/help", (req, res) => {
let html = "<h1>Помощь для разработчика</h1>";
if (Array.isArray(global.EVENT_CHANNEL)) {
html += "<h3>Подписка на события брокера</h3>";
for (let f of global.EVENT_CHANNEL) {
html += `<p><b>${f.event}</b> - ${f.callback}</p>`;
}
}
if (Array.isArray(global.MESSAGE_CHANNEL)) {
html += `<hr/><h3>События телеграмм команд</h3>`;
for (let f of global.MESSAGE_CHANNEL) {
html += `<p><b>${f.event.toString()}</b> - ${f.callback}</p>`;
}
}
if (Array.isArray(global.API_CHANNEL)) {
html += `<hr/><p>События WEB API</p>`;
for (let f of global.API_CHANNEL) {
html += `<p><b>[${f.method}] ${f.url} </b> - ${f.callback}</p>`;
}
}
res.send(html);
});
app.listen(app.get("port"), async () => {
console.log("[*] Сервис запущен локально: " +
EventLocal.localHost +
":" +
process.env.SERVICE_PORT);
EventLocal.broker = await EventLocalInit_1.EventLocalInit();
EventLocal.message = MessageLocalInit_1.MessageLocalInit(this);
ApiLocalInit_1.ApiLocalInit(app);
await hl.activation();
rl.on("SIGINT", async () => {
await hl.deactivation();
process.exit(0);
});
resolve(this);
});
});
}
sendEvent(q, m) {
return EventLocal.broker.emit(q, m);
}
sendMessage(chatId, m) {
return EventLocal.message.send(chatId, m);
}
sendApi(host, port, path) {
return new Promise((resolve, rej) => {
var req = http.get({
host: host,
port: port,
agent: false,
path: path,
}, function (res) {
if (res.statusCode !== 200) {
resolve(res);
}
else {
res.on("data", d => {
resolve(JSON.parse(d.toString("utf8")));
});
}
});
req.on("error", function (e) {
resolve(e);
});
});
}
}
exports.default = EventLocal;
//# sourceMappingURL=EventLocal.js.map
;