Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| import json | |
| import os | |
| from datetime import datetime | |
| from auth import validate_api_key, get_key_info | |
| from key_generator import generate_api_key | |
| app = FastAPI(title="Billing & Auth Gateway", version="1.0.0") | |
| # Enable CORS | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Request models | |
| class GenerateKeyRequest(BaseModel): | |
| plan_tier: str | |
| user_email: str = "demo@example.com" | |
| class ValidateKeyRequest(BaseModel): | |
| api_key: str | |
| class RevokeKeyRequest(BaseModel): | |
| api_key: str | |
| async def root(): | |
| return { | |
| "service": "Billing & Auth Gateway", | |
| "version": "1.0.0", | |
| "status": "operational", | |
| "endpoints": [ | |
| "/api/generate_key", | |
| "/api/validate_key", | |
| "/api/revoke_key", | |
| "/api/key_info/{api_key}", | |
| "/health" | |
| ] | |
| } | |
| async def generate_key(request: GenerateKeyRequest): | |
| """Generate a new API key""" | |
| try: | |
| result = generate_api_key(request.plan_tier, request.user_email) | |
| return { | |
| "api_key": result["api_key"], | |
| "plan_tier": result["plan_tier"], | |
| "calls_limit": result["calls_limit"], | |
| "created_date": result["created_date"], | |
| "status": "success" | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def validate_key(request: ValidateKeyRequest): | |
| """Validate an API key""" | |
| result = validate_api_key(request.api_key) | |
| if not result["valid"]: | |
| return { | |
| "valid": False, | |
| "error": "Invalid API key", | |
| "status": "error" | |
| } | |
| return { | |
| "valid": True, | |
| "plan_tier": result["plan_tier"], | |
| "calls_limit": result["calls_limit"], | |
| "calls_used": result["calls_used"], | |
| "active": result["active"], | |
| "status": "success" | |
| } | |
| async def revoke_key(request: RevokeKeyRequest): | |
| """Revoke an API key""" | |
| try: | |
| # Load existing keys | |
| keys_file = "data/api_keys.json" | |
| if os.path.exists(keys_file): | |
| with open(keys_file, 'r') as f: | |
| keys = json.load(f) | |
| else: | |
| return {"revoked": False, "error": "Key not found", "status": "error"} | |
| # Find and revoke key | |
| key_found = False | |
| for key in keys: | |
| if key["api_key"] == request.api_key: | |
| key["active"] = False | |
| key_found = True | |
| break | |
| if not key_found: | |
| return {"revoked": False, "error": "Key not found", "status": "error"} | |
| # Save updated keys | |
| with open(keys_file, 'w') as f: | |
| json.dump(keys, f, indent=2) | |
| return {"revoked": True, "status": "success"} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def key_info(api_key: str): | |
| """Get information about an API key""" | |
| info = get_key_info(api_key) | |
| if not info: | |
| raise HTTPException(status_code=404, detail="Key not found") | |
| return { | |
| "plan_tier": info["plan_tier"], | |
| "calls_limit": info["calls_limit"], | |
| "calls_used": info["calls_used"], | |
| "created_date": info["created_date"], | |
| "active": info["active"], | |
| "status": "success" | |
| } | |
| async def health(): | |
| return { | |
| "status": "healthy", | |
| "service": "billing-auth-gateway", | |
| "version": "1.0.0", | |
| "timestamp": datetime.now().isoformat() | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |