Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
kafmws committed Apr 28, 2024
1 parent 112ca63 commit 127352d
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions g4f/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
from g4f.client import AsyncClient
from g4f.typing import Messages

global _g4f_api_key

def create_app():
def create_app(g4f_api_key:str = None):
app = FastAPI()
api = Api(app, g4f_api_key=_g4f_api_key)
api = Api(app, g4f_api_key=g4f_api_key)
api.register_routes()
api.register_authorization()
api.register_validation_exception_handler()
Expand Down Expand Up @@ -53,7 +51,6 @@ def __init__(self, app: FastAPI, g4f_api_key=None) -> None:
self.app = app
self.client = AsyncClient()
self.g4f_api_key = g4f_api_key
print(g4f_api_key)
self.get_g4f_api_key = APIKeyHeader(name="g4f-api-key")

def register_authorization(self):
Expand All @@ -64,7 +61,6 @@ async def authorization(request: Request, call_next):
user_g4f_api_key = await self.get_g4f_api_key(request)
except HTTPException as e:
if e.status_code == 403:
print(e)
return JSONResponse(
status_code=HTTP_401_UNAUTHORIZED,
content=jsonable_encoder({"detail": "G4F API key required"}),
Expand Down Expand Up @@ -184,18 +180,14 @@ def run_api(
bind: str = None,
debug: bool = False,
workers: int = None,
g4f_api_key: str = None,
use_colors: bool = None
use_colors: bool = None,
g4f_api_key: str = None
) -> None:

global _g4f_api_key
_g4f_api_key = g4f_api_key

print(f'Starting server... [g4f v-{g4f.version.utils.current_version}]' + (" (debug)" if debug else ""))
if use_colors is None:
use_colors = debug
if bind is not None:
host, port = bind.split(":")
if debug:
g4f.debug.logging = True
uvicorn.run("g4f.api:create_app", host=host, port=int(port), workers=workers, use_colors=use_colors, factory=True)
uvicorn.run(create_app(g4f_api_key), host=host, port=int(port), workers=workers, use_colors=use_colors)

0 comments on commit 127352d

Please sign in to comment.