Initial commit
This commit is contained in:
59
00_Globale_Richtlinien/Entworfener_Code/app/code/app/main.py
Normal file
59
00_Globale_Richtlinien/Entworfener_Code/app/code/app/main.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
|
||||
def create_app(config: Optional[Dict[str, Any]] = None) -> FastAPI:
|
||||
"""
|
||||
Erzeugt und konfiguriert die FastAPI-Anwendung.
|
||||
|
||||
Args:
|
||||
config: Konfigurations-Dictionary (z. B. aus app/config/config.yaml geladen).
|
||||
|
||||
Returns:
|
||||
FastAPI: Konfigurierte FastAPI-App.
|
||||
"""
|
||||
cfg: Dict[str, Any] = config or {}
|
||||
meta: Dict[str, Any] = cfg.get("meta", {})
|
||||
app_cfg: Dict[str, Any] = cfg.get("app", {})
|
||||
cors_cfg: Dict[str, Any] = cfg.get("cors", {})
|
||||
|
||||
app = FastAPI(
|
||||
title=app_cfg.get("name", "entworfener_code_service"),
|
||||
version=str(meta.get("version", "0.1.0")),
|
||||
docs_url="/docs",
|
||||
redoc_url="/redoc",
|
||||
)
|
||||
|
||||
# CORS
|
||||
allow_origins: List[str] = cors_cfg.get("allow_origins", ["*"])
|
||||
allow_credentials: bool = bool(cors_cfg.get("allow_credentials", True))
|
||||
allow_methods: List[str] = cors_cfg.get("allow_methods", ["*"])
|
||||
allow_headers: List[str] = cors_cfg.get("allow_headers", ["*"])
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=allow_origins,
|
||||
allow_credentials=allow_credentials,
|
||||
allow_methods=allow_methods,
|
||||
allow_headers=allow_headers,
|
||||
)
|
||||
|
||||
# Router registrieren (unter app/code/api)
|
||||
try:
|
||||
from api.router import api_router
|
||||
|
||||
app.include_router(api_router, prefix="/api")
|
||||
except Exception:
|
||||
# Router existiert evtl. noch nicht beim ersten Scaffold
|
||||
pass
|
||||
|
||||
@app.get("/health", tags=["health"])
|
||||
async def health() -> Dict[str, str]:
|
||||
"""
|
||||
Einfache Health-Check-Route.
|
||||
"""
|
||||
return {"status": "ok"}
|
||||
|
||||
return app
|
||||
@@ -0,0 +1,36 @@
|
||||
# Application settings
|
||||
app:
|
||||
name: "entworfener_code_service"
|
||||
host: "0.0.0.0"
|
||||
port: 8000
|
||||
reload: true
|
||||
|
||||
# Logging settings
|
||||
logging:
|
||||
level: "INFO"
|
||||
config_file: "config/logging.yaml"
|
||||
|
||||
# CORS settings
|
||||
cors:
|
||||
allow_origins:
|
||||
- "*"
|
||||
allow_credentials: true
|
||||
allow_methods:
|
||||
- "*"
|
||||
allow_headers:
|
||||
- "*"
|
||||
|
||||
# Paths (relativ zu app/)
|
||||
paths:
|
||||
log_dir: "logs"
|
||||
data_dir: "data"
|
||||
|
||||
# Server timeouts
|
||||
server:
|
||||
timeout_keep_alive: 5
|
||||
backlog: 2048
|
||||
|
||||
# Metadata
|
||||
meta:
|
||||
version: "0.1.0"
|
||||
environment: "dev"
|
||||
@@ -0,0 +1,57 @@
|
||||
version: 1
|
||||
disable_existing_loggers: false
|
||||
|
||||
formatters:
|
||||
standard:
|
||||
format: "%(asctime)s | %(levelname)s | %(name)s | %(filename)s:%(lineno)d | %(message)s"
|
||||
datefmt: "%Y-%m-%d %H:%M:%S"
|
||||
access:
|
||||
format: '%(asctime)s | %(levelname)s | %(client_addr)s - "%(request_line)s" %(status_code)s'
|
||||
datefmt: "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
handlers:
|
||||
console:
|
||||
class: logging.StreamHandler
|
||||
level: INFO
|
||||
formatter: standard
|
||||
stream: ext://sys.stdout
|
||||
|
||||
app_file:
|
||||
class: logging.handlers.TimedRotatingFileHandler
|
||||
level: INFO
|
||||
formatter: standard
|
||||
filename: app/logs/app.log
|
||||
when: D
|
||||
interval: 1
|
||||
backupCount: 7
|
||||
encoding: utf-8
|
||||
|
||||
access_file:
|
||||
class: logging.handlers.TimedRotatingFileHandler
|
||||
level: INFO
|
||||
formatter: access
|
||||
filename: app/logs/access.log
|
||||
when: D
|
||||
interval: 1
|
||||
backupCount: 7
|
||||
encoding: utf-8
|
||||
|
||||
loggers:
|
||||
uvicorn:
|
||||
level: INFO
|
||||
handlers: [console, app_file]
|
||||
propagate: false
|
||||
|
||||
uvicorn.error:
|
||||
level: INFO
|
||||
handlers: [console, app_file]
|
||||
propagate: false
|
||||
|
||||
uvicorn.access:
|
||||
level: INFO
|
||||
handlers: [console, access_file]
|
||||
propagate: false
|
||||
|
||||
root:
|
||||
level: INFO
|
||||
handlers: [console, app_file]
|
||||
161
00_Globale_Richtlinien/Entworfener_Code/app/start.py
Normal file
161
00_Globale_Richtlinien/Entworfener_Code/app/start.py
Normal file
@@ -0,0 +1,161 @@
|
||||
from typing import Any, Dict, Optional, List
|
||||
import argparse
|
||||
import logging
|
||||
import logging.config
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
import uvicorn
|
||||
import yaml
|
||||
from fastapi import FastAPI
|
||||
|
||||
# Basisverzeichnis: .../Entworfener_Code/app
|
||||
BASE_DIR: Path = Path(__file__).resolve().parent
|
||||
CODE_DIR: Path = BASE_DIR / "code" # .../app/code
|
||||
CONFIG_DIR: Path = BASE_DIR / "config" # .../app/config
|
||||
|
||||
# Sicherstellen, dass .../app/code importierbar ist (bevorzugt neue Struktur)
|
||||
if str(CODE_DIR) not in sys.path:
|
||||
sys.path.insert(0, str(CODE_DIR))
|
||||
|
||||
# Jetzt Import aus neuer Struktur (app/code/app/main.py)
|
||||
from app.main import create_app # noqa: E402
|
||||
|
||||
APP_CONFIG_ENV = "APP_CONFIG_PATH"
|
||||
|
||||
|
||||
def load_yaml(path: Path) -> Dict[str, Any]:
|
||||
"""
|
||||
Lädt eine YAML-Datei und gibt den Inhalt als Dictionary zurück.
|
||||
"""
|
||||
with path.open("r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f) or {}
|
||||
|
||||
|
||||
def setup_logging(cfg: Dict[str, Any]) -> None:
|
||||
"""
|
||||
Initialisiert das Logging anhand der in app/config/logging.yaml definierten Konfiguration.
|
||||
Stellt sicher, dass das Log-Verzeichnis existiert.
|
||||
"""
|
||||
logging_cfg = cfg.get("logging", {}) or {}
|
||||
paths_cfg = cfg.get("paths", {}) or {}
|
||||
|
||||
log_dir = Path(paths_cfg.get("log_dir", "logs"))
|
||||
if not log_dir.is_absolute():
|
||||
log_dir = BASE_DIR / log_dir
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
logging_config_file = logging_cfg.get("config_file", "config/logging.yaml")
|
||||
logging_path = Path(logging_config_file)
|
||||
if not logging_path.is_absolute():
|
||||
logging_path = BASE_DIR / logging_path
|
||||
|
||||
if logging_path.exists():
|
||||
with logging_path.open("r", encoding="utf-8") as f:
|
||||
config_dict = yaml.safe_load(f) or {}
|
||||
logging.config.dictConfig(config_dict)
|
||||
else:
|
||||
logging.basicConfig(
|
||||
level=getattr(logging, (logging_cfg.get("level") or "INFO").upper(), logging.INFO),
|
||||
format="%(asctime)s | %(levelname)s | %(name)s | %(filename)s:%(lineno)d | %(message)s",
|
||||
)
|
||||
|
||||
logging.getLogger(__name__).debug("Logging initialisiert. Log-Verzeichnis: %s", str(log_dir))
|
||||
|
||||
|
||||
def load_config_from_env() -> Dict[str, Any]:
|
||||
"""
|
||||
Lädt die Anwendungskonfiguration aus der durch APP_CONFIG_PATH angegebenen Datei.
|
||||
Fällt zurück auf app/config/config.yaml.
|
||||
"""
|
||||
env_path = os.environ.get(APP_CONFIG_ENV)
|
||||
if env_path:
|
||||
cfg_path = Path(env_path)
|
||||
else:
|
||||
cfg_path = CONFIG_DIR / "config.yaml"
|
||||
|
||||
if not cfg_path.exists():
|
||||
print(f"Konfigurationsdatei nicht gefunden: {cfg_path}", file=sys.stderr)
|
||||
return {}
|
||||
|
||||
return load_yaml(cfg_path)
|
||||
|
||||
|
||||
def app_factory() -> FastAPI:
|
||||
"""
|
||||
Uvicorn-Factory für Reload-Betrieb (importierbare App-Fabrik).
|
||||
Liest Konfiguration, initialisiert Logging und erzeugt die FastAPI-App.
|
||||
"""
|
||||
cfg = load_config_from_env()
|
||||
setup_logging(cfg)
|
||||
app = create_app(cfg)
|
||||
return app
|
||||
|
||||
|
||||
def parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace:
|
||||
"""
|
||||
CLI-Argumente parsen.
|
||||
"""
|
||||
parser = argparse.ArgumentParser(description="Startet den FastAPI-Server (app/code-Struktur).")
|
||||
parser.add_argument(
|
||||
"--config",
|
||||
type=str,
|
||||
default=str(CONFIG_DIR / "config.yaml"),
|
||||
help="Pfad zur Konfigurationsdatei (YAML).",
|
||||
)
|
||||
return parser.parse_args(argv)
|
||||
|
||||
|
||||
def main(argv: Optional[List[str]] = None) -> None:
|
||||
"""
|
||||
Einstiegspunkt: lädt Konfiguration, initialisiert Logging, startet Uvicorn.
|
||||
"""
|
||||
args = parse_args(argv)
|
||||
|
||||
# Resolve config path
|
||||
config_path = Path(args.config)
|
||||
if not config_path.is_absolute():
|
||||
config_path = (BASE_DIR / args.config).resolve()
|
||||
|
||||
if not config_path.exists():
|
||||
print(f"Konfigurationsdatei nicht gefunden: {config_path}", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
# Set env var für Factory
|
||||
os.environ[APP_CONFIG_ENV] = str(config_path)
|
||||
|
||||
cfg = load_yaml(config_path)
|
||||
setup_logging(cfg)
|
||||
|
||||
app_cfg = cfg.get("app", {}) or {}
|
||||
host = str(app_cfg.get("host", "0.0.0.0"))
|
||||
port = int(app_cfg.get("port", 8000))
|
||||
reload_enabled = bool(app_cfg.get("reload", False))
|
||||
|
||||
logger = logging.getLogger("start")
|
||||
|
||||
if reload_enabled:
|
||||
# Für Reload muss eine importierbare App-Factory übergeben werden
|
||||
logger.info("Starte Uvicorn mit Reload (Factory-Modus, app/code).")
|
||||
module_name = Path(__file__).stem # "start"
|
||||
uvicorn.run(
|
||||
f"{module_name}:app_factory",
|
||||
factory=True,
|
||||
host=host,
|
||||
port=port,
|
||||
reload=True,
|
||||
)
|
||||
else:
|
||||
logger.info("Starte Uvicorn ohne Reload (app/code).")
|
||||
app = create_app(cfg)
|
||||
uvicorn.run(
|
||||
app,
|
||||
host=host,
|
||||
port=port,
|
||||
reload=False,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user