mempool/backend/src/api/explorer/general.routes.ts

54 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-05-09 18:21:42 +04:00
import config from '../../config';
import { Application, Request, Response } from 'express';
2022-05-09 18:21:42 +04:00
import nodesApi from './nodes.api';
import channelsApi from './channels.api';
2022-07-01 16:50:53 +02:00
import statisticsApi from './statistics.api';
class GeneralLightningRoutes {
2022-05-09 18:21:42 +04:00
constructor() { }
public initRoutes(app: Application) {
2022-05-09 18:21:42 +04:00
app
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/search', this.$searchNodesAndChannels)
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/statistics/latest', this.$getGeneralStats)
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/statistics', this.$getStatistics)
;
2022-05-09 18:21:42 +04:00
}
private async $searchNodesAndChannels(req: Request, res: Response) {
if (typeof req.query.searchText !== 'string') {
res.status(400).send('Missing parameter: searchText');
2022-05-09 18:21:42 +04:00
return;
}
try {
const nodes = await nodesApi.$searchNodeByPublicKeyOrAlias(req.query.searchText);
const channels = await channelsApi.$searchChannelsById(req.query.searchText);
res.json({
nodes: nodes,
channels: channels,
});
} catch (e) {
res.status(500).send(e instanceof Error ? e.message : e);
}
}
2022-07-01 16:50:53 +02:00
private async $getStatistics(req: Request, res: Response) {
try {
const statistics = await statisticsApi.$getStatistics();
res.json(statistics);
} catch (e) {
res.status(500).send(e instanceof Error ? e.message : e);
}
}
private async $getGeneralStats(req: Request, res: Response) {
try {
const statistics = await statisticsApi.$getLatestStatistics();
res.json(statistics);
} catch (e) {
res.status(500).send(e instanceof Error ? e.message : e);
}
}
2022-05-09 18:21:42 +04:00
}
export default new GeneralLightningRoutes();