Skip to content

Commit 5cb705a

Browse files
change configuration of config and types to make the parent key optional, not the subkeys (#46)
1 parent ba8f937 commit 5cb705a

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

config.template.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ maintenance = false
88
[DATABASE]
99
dsn = "postgres://mystbin:mystbin@database:5432/mystbin"
1010

11-
[REDIS]
12-
limiter = "redis://redis:6379/0" # Optional
13-
sessions = "redis://redis:6379/1" # Optional
14-
1511
[LIMITS]
1612
paste_get = { rate = 30, per = 60, priority = 1, bucket = "ip" }
1713
paste_get_day = { rate = 7200, per = 86400, priority = 2, bucket = "ip" }
@@ -24,6 +20,10 @@ char_limit = 300_000
2420
file_limit = 5
2521
name_limit = 25
2622

23+
[REDIS] # optional key
24+
limiter = "redis://redis:6379/0" # required if key present
25+
sessions = "redis://redis:6379/1" # required if key present
26+
2727
[GITHUB] # optional key
2828
token = "..." # a github token capable of creating gists, non-optional if the above key is provided
2929
timeout = 10 # how long to wait between posting gists if there's an influx of tokens posted. Non-optional

core/server.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,15 @@ def __init__(self, *, database: Database, session: aiohttp.ClientSession | None
4747
]
4848
routes: list[Mount | Route] = [Mount("/static", app=StaticFiles(directory="web/static"), name="static")]
4949

50-
limit_redis = starlette_plus.Redis(url=CONFIG["REDIS"]["limiter"]) if CONFIG["REDIS"]["limiter"] else None
51-
sess_redis = starlette_plus.Redis(url=CONFIG["REDIS"]["sessions"]) if CONFIG["REDIS"]["sessions"] else None
50+
if redis_key := CONFIG.get("REDIS"):
51+
limit_url = redis_key["limiter"]
52+
session_url = redis_key["sessions"]
53+
else:
54+
limit_url = None
55+
session_url = None
56+
57+
limit_redis = starlette_plus.Redis(url=limit_url)
58+
sess_redis = starlette_plus.Redis(url=session_url)
5259

5360
global_limits = [CONFIG["LIMITS"]["global_limit"]]
5461
middleware = [

types_/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Github(TypedDict):
6060
class Config(TypedDict):
6161
SERVER: Server
6262
DATABASE: Database
63-
REDIS: Redis
63+
REDIS: NotRequired[Redis]
6464
LIMITS: Limits
6565
PASTES: Pastes
6666
GITHUB: NotRequired[Github]

0 commit comments

Comments
 (0)