[t-002] Create config.py — Pydantic YAML loader #2

Open
opened 2026-07-05 08:30:58 +00:00 by david · 0 comments
Owner

Goal

Implement the configuration loading system using Pydantic models backed by YAML.

References

Reference

  • DESIGN.md §4 Config section (~/.keryx/config.yaml structure)
  • AGENT.md §11 Config & Deployment section

Implementation Steps

Implementation Steps

1. Create src/core/config.py

Create a complete config module with:

# src/core/config.py
from pathlib import Path
from pydantic import BaseModel, Field
import yaml

class AppConfig(BaseModel):
    name: str = "Keryx"
    version: str = "0.1.0"
    supported_app_versions: str = "^1.0.0"

class ServerConfig(BaseModel):
    host: str = "0.0.0.0"
    port: int = 8643
    api_key: str = Field(default="keryx-bff-change-me")

class HermesConfig(BaseModel):
    api_url: str = "http://127.0.0.1:8642"
    api_key: str = Field(default="hermes-api-server-key")
    workspace_path: str = "/home/david/workspace"

class BffConfig(BaseModel):
    staging_dir: str = "/tmp/keryx"
    db_path: str = "~/.keryx/keryx.db"
    max_upload_size_mb: int = 50
    staging_cleanup_days: int = 30

class FullConfig(BaseModel):
    app: AppConfig
    server: ServerConfig
    hermes: HermesConfig
    bff: BffConfig

_config_path_env_key = "KERYX_CONFIG"
_default_config_path = Path.home() / ".keryx" / "config.yaml"

def load_config(path: Path | None = None) -> FullConfig:
    config_path = path or (Path.environ.get(_config_path_env_key, str(_default_config_path)))
    text = config_path.read_text(encoding="utf-8")
    data = yaml.safe_load(text)
    return FullConfig(**data)

2. Create a default config.yaml for development

Create ~/.keryx/config.yaml with the exact values from DESIGN.md §4:

app:
  name: "Keryx"
  version: "0.1.0"
  supported_app_versions: "^1.0.0"
server:
  host: "0.0.0.0"
  port: 8643
  api_key: "keryx-bff-change-me"
hermes:
  api_url: "http://127.0.0.1:8642"
  api_key: "hermes-api-server-key"
  workspace_path: "/home/david/workspace"
bff:
  staging_dir: "/tmp/keryx"
  db_path: "/home/david/.keryx/keryx.db"
  max_upload_size_mb: 50
  staging_cleanup_days: 30

Verification Gate

Verification Gate

Run:

cd /home/david/Projects/keryx-bff && uv run python -c "from src.core.config import load_config; c = load_config(); print(c.app.name, c.server.port, c.hermes.api_url)"
# Expected output: Keryx 8643 http://127.0.0.1:8642

STOP Conditions

STOP Conditions

  • If the config YAML cannot be parsed by yaml.safe_load, stop and report the parse error.

Depends on: t-001

## Goal Implement the configuration loading system using Pydantic models backed by YAML. ## References ## Reference - DESIGN.md §4 Config section (`~/.keryx/config.yaml` structure) - AGENT.md §11 Config & Deployment section ## Implementation Steps ## Implementation Steps ### 1. Create `src/core/config.py` Create a complete config module with: ```python # src/core/config.py from pathlib import Path from pydantic import BaseModel, Field import yaml class AppConfig(BaseModel): name: str = "Keryx" version: str = "0.1.0" supported_app_versions: str = "^1.0.0" class ServerConfig(BaseModel): host: str = "0.0.0.0" port: int = 8643 api_key: str = Field(default="keryx-bff-change-me") class HermesConfig(BaseModel): api_url: str = "http://127.0.0.1:8642" api_key: str = Field(default="hermes-api-server-key") workspace_path: str = "/home/david/workspace" class BffConfig(BaseModel): staging_dir: str = "/tmp/keryx" db_path: str = "~/.keryx/keryx.db" max_upload_size_mb: int = 50 staging_cleanup_days: int = 30 class FullConfig(BaseModel): app: AppConfig server: ServerConfig hermes: HermesConfig bff: BffConfig _config_path_env_key = "KERYX_CONFIG" _default_config_path = Path.home() / ".keryx" / "config.yaml" def load_config(path: Path | None = None) -> FullConfig: config_path = path or (Path.environ.get(_config_path_env_key, str(_default_config_path))) text = config_path.read_text(encoding="utf-8") data = yaml.safe_load(text) return FullConfig(**data) ``` ### 2. Create a default config.yaml for development Create `~/.keryx/config.yaml` with the exact values from DESIGN.md §4: ```yaml app: name: "Keryx" version: "0.1.0" supported_app_versions: "^1.0.0" server: host: "0.0.0.0" port: 8643 api_key: "keryx-bff-change-me" hermes: api_url: "http://127.0.0.1:8642" api_key: "hermes-api-server-key" workspace_path: "/home/david/workspace" bff: staging_dir: "/tmp/keryx" db_path: "/home/david/.keryx/keryx.db" max_upload_size_mb: 50 staging_cleanup_days: 30 ``` ## Verification Gate ## Verification Gate Run: ```bash cd /home/david/Projects/keryx-bff && uv run python -c "from src.core.config import load_config; c = load_config(); print(c.app.name, c.server.port, c.hermes.api_url)" # Expected output: Keryx 8643 http://127.0.0.1:8642 ``` ## STOP Conditions ## STOP Conditions - If the config YAML cannot be parsed by yaml.safe_load, stop and report the parse error. Depends on: t-001
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: david/keryx-bff#2
No description provided.