nitter/src/nitter.nim

92 lines
2.1 KiB
Nim
Raw Normal View History

2020-11-07 20:46:03 +00:00
import asyncdispatch, strformat
2019-07-31 00:15:43 +00:00
from net import Port
2021-01-07 21:31:29 +00:00
from htmlgen import a
2019-06-20 14:16:20 +00:00
2019-09-06 00:42:35 +00:00
import jester
2019-06-24 21:25:21 +00:00
import types, config, prefs, formatters, redis_cache, http_pool, tokens
2019-09-09 02:41:20 +00:00
import views/[general, about]
2019-09-23 14:12:20 +00:00
import routes/[
preferences, timeline, status, media, search, rss, list,
2020-06-09 14:45:21 +00:00
unsupported, embed, resolver, router_utils]
2019-06-20 14:16:20 +00:00
2021-01-07 21:31:29 +00:00
const instancesUrl = "https://github.com/zedeus/nitter/wiki/Instances"
2019-07-31 00:15:43 +00:00
const configPath {.strdefine.} = "./nitter.conf"
let (cfg, fullCfg) = getConfig(configPath)
when defined(release):
2020-11-07 22:16:33 +00:00
import logging
# Silence Jester's query warning
addHandler(newConsoleLogger())
setLogFilter(lvlError)
2020-06-07 07:18:40 +00:00
2021-01-08 01:25:43 +00:00
stdout.write &"Starting Nitter at {getUrlPrefix(cfg)}\n"
2020-06-07 07:18:40 +00:00
stdout.flushFile
updateDefaultPrefs(fullCfg)
2020-06-01 00:25:39 +00:00
setCacheTimes(cfg)
setHmacKey(cfg.hmacKey)
2020-06-09 13:04:38 +00:00
setProxyEncoding(cfg.base64Media)
setMaxHttpConns(100)
2020-06-01 11:36:10 +00:00
waitFor initRedisPool(cfg)
2020-06-07 07:18:40 +00:00
stdout.write &"Connected to Redis at {cfg.redisHost}:{cfg.redisPort}\n"
stdout.flushFile
2020-06-01 00:25:39 +00:00
asyncCheck initTokenPool(cfg)
2019-09-23 14:12:20 +00:00
createUnsupportedRouter(cfg)
createResolverRouter(cfg)
2019-09-06 00:42:35 +00:00
createPrefRouter(cfg)
createTimelineRouter(cfg)
2019-09-20 23:08:30 +00:00
createListRouter(cfg)
2019-09-20 12:10:10 +00:00
createStatusRouter(cfg)
2019-09-13 20:24:58 +00:00
createSearchRouter(cfg)
2019-09-06 00:42:35 +00:00
createMediaRouter(cfg)
2019-12-06 14:15:56 +00:00
createEmbedRouter(cfg)
createRssRouter(cfg)
2019-07-31 00:15:43 +00:00
settings:
port = Port(cfg.port)
staticDir = cfg.staticDir
bindAddr = cfg.address
2019-06-20 14:16:20 +00:00
routes:
get "/":
2020-06-09 14:45:21 +00:00
resp renderMain(renderSearch(), request, cfg, themePrefs())
2019-06-20 14:16:20 +00:00
2019-09-09 02:41:20 +00:00
get "/about":
2020-06-09 14:45:21 +00:00
resp renderMain(renderAbout(), request, cfg, themePrefs())
2019-09-09 02:41:20 +00:00
2019-09-23 14:12:20 +00:00
get "/explore":
redirect("/about")
2019-09-23 14:12:20 +00:00
get "/help":
redirect("/about")
2019-10-17 22:45:54 +00:00
get "/i/redirect":
let url = decodeUrl(@"url")
2019-10-21 05:59:22 +00:00
if url.len == 0: resp Http404
2019-10-17 22:45:54 +00:00
redirect(replaceUrl(url, cookiePrefs()))
error Http404:
2019-10-21 05:59:22 +00:00
resp Http404, showError("Page not found", cfg)
2021-01-07 21:31:29 +00:00
error RateLimitError:
echo error.exc.msg
resp Http429, showError("Instance has been rate limited.<br>Use " &
a("another instance", href = instancesUrl) &
" or try again later.", cfg)
2019-09-23 14:12:20 +00:00
extend unsupported, ""
2019-09-06 00:42:35 +00:00
extend preferences, ""
extend resolver, ""
extend rss, ""
2019-09-13 20:24:58 +00:00
extend search, ""
2019-09-06 00:42:35 +00:00
extend timeline, ""
2019-09-20 23:08:30 +00:00
extend list, ""
2019-09-20 12:10:10 +00:00
extend status, ""
2019-09-06 00:42:35 +00:00
extend media, ""
2019-12-06 14:15:56 +00:00
extend embed, ""