38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
import hmac
|
|
import os
|
|
from fastapi import FastAPI, Depends, HTTPException, Security
|
|
from fastapi.security.api_key import APIKeyHeader
|
|
from strawberry.fastapi import GraphQLRouter
|
|
from dotenv import load_dotenv
|
|
|
|
from api.schema import schema
|
|
|
|
load_dotenv()
|
|
|
|
API_KEY_NAME = "X-API-Key"
|
|
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
|
|
|
|
|
def get_api_key(api_key_header: str = Security(api_key_header)):
|
|
expected_api_key = os.getenv("API_KEY")
|
|
if not expected_api_key:
|
|
# If no key is configured, deny all requests for safety
|
|
raise HTTPException(status_code=500, detail="API_KEY not configured on server")
|
|
|
|
if hmac.compare_digest(api_key_header or "", expected_api_key):
|
|
return api_key_header
|
|
raise HTTPException(status_code=401, detail="Invalid or missing API Key")
|
|
|
|
|
|
graphql_app = GraphQLRouter(schema)
|
|
|
|
app = FastAPI(title="Wecker API")
|
|
|
|
# Add auth dependency to the graphql route
|
|
app.include_router(graphql_app, prefix="/graphql", dependencies=[Depends(get_api_key)])
|
|
|
|
|
|
@app.get("/health")
|
|
def health_check():
|
|
return {"status": "ok"}
|