Files
Mace-AIO-Discord-Bot---With…/bot/api/dependencies.py
TheOnlyMace b4110c3d66
Some checks failed
CI / Bot (Python) (push) Failing after 49s
CI / Dashboard (Next.js) (push) Failing after 11s
Update project configuration files, and API routes accordingly. Add SQLite runtime file ignores to .gitignore for better file management.
2026-07-21 17:11:38 +02:00

75 lines
3.0 KiB
Python

# ╔══════════════════════════════════════════════════════════════════╗
# ║ ║
# ║ +-+-+-+-+-+-+-+-+ ║
# ║ |H|e|x|a|H|o|s|t| ║
# ║ +-+-+-+-+-+-+-+-+ ║
# ║ ║
# ║ © 2026 HexaHost — All Rights Reserved ║
# ║ ║
# ║ discord ── https://discord.gg/hexahost ║
# ║ github ── https://github.com/theoneandonlymace ║
# ║ ║
# ╚══════════════════════════════════════════════════════════════════╝
import os
from typing import Optional, TYPE_CHECKING
from fastapi import HTTPException, Depends, Security
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from slowapi import Limiter
from slowapi.util import get_remote_address
if TYPE_CHECKING:
from core.hexahost import HexaHost
# Initialize rate limiter
limiter = Limiter(key_func=get_remote_address, default_limits=["1000 per minute"])
# Global reference to the bot instance
_bot_instance: Optional["HexaHost"] = None
# Security scheme
security = HTTPBearer()
def verify_api_key(credentials: HTTPAuthorizationCredentials = Security(security)):
"""
Dependency to verify the API key from the Authorization header.
Expected: Authorization: Bearer <API_KEY>
"""
api_key = os.getenv("DASHBOARD_API_KEY")
# If no key is set in env, we might want to allow for local testing or force it.
# The requirement says "Read API key from environment variable", implying it's required.
if not api_key:
raise HTTPException(
status_code=500,
detail="DASHBOARD_API_KEY environment variable is not set."
)
if credentials.credentials != api_key:
raise HTTPException(
status_code=401,
detail="Invalid or missing API key."
)
return credentials.credentials
def set_bot(bot_instance: "HexaHost"):
"""
Sets the global bot instance.
This should be called in HexaHost.py during startup.
"""
global _bot_instance
_bot_instance = bot_instance
def get_bot() -> "HexaHost":
"""
FastAPI dependency to retrieve the Discord bot instance.
Usage: bot: HexaHost = Depends(get_bot)
"""
if _bot_instance is None:
raise HTTPException(
status_code=503,
detail="Discord bot instance is not initialized yet."
)
return _bot_instance