2022-04-27 02:52:23 +04:00
|
|
|
import config from '../../config';
|
2022-07-06 11:58:06 +02:00
|
|
|
import { Application, Request, Response } from 'express';
|
2022-04-27 02:52:23 +04:00
|
|
|
import nodesApi from './nodes.api';
|
2022-07-16 23:25:44 +02:00
|
|
|
import DB from '../../database';
|
|
|
|
|
2022-04-27 02:52:23 +04:00
|
|
|
class NodesRoutes {
|
2022-05-01 03:01:27 +04:00
|
|
|
constructor() { }
|
|
|
|
|
2022-07-06 11:58:06 +02:00
|
|
|
public initRoutes(app: Application) {
|
2022-04-27 02:52:23 +04:00
|
|
|
app
|
2022-07-13 00:25:40 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/country/:country', this.$getNodesPerCountry)
|
2022-07-17 22:57:29 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/isp/:isp', this.$getNodesPerISP)
|
2022-07-06 11:58:06 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/search/:search', this.$searchNode)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/top', this.$getTopNodes)
|
2022-07-17 11:46:36 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/isp', this.$getNodesISP)
|
2022-07-17 11:10:17 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/countries', this.$getNodesCountries)
|
2022-07-06 11:58:06 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key/statistics', this.$getHistoricalNodeStats)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/:public_key', this.$getNode)
|
|
|
|
;
|
2022-04-27 02:52:23 +04:00
|
|
|
}
|
|
|
|
|
2022-05-09 18:21:42 +04:00
|
|
|
private async $searchNode(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const nodes = await nodesApi.$searchNodeByPublicKeyOrAlias(req.params.search);
|
|
|
|
res.json(nodes);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 03:57:27 +04:00
|
|
|
private async $getNode(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const node = await nodesApi.$getNode(req.params.public_key);
|
|
|
|
if (!node) {
|
|
|
|
res.status(404).send('Node not found');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.json(node);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 23:19:24 +04:00
|
|
|
private async $getHistoricalNodeStats(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const statistics = await nodesApi.$getNodeStats(req.params.public_key);
|
|
|
|
res.json(statistics);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 02:52:23 +04:00
|
|
|
private async $getTopNodes(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const topCapacityNodes = await nodesApi.$getTopCapacityNodes();
|
|
|
|
const topChannelsNodes = await nodesApi.$getTopChannelsNodes();
|
|
|
|
res.json({
|
|
|
|
topByCapacity: topCapacityNodes,
|
|
|
|
topByChannels: topChannelsNodes,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 22:32:13 +02:00
|
|
|
|
2022-07-17 11:46:36 +02:00
|
|
|
private async $getNodesISP(req: Request, res: Response) {
|
2022-07-12 22:32:13 +02:00
|
|
|
try {
|
2022-07-17 11:46:36 +02:00
|
|
|
const nodesPerAs = await nodesApi.$getNodesISP();
|
2022-07-12 22:32:13 +02:00
|
|
|
res.header('Pragma', 'public');
|
|
|
|
res.header('Cache-control', 'public');
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 600).toUTCString());
|
|
|
|
res.json(nodesPerAs);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 00:25:40 +02:00
|
|
|
|
|
|
|
private async $getNodesPerCountry(req: Request, res: Response) {
|
|
|
|
try {
|
2022-07-17 09:53:02 +02:00
|
|
|
const [country]: any[] = await DB.query(
|
|
|
|
`SELECT geo_names.id, geo_names_country.names as country_names
|
|
|
|
FROM geo_names
|
|
|
|
JOIN geo_names geo_names_country on geo_names.id = geo_names_country.id AND geo_names_country.type = 'country'
|
|
|
|
WHERE geo_names.type = 'country_iso_code' AND geo_names.names = ?`,
|
|
|
|
[req.params.country]
|
|
|
|
);
|
2022-07-16 23:25:44 +02:00
|
|
|
|
2022-07-17 09:53:02 +02:00
|
|
|
if (country.length === 0) {
|
|
|
|
res.status(404).send(`This country does not exist or does not host any lightning nodes on clearnet`);
|
2022-07-16 23:25:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-17 09:53:02 +02:00
|
|
|
const nodes = await nodesApi.$getNodesPerCountry(country[0].id);
|
2022-07-13 00:25:40 +02:00
|
|
|
res.header('Pragma', 'public');
|
|
|
|
res.header('Cache-control', 'public');
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
2022-07-16 23:25:44 +02:00
|
|
|
res.json({
|
2022-07-17 09:53:02 +02:00
|
|
|
country: JSON.parse(country[0].country_names),
|
2022-07-16 23:25:44 +02:00
|
|
|
nodes: nodes,
|
|
|
|
});
|
2022-07-13 00:25:40 +02:00
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
2022-07-17 22:57:29 +02:00
|
|
|
|
|
|
|
private async $getNodesPerISP(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const [isp]: any[] = await DB.query(
|
|
|
|
`SELECT geo_names.names as isp_name
|
|
|
|
FROM geo_names
|
|
|
|
WHERE geo_names.type = 'as_organization' AND geo_names.id = ?`,
|
|
|
|
[req.params.isp]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isp.length === 0) {
|
|
|
|
res.status(404).send(`This ISP does not exist or does not host any lightning nodes on clearnet`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nodes = await nodesApi.$getNodesPerISP(req.params.isp);
|
|
|
|
res.header('Pragma', 'public');
|
|
|
|
res.header('Cache-control', 'public');
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
|
|
|
res.json({
|
|
|
|
isp: JSON.parse(isp[0].isp_name),
|
|
|
|
nodes: nodes,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
2022-07-17 11:10:17 +02:00
|
|
|
|
|
|
|
private async $getNodesCountries(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const nodesPerAs = await nodesApi.$getNodesCountries();
|
|
|
|
res.header('Pragma', 'public');
|
|
|
|
res.header('Cache-control', 'public');
|
|
|
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 600).toUTCString());
|
|
|
|
res.json(nodesPerAs);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).send(e instanceof Error ? e.message : e);
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 02:52:23 +04:00
|
|
|
}
|
|
|
|
|
2022-05-01 03:01:27 +04:00
|
|
|
export default new NodesRoutes();
|