Spaces:
Runtime error
Runtime error
Silicon Valley - Admin
commited on
Commit
路
49a61bb
1
Parent(s):
76ca3e9
Enhance server.py with CORS support, versioning, and health check endpoints
Browse files- Added CORS configuration to allow requests from specified domains.
- Introduced a version constant for the API to improve version management.
- Expanded trusted hosts for better network access.
- Implemented new root and health check endpoints for improved API usability.
- Updated status endpoint to return the API version from the constant instead of using importlib.metadata.
server.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
# server.py
|
| 2 |
from dataclasses import dataclass, asdict
|
| 3 |
-
import importlib.metadata
|
| 4 |
import secrets
|
| 5 |
import logging
|
| 6 |
import asyncio
|
|
@@ -9,17 +8,25 @@ from typing import Tuple
|
|
| 9 |
|
| 10 |
from quart import Quart, websocket, request
|
| 11 |
from quart_schema import QuartSchema, validate_request, validate_response
|
|
|
|
| 12 |
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
|
| 13 |
|
| 14 |
from broker import SessionBroker, SessionDoesNotExist, ClientRequest, ClientResponse, ClientError
|
| 15 |
|
| 16 |
# Configuraci贸n
|
|
|
|
| 17 |
TIMEOUT: int = 40
|
| 18 |
LOG_LEVEL: int = logging.DEBUG
|
| 19 |
-
TRUSTED_HOSTS: list[str] = ["127.0.0.1"]
|
| 20 |
|
| 21 |
# Create app
|
| 22 |
app = Quart(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
QuartSchema(app)
|
| 24 |
app.asgi_app = ProxyHeadersMiddleware(app.asgi_app, trusted_hosts=TRUSTED_HOSTS)
|
| 25 |
app.logger.setLevel(LOG_LEVEL)
|
|
@@ -74,7 +81,7 @@ class ErrorResponse:
|
|
| 74 |
@app.get("/status")
|
| 75 |
@validate_response(Status)
|
| 76 |
async def status() -> Status:
|
| 77 |
-
return Status(status="OK", version=
|
| 78 |
|
| 79 |
@app.websocket('/session')
|
| 80 |
async def session_handler():
|
|
@@ -156,5 +163,17 @@ async def write(data: Write) -> Tuple[WriteResponse | ErrorResponse, int]:
|
|
| 156 |
except asyncio.TimeoutError:
|
| 157 |
return ErrorResponse('Timeout when waiting for client.'), 500
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
def run():
|
| 160 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# server.py
|
| 2 |
from dataclasses import dataclass, asdict
|
|
|
|
| 3 |
import secrets
|
| 4 |
import logging
|
| 5 |
import asyncio
|
|
|
|
| 8 |
|
| 9 |
from quart import Quart, websocket, request
|
| 10 |
from quart_schema import QuartSchema, validate_request, validate_response
|
| 11 |
+
from quart_cors import cors
|
| 12 |
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
|
| 13 |
|
| 14 |
from broker import SessionBroker, SessionDoesNotExist, ClientRequest, ClientResponse, ClientError
|
| 15 |
|
| 16 |
# Configuraci贸n
|
| 17 |
+
VERSION = "1.0.0" # Versi贸n de la API
|
| 18 |
TIMEOUT: int = 40
|
| 19 |
LOG_LEVEL: int = logging.DEBUG
|
| 20 |
+
TRUSTED_HOSTS: list[str] = ["127.0.0.1", "10.16.38.136", "10.16.3.13", "10.16.13.73"]
|
| 21 |
|
| 22 |
# Create app
|
| 23 |
app = Quart(__name__)
|
| 24 |
+
app = cors(app,
|
| 25 |
+
allow_origin=["https://*.hf.space", "https://*.huggingface.co"],
|
| 26 |
+
allow_methods=["GET", "POST", "OPTIONS"],
|
| 27 |
+
allow_headers=["Content-Type"],
|
| 28 |
+
max_age=3600
|
| 29 |
+
)
|
| 30 |
QuartSchema(app)
|
| 31 |
app.asgi_app = ProxyHeadersMiddleware(app.asgi_app, trusted_hosts=TRUSTED_HOSTS)
|
| 32 |
app.logger.setLevel(LOG_LEVEL)
|
|
|
|
| 81 |
@app.get("/status")
|
| 82 |
@validate_response(Status)
|
| 83 |
async def status() -> Status:
|
| 84 |
+
return Status(status="OK", version=VERSION)
|
| 85 |
|
| 86 |
@app.websocket('/session')
|
| 87 |
async def session_handler():
|
|
|
|
| 163 |
except asyncio.TimeoutError:
|
| 164 |
return ErrorResponse('Timeout when waiting for client.'), 500
|
| 165 |
|
| 166 |
+
# Agregar un endpoint de health check y root
|
| 167 |
+
@app.route("/")
|
| 168 |
+
async def root():
|
| 169 |
+
return {"message": "Kaio API Server", "version": VERSION}
|
| 170 |
+
|
| 171 |
+
@app.route("/health")
|
| 172 |
+
async def health_check():
|
| 173 |
+
return {"status": "healthy"}
|
| 174 |
+
|
| 175 |
def run():
|
| 176 |
+
app.run(host='0.0.0.0', port=7860)
|
| 177 |
+
|
| 178 |
+
if __name__ == "__main__":
|
| 179 |
+
run()
|