Files
starlette-dial/main.py
2025-06-28 21:28:43 +08:00

63 lines
1.8 KiB
Python

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from starlette.requests import Request
from handler.error import (
handle_400,
handle_401,
handle_403,
handle_500,
handle_dail_401,
)
from handler.home import handle_home
from handler.other import handle_auth_check, handle_login
from pkg.dial import client
from pkg.dial.req import ModifiableRequest
from pkg.dial.exception import (
BadRequestException,
ForbiddenException,
NotFoundException,
ServerErrorException,
UnauthorizationException,
bad_request_exception_handler,
forbidden_exception_handler,
not_found_exception_handler,
server_error_exception_handler,
unauthorization_exception_handler,
)
def req_fn(request: ModifiableRequest):
request.rewrite_uri("/api/v3/auth/check")
return request.build()
app = Starlette(
debug=True,
routes=[
Route("/", handle_home),
Route("/400", handle_400),
Route("/500", handle_500),
Route("/401", handle_401),
Route("/403", handle_403),
Route("/api/login", handle_login, methods=["POST"]),
Route(
"/api/check", client.proxy("127.0.0.1:3000", req_fn, None), methods=["GET"]
),
Route("/api/dial/401", handle_dail_401, methods=["GET"]),
],
exception_handlers={
ServerErrorException: server_error_exception_handler,
UnauthorizationException: unauthorization_exception_handler,
ForbiddenException: forbidden_exception_handler,
BadRequestException: bad_request_exception_handler,
NotFoundException: not_found_exception_handler,
},
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)