diff --git a/backend/src/api/explorer/nodes.api.ts b/backend/src/api/explorer/nodes.api.ts index c3b3f8124..4abad3e4e 100644 --- a/backend/src/api/explorer/nodes.api.ts +++ b/backend/src/api/explorer/nodes.api.ts @@ -124,6 +124,21 @@ class NodesApi { throw e; } } + + public async $getNodesPerCountry(country: string) { + try { + const query = `SELECT nodes.* FROM nodes + JOIN geo_names ON geo_names.id = nodes.country_id + WHERE LOWER(json_extract(names, '$.en')) = ? + `; + + const [rows]: any = await DB.query(query, [`"${country}"`]); + return rows; + } catch (e) { + logger.err(`Cannot get nodes for country ${country}. Reason: ${e instanceof Error ? e.message : e}`); + throw e; + } + } } export default new NodesApi(); diff --git a/backend/src/api/explorer/nodes.routes.ts b/backend/src/api/explorer/nodes.routes.ts index d2960155b..07986f0b1 100644 --- a/backend/src/api/explorer/nodes.routes.ts +++ b/backend/src/api/explorer/nodes.routes.ts @@ -6,6 +6,7 @@ class NodesRoutes { public initRoutes(app: Application) { app + .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/country/:country', this.$getNodesPerCountry) .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/search/:search', this.$searchNode) .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/top', this.$getTopNodes) .get(config.MEMPOOL.API_URL_PREFIX + 'lightning/nodes/asShare', this.$getNodesAsShare) @@ -69,6 +70,18 @@ class NodesRoutes { res.status(500).send(e instanceof Error ? e.message : e); } } + + private async $getNodesPerCountry(req: Request, res: Response) { + try { + const nodes = await nodesApi.$getNodesPerCountry(req.params.country.toLowerCase()); + res.header('Pragma', 'public'); + res.header('Cache-control', 'public'); + res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString()); + res.json(nodes); + } catch (e) { + res.status(500).send(e instanceof Error ? e.message : e); + } + } } export default new NodesRoutes();