|
|
import os |
|
|
import asyncio |
|
|
from fastapi import FastAPI, HTTPException, Security, Depends, Query |
|
|
from fastapi.security import APIKeyHeader |
|
|
from pydantic import BaseModel, Field, create_model |
|
|
from typing import List, Optional |
|
|
import json |
|
|
import logging |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
app = FastAPI( |
|
|
title="FastAPI", |
|
|
version="0.1.0", |
|
|
servers=[ |
|
|
{ |
|
|
"url": "https://pvanand-doc-maker.hf.space", |
|
|
"description": "Production server" |
|
|
} |
|
|
] |
|
|
) |
|
|
|
|
|
from doc_maker import router as doc_maker_router |
|
|
app.include_router(doc_maker_router, prefix="/api/v1") |
|
|
|
|
|
|
|
|
CHAT_AUTH_KEY = os.getenv("CHAT_AUTH_KEY") |
|
|
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False) |
|
|
|
|
|
async def verify_api_key(api_key: str = Security(api_key_header)): |
|
|
if api_key != CHAT_AUTH_KEY: |
|
|
logger.warning("Invalid API key used") |
|
|
raise HTTPException(status_code=403, detail="Could not validate credentials") |
|
|
return api_key |
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=["*"], |
|
|
allow_credentials=True, |
|
|
allow_methods=["GET", "POST"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
import uvicorn |
|
|
uvicorn.run(app, host="0.0.0.0", port=8000) |