first commit

This commit is contained in:
Admin 2025-12-19 09:58:58 +07:00
parent d7c34b80e2
commit 91f51ee47b
4 changed files with 69 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import os
import yaml import yaml
SERVICES_DIR = "services" SERVICES_DIR = "services"
CONFIG_DIR = "configs"
def load_services(): def load_services():
services = [] services = []
@ -20,3 +21,23 @@ def load_services():
services.append(cfg) services.append(cfg)
return services return services
def load_app_config():
config = {}
if not os.path.isdir(CONFIG_DIR):
return config
for filename in os.listdir(CONFIG_DIR):
if not filename.endswith((".yml", ".yaml")):
continue
path = os.path.join(CONFIG_DIR, filename)
with open(path, "r", encoding="utf-8") as f:
data = yaml.safe_load(f) or {}
# merge config
config.update(data)
return config

View File

@ -6,12 +6,15 @@ from app.loader import load_services
from app.store import all, get, history, init_db from app.store import all, get, history, init_db
from app.scheduler import run_service from app.scheduler import run_service
from app.utils.schema import load_schema, render_response from app.utils.schema import load_schema, render_response
from app.middleware.auth import auth_middleware
services = load_services() services = load_services()
TASKS = [] TASKS = []
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
# ===== STARTUP ===== # ===== STARTUP =====
@ -32,6 +35,8 @@ app = FastAPI(
lifespan=lifespan lifespan=lifespan
) )
app.middleware("http")(auth_middleware)
@app.get("/health") @app.get("/health")
def health(): def health():
health_schema = load_schema("health_response") health_schema = load_schema("health_response")
@ -50,9 +55,6 @@ def health():
"last_updated": data.get("last_updated"), "last_updated": data.get("last_updated"),
}) })
print(services)
return render_response(health_schema, services) return render_response(health_schema, services)
@app.get("/services") @app.get("/services")

38
app/middleware/auth.py Normal file
View File

@ -0,0 +1,38 @@
from fastapi import Request
from fastapi.responses import JSONResponse
from app.loader import load_app_config
CONFIG = load_app_config()
AUTH = CONFIG.get("auth", {})
AUTH_ENABLED = AUTH.get("enabled", False)
API_TOKEN = AUTH.get("token")
PUBLIC_PATHS = set(AUTH.get("public_paths", []))
async def auth_middleware(request: Request, call_next):
if not AUTH_ENABLED:
return await call_next(request)
path = request.url.path
if path in PUBLIC_PATHS:
return await call_next(request)
auth_header = request.headers.get("Authorization")
if not auth_header or not auth_header.startswith("Bearer "):
return JSONResponse(
status_code=401,
content={"message": "Missing or invalid Authorization header"},
)
token = auth_header.replace("Bearer ", "").strip()
if token != API_TOKEN:
return JSONResponse(
status_code=403,
content={"message": "Invalid token"},
)
return await call_next(request)

5
configs/app.yml Normal file
View File

@ -0,0 +1,5 @@
auth:
enabled: true
token: Work1234.
public_paths:
- /health