Skip to content

Commit

Permalink
feat: Add root path options
Browse files Browse the repository at this point in the history
  • Loading branch information
hv0905 committed Dec 25, 2023
1 parent 7f23b54 commit fdd504d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 7 additions & 5 deletions app/webapp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from typing import Annotated

from fastapi import FastAPI
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.params import Depends
from fastapi.staticfiles import StaticFiles
Expand Down Expand Up @@ -34,16 +34,18 @@


@app.get("/", description="Default portal. Test for server availability.")
def welcome(token_passed: Annotated[bool, Depends(permissive_access_token_verify)],
def welcome(request: Request,
token_passed: Annotated[bool, Depends(permissive_access_token_verify)],
admin_token_passed: Annotated[bool, Depends(permissive_admin_token_verify)],
) -> WelcomeApiResponse:
root_path: str = request.scope.get('root_path').rstrip('/')
return WelcomeApiResponse(
message="Ciallo~ Welcome to NekoImageGallery API!",
server_time=datetime.now(),
wiki={
"openAPI": "/openapi.json",
"swagger UI": "/docs",
"redoc": "/redoc"
"openAPI": f"{root_path}/openapi.json",
"swagger UI": f"{root_path}/docs",
"redoc": f"{root_path}/redoc"
},
admin_api=WelcomeApiAdminPortalAuthenticationResponse(available=config.admin_api_enable,
passed=admin_token_passed),
Expand Down
7 changes: 6 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def parse_args():
'is set, will not start the server.')
parser.add_argument('--port', type=int, default=8000, help="Port to listen on, default is 8000")
parser.add_argument('--host', type=str, default="0.0.0.0", help="Host to bind on, default is 0.0.0.0")
parser.add_argument('--root-path', type=str, default="",
help="Root path of the server if your server is deployed behind a reverse proxy. "
"See https://fastapi.tiangolo.com/advanced/behind-a-proxy/ for detail.")

parser.add_argument('--version', action='version', version='%(prog)s 1.0.0')
return parser.parse_args()


Expand All @@ -48,4 +53,4 @@ def parse_args():

asyncio.run(local_create_thumbnail.main())
else:
uvicorn.run("app.webapp:app", host=args.host, port=args.port)
uvicorn.run("app.webapp:app", host=args.host, port=args.port, root_path=args.root_path)

0 comments on commit fdd504d

Please sign in to comment.