refactor: Migrate CLI and API components to use castle-core for configuration and manifest handling

This commit is contained in:
2026-02-22 22:54:38 -08:00
parent 6d0332e32b
commit 41d4c574cb
31 changed files with 1699 additions and 904 deletions

View File

@@ -8,11 +8,11 @@ dependencies = [
"uvicorn>=0.34.0",
"pydantic-settings>=2.0.0",
"httpx>=0.27.0",
"castle-cli",
"castle-core",
]
[tool.uv.sources]
castle-cli = { path = "../cli", editable = true }
castle-core = { workspace = true }
[project.scripts]
castle-api = "castle_api.main:run"

View File

@@ -9,8 +9,8 @@ import yaml
from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel
from castle_cli.config import load_config, save_config
from castle_cli.manifest import ComponentManifest
from castle_core.config import load_config, save_config
from castle_core.manifest import ComponentManifest
from castle_api.config import settings
from castle_api.stream import broadcast
@@ -148,12 +148,12 @@ async def apply_config() -> ApplyResponse:
errors.append(f"Failed to restart {name}: {output}")
# Reload gateway
from castle_cli.commands.gateway import _generate_caddyfile
from castle_cli.config import GENERATED_DIR, ensure_dirs
from castle_core.config import GENERATED_DIR, ensure_dirs
from castle_core.generators import generate_caddyfile
ensure_dirs()
caddyfile_path = GENERATED_DIR / "Caddyfile"
caddyfile_path.write_text(_generate_caddyfile(config))
caddyfile_path.write_text(generate_caddyfile(config))
actions.append("Generated Caddyfile")
if shutil.which("caddy"):

View File

@@ -7,7 +7,7 @@ import time
import httpx
from castle_cli.config import CastleConfig
from castle_core.config import CastleConfig
from castle_api.models import HealthStatus

View File

@@ -8,7 +8,7 @@ from collections.abc import AsyncGenerator
from fastapi import APIRouter, HTTPException, Query, status
from starlette.responses import StreamingResponse
from castle_cli.config import load_config
from castle_core.config import load_config
from castle_api.config import settings

View File

@@ -7,7 +7,7 @@ from pathlib import Path
from fastapi import APIRouter, HTTPException, status
from castle_cli.config import load_config
from castle_core.config import load_config
from castle_api.config import settings
from castle_api.health import check_all_health
@@ -137,7 +137,7 @@ def get_gateway() -> GatewayInfo:
@router.get("/gateway/caddyfile")
def get_caddyfile() -> dict[str, str]:
"""Return the generated Caddyfile content."""
from castle_cli.commands.gateway import _generate_caddyfile
from castle_core.generators import generate_caddyfile
config = load_config(settings.castle_root)
return {"content": _generate_caddyfile(config)}
return {"content": generate_caddyfile(config)}

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel
from castle_cli.config import SECRETS_DIR
from castle_core.config import SECRETS_DIR
router = APIRouter(prefix="/secrets", tags=["secrets"])

View File

@@ -8,7 +8,7 @@ import time
from fastapi import APIRouter, HTTPException, status
from starlette.responses import JSONResponse
from castle_cli.config import load_config
from castle_core.config import load_config
from castle_api.config import settings
from castle_api.health import check_all_health
@@ -115,13 +115,13 @@ async def _do_action(name: str, action: str) -> JSONResponse:
@router.get("/{name}/unit")
def get_unit(name: str) -> dict[str, str | None]:
"""Return the generated systemd unit file(s) for a managed component."""
from castle_cli.commands.service import _generate_timer, _generate_unit
from castle_core.generators import generate_timer, generate_unit
_validate_managed(name)
config = load_config(settings.castle_root)
manifest = config.managed[name]
unit = _generate_unit(config, name, manifest)
timer = _generate_timer(name, manifest)
unit = generate_unit(config, name, manifest)
timer = generate_timer(name, manifest)
return {"service": unit, "timer": timer}

View File

@@ -7,7 +7,7 @@ import json
import logging
import time
from castle_cli.config import load_config
from castle_core.config import load_config
from castle_api.config import settings
from castle_api.health import check_all_health

View File

@@ -7,8 +7,8 @@ from pathlib import Path
from fastapi import APIRouter, HTTPException, status
from castle_cli.config import load_config
from castle_cli.manifest import ComponentManifest
from castle_core.config import load_config
from castle_core.manifest import ComponentManifest
from castle_api.config import settings
from castle_api.models import ToolDetail, ToolSummary