Config outline
This commit is contained in:
parent
60c4aeb4e3
commit
2cde0de07a
4 changed files with 36 additions and 2 deletions
|
|
@ -1,4 +1,7 @@
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from main import run
|
try:
|
||||||
|
from main import run
|
||||||
|
except ImportError:
|
||||||
|
from .main import run
|
||||||
|
|
||||||
run()
|
run()
|
||||||
|
|
|
||||||
28
app/main.py
28
app/main.py
|
|
@ -1,3 +1,6 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
import yaml
|
||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||||
|
|
@ -10,7 +13,26 @@ templates_env = Environment(
|
||||||
autoescape=select_autoescape(["html", "xml"]),
|
autoescape=select_autoescape(["html", "xml"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def load_config() -> dict:
|
||||||
|
"""
|
||||||
|
Ładuje konfigurację z pliku YAML.
|
||||||
|
Domyślna ścieżka: config/config.yaml
|
||||||
|
Można nadpisać przez APP_CONFIG_FILE.
|
||||||
|
"""
|
||||||
|
config_path = os.getenv("APP_CONFIG_FILE", "config/config.yaml")
|
||||||
|
if not os.path.exists(config_path):
|
||||||
|
# Zwróć minimalną domyślną konfigurację, jeśli plik nie istnieje
|
||||||
|
return {
|
||||||
|
"app": {"host": "127.0.0.1", "port": 8000, "reload": True},
|
||||||
|
}
|
||||||
|
with open(config_path, "r", encoding="utf-8") as f:
|
||||||
|
return yaml.safe_load(f) or {}
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(title="Karl", version="0.1.0")
|
app = FastAPI(title="Karl", version="0.1.0")
|
||||||
|
# Załaduj konfigurację do stanu aplikacji
|
||||||
|
app.state.config = load_config()
|
||||||
|
|
||||||
# Rejestracja routera API pod /api/v1
|
# Rejestracja routera API pod /api/v1
|
||||||
app.include_router(api_v1_router, prefix="/api/v1", tags=["v1"])
|
app.include_router(api_v1_router, prefix="/api/v1", tags=["v1"])
|
||||||
|
|
@ -26,4 +48,8 @@ async def index(request: Request) -> HTMLResponse:
|
||||||
|
|
||||||
def run() -> None:
|
def run() -> None:
|
||||||
import uvicorn
|
import uvicorn
|
||||||
uvicorn.run("app.main:app", host="127.0.0.1", port=8000, reload=True)
|
cfg = getattr(app.state, "config", {})
|
||||||
|
host = cfg.get("app", {}).get("host", "127.0.0.1")
|
||||||
|
port = int(cfg.get("app", {}).get("port", 8000))
|
||||||
|
reload = bool(cfg.get("app", {}).get("reload", True))
|
||||||
|
uvicorn.run("app.main:app", host=host, port=port, reload=reload)
|
||||||
|
|
|
||||||
4
config/config.yaml
Normal file
4
config/config.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
app:
|
||||||
|
host: "127.0.0.1"
|
||||||
|
port: 8000
|
||||||
|
reload: true
|
||||||
|
|
@ -9,6 +9,7 @@ dependencies = [
|
||||||
"fastapi>=0.115.0",
|
"fastapi>=0.115.0",
|
||||||
"uvicorn[standard]>=0.30.0",
|
"uvicorn[standard]>=0.30.0",
|
||||||
"jinja2>=3.1.4",
|
"jinja2>=3.1.4",
|
||||||
|
"pyyaml>=6.0.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue