Files
kaya-rbcs/src/kaya_rbcs/config.py
T
woggioni-opencode-agent bdd7a791e4
CI / Build and push docker image (push) Successful in 2m57s
Implement kaya-rbcs: simplified RBCS clone on the Kaya framework
- KayaApp with GET/PUT recursive routes backed by memcached (aiomcache)
- env-based configuration, metadata-aware value encoding, key prefix/digest
- Granian RSGI launcher (rbcs-server / python -m kaya_rbcs)
- pinned requirements via pip-compile, alpine:3.24 Dockerfile with healthcheck
- offline tests using an in-process fake memcached
- Gitea Actions workflow to build and push the multi-arch Docker image
2026-08-02 15:25:25 +00:00

45 lines
1.4 KiB
Python

from dataclasses import dataclass, field
from datetime import timedelta
from os import getenv
def _parse_max_age(raw: str) -> timedelta:
if raw.endswith('s'):
return timedelta(seconds=int(raw[:-1]))
if raw.endswith('m'):
return timedelta(minutes=int(raw[:-1]))
if raw.endswith('h'):
return timedelta(hours=int(raw[:-1]))
if raw.endswith('d'):
return timedelta(days=int(raw[:-1]))
return timedelta(seconds=int(raw))
@dataclass(frozen=True)
class Config:
host: str = '127.0.0.1'
port: int = 8080
path_prefix: str = '/'
memcache_host: str = '127.0.0.1'
memcache_port: int = 11211
key_prefix: str | None = None
digest: str | None = None
max_age: timedelta = field(default_factory=lambda: timedelta(days=7))
@staticmethod
def from_env() -> 'Config':
def get_int(name: str, default: int) -> int:
raw = getenv(name)
return int(raw) if raw is not None else default
return Config(
host=getenv('RBCS_HOST', '127.0.0.1'),
port=get_int('RBCS_PORT', 8080),
path_prefix=getenv('RBCS_PATH_PREFIX', '/'),
memcache_host=getenv('RBCS_MEMCACHE_HOST', '127.0.0.1'),
memcache_port=get_int('RBCS_MEMCACHE_PORT', 11211),
key_prefix=getenv('RBCS_KEY_PREFIX'),
digest=getenv('RBCS_DIGEST'),
max_age=_parse_max_age(getenv('RBCS_MAX_AGE', '7d')),
)