From f9c9b0d3a4195be1ce308fe2fc86e8cb4d45109a Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 5 Jan 2022 22:49:16 +0100 Subject: [PATCH] Add /.tokens debug endpoint to see token pool --- src/nitter.nim | 4 +++- src/routes/router_utils.nim | 3 +++ src/tokens.nim | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/nitter.nim b/src/nitter.nim index f6b3189..77c079a 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -9,7 +9,7 @@ import jester import types, config, prefs, formatters, redis_cache, http_pool, tokens import views/[general, about] import routes/[ - preferences, timeline, status, media, search, rss, list, + preferences, timeline, status, media, search, rss, list, debug, unsupported, embed, resolver, router_utils] const instancesUrl = "https://github.com/zedeus/nitter/wiki/Instances" @@ -49,6 +49,7 @@ createSearchRouter(cfg) createMediaRouter(cfg) createEmbedRouter(cfg) createRssRouter(cfg) +createDebugRouter(cfg) settings: port = Port(cfg.port) @@ -98,3 +99,4 @@ routes: extend status, "" extend media, "" extend embed, "" + extend debug, "" diff --git a/src/routes/router_utils.nim b/src/routes/router_utils.nim index dc8db29..4ae3f1d 100644 --- a/src/routes/router_utils.nim +++ b/src/routes/router_utils.nim @@ -42,3 +42,6 @@ template getCursor*(req: Request): string = proc getNames*(name: string): seq[string] = name.strip(chars={'/'}).split(",").filterIt(it.len > 0) + +template respJson*(body: string) = + resp body, "application/json" diff --git a/src/tokens.nim b/src/tokens.nim index da9f36a..c18b032 100644 --- a/src/tokens.nim +++ b/src/tokens.nim @@ -14,6 +14,22 @@ var tokenPool: seq[Token] lastFailed: Time +proc getPoolJson*: string = + let list = newJObject() + for token in tokenPool: + list[token.tok] = %*{ + "apis": newJObject(), + "init": $token.init, + "lastUse": $token.lastUse + } + + for api in token.apis.keys: + list[token.tok]["apis"][$api] = %*{ + "remaining": token.apis[api].remaining, + "reset": $token.apis[api].reset + } + + return $list proc rateLimitError*(): ref RateLimitError = newException(RateLimitError, "rate limited")