Last active
September 10, 2021 08:12
-
-
Save EasonC13/94514bc147c35aa2956a4b90b00f2067 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fastapi import FastAPI | |
| from fastapi.openapi.docs import get_swagger_ui_html | |
| from fastapi.openapi.utils import get_openapi | |
| import secrets | |
| from fastapi import FastAPI | |
| app = FastAPI(docs_url=None, redoc_url=None, openapi_url = None) | |
| import secrets | |
| from fastapi import Depends, FastAPI, HTTPException, status | |
| from fastapi.security import HTTPBasic, HTTPBasicCredentials | |
| security = HTTPBasic() | |
| def get_current_username(credentials: HTTPBasicCredentials = Depends(security)): | |
| correct_username = secrets.compare_digest(credentials.username, "user") | |
| correct_password = secrets.compare_digest(credentials.password, "password") | |
| if not (correct_username and correct_password): | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Incorrect email or password", | |
| headers={"WWW-Authenticate": "Basic"}, | |
| ) | |
| return credentials.username | |
| from fastapi.openapi.docs import get_swagger_ui_html | |
| from fastapi.openapi.utils import get_openapi | |
| @app.get("/docs", include_in_schema=False) | |
| async def get_documentation(username: str = Depends(get_current_username)): | |
| return get_swagger_ui_html(openapi_url="/openapi.json", title="docs") | |
| @app.get("/openapi.json", include_in_schema=False) | |
| async def openapi(username: str = Depends(get_current_username)): | |
| return get_openapi(title = "FastAPI", version="0.1.0", routes=app.routes) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to logout