From e00dee501b72b5b639df1f3025fccc47b65beb02 Mon Sep 17 00:00:00 2001 From: jackyzy823 Date: Mon, 15 May 2023 14:51:57 +0800 Subject: [PATCH] Implement config from env --- README.md | 14 +++++++++++--- nitter.example.conf | 2 ++ src/config.nim | 8 +++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4f8235d..68e9182 100644 --- a/README.md +++ b/README.md @@ -100,9 +100,10 @@ $ cp nitter.example.conf nitter.conf ``` Set your hostname, port, HMAC key, https (must be correct for cookies), and -Redis info in `nitter.conf`. To run Redis, either run -`redis-server --daemonize yes`, or `systemctl enable --now redis` (or -redis-server depending on the distro). Run Nitter by executing `./nitter` or +Redis info in `nitter.conf` or Environment Variables in format of +`NITTER_SECTION_KEY=xxxx` in upper case (for example: NITTER_SERVER_HOSTNAME=nitter.net). +To run Redis, either run `redis-server --daemonize yes`, or `systemctl enable --now redis` +(or redis-server depending on the distro). Run Nitter by executing `./nitter` or using the systemd service below. You should run Nitter behind a reverse proxy such as [Nginx](https://github.com/zedeus/nitter/wiki/Nginx) or [Apache](https://github.com/zedeus/nitter/wiki/Apache) for security and @@ -125,6 +126,13 @@ docker build -t nitter:latest . docker run -v $(pwd)/nitter.conf:/src/nitter.conf -d --network host nitter:latest ``` +or + +```bash +docker build -t nitter:latest . +docker run -e NITTER_SERVER_ADDRESS=0.0.0.0 -e NITTER_SERVER_PORT=8080 -d --network host nitter:latest +``` + Note: For ARM64, use this Dockerfile: [`Dockerfile.arm64`](https://github.com/zedeus/nitter/blob/master/Dockerfile.arm64). A prebuilt Docker image is provided as well: diff --git a/nitter.example.conf b/nitter.example.conf index 0d4deb7..956234a 100644 --- a/nitter.example.conf +++ b/nitter.example.conf @@ -1,3 +1,5 @@ +# Config can be set via Environment Variables in format of `NITTER_SECTION_KEY`. +# Environment Variables override nitter.conf. [Server] hostname = "nitter.net" # for generating links, change this to your own domain/ip title = "nitter" diff --git a/src/config.nim b/src/config.nim index 1b05ffe..e73ffb4 100644 --- a/src/config.nim +++ b/src/config.nim @@ -1,9 +1,11 @@ # SPDX-License-Identifier: AGPL-3.0-only import parsecfg except Config -import types, strutils +import types, strutils, std/os proc get*[T](config: parseCfg.Config; section, key: string; default: T): T = - let val = config.getSectionValue(section, key) + let envKey = "NITTER_" & section.toUpperAscii & "_" & key.toUpperAscii + let envVal = getEnv(envKey) + let val = if envVal.len == 0: config.getSectionValue(section, key) else: envVal if val.len == 0: return default when T is int: parseInt(val) @@ -11,7 +13,7 @@ proc get*[T](config: parseCfg.Config; section, key: string; default: T): T = elif T is string: val proc getConfig*(path: string): (Config, parseCfg.Config) = - var cfg = loadConfig(path) + var cfg = try: loadConfig(path) except IOError: parseCfg.Config() let conf = Config( # Server