Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metrics): добавил роут метрики активности подключения к socket R… #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/connections/IPCServer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import RootIPC from 'node-ipc';
import logger, { IPCServerLogger } from '../logger';
import { IPCServerName, socketPath } from '../config';
import { isDebug, IPCServerName, socketPath } from '../config';
import fs from 'fs';
import SessionService from '../service/SessionService';
import MessageData from '../types/Message';
import Message from '../messages';

import http, { IncomingMessage, RequestListener, Server, ServerResponse } from 'http';
/**
* Класс IPC сервера
* отвечает за работу демонизированного процесса
Expand All @@ -30,6 +30,28 @@ export class IPCServer {
RootIPC.serve(socketPath);
this.handleEvents();
this.getCurrent().start();

if (isDebug) {
this.initHttpServer();
}
}

public initHttpServer() {
const port = 3000;
const requestHandler: RequestListener = (req: IncomingMessage, res: ServerResponse) => {
if (req.url === '/metrics') {
const isConnected = this.session.client.isConnect() ?? false;
const payload = { isConnected };
const content = JSON.stringify(payload);
console.log(`get metrics:${content}`);
res.setHeader('Content-Type', 'application/json');
res.end(content);
} else {
res.end('');
}
};
const server: Server = http.createServer(requestHandler);
server.listen(port);
}

public getCurrent() {
Expand Down