2022-08-02 14:25:51 +07:00
|
|
|
# ===================== Importing FastAPI necessary packages =============
|
2021-07-19 23:02:31 +03:00
|
|
|
from fastapi import (
|
|
|
|
|
FastAPI,
|
|
|
|
|
HTTPException,
|
|
|
|
|
status,
|
|
|
|
|
Request,
|
|
|
|
|
)
|
2022-08-02 14:25:51 +07:00
|
|
|
from fastapi.staticfiles import StaticFiles
|
2021-07-21 17:40:52 +03:00
|
|
|
from src.dependecies import authenticate_user
|
2022-07-26 13:46:19 +07:00
|
|
|
from src.routers.routers import router
|
|
|
|
|
from src.routers.post import post
|
|
|
|
|
from src.routers.history_find import history
|
2022-08-02 14:25:51 +07:00
|
|
|
from src.routers.post_save import post_save
|
2021-07-19 23:02:31 +03:00
|
|
|
|
|
|
|
|
import base64
|
|
|
|
|
import binascii
|
|
|
|
|
|
2022-07-26 13:46:19 +07:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2021-07-19 23:02:31 +03:00
|
|
|
|
2022-08-02 14:25:51 +07:00
|
|
|
# ------------------ FastAPI variable ----------------------------------
|
2021-07-19 23:02:31 +03:00
|
|
|
app = FastAPI()
|
|
|
|
|
|
2022-08-02 14:25:51 +07:00
|
|
|
app.mount("/post", StaticFiles(directory="post"), name="post")
|
2022-07-26 13:46:19 +07:00
|
|
|
|
|
|
|
|
origins = ["*"]
|
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=origins,
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2021-07-19 23:02:31 +03:00
|
|
|
# ================ Authentication Middleware =======================
|
2022-08-02 14:25:51 +07:00
|
|
|
# ----------- Here authentication is based on basic scheme,
|
|
|
|
|
# ----------- another authentication, based on bearer scheme, is used throughout
|
|
|
|
|
# ---------- the application (as decribed in FastAPI oficial documentation)
|
2021-07-19 23:02:31 +03:00
|
|
|
@app.middleware("http")
|
|
|
|
|
async def authenticate(request: Request, call_next):
|
|
|
|
|
|
2022-08-02 14:25:51 +07:00
|
|
|
# -------------------- Authentication basic scheme -----------------------------
|
2021-07-19 23:02:31 +03:00
|
|
|
if "Authorization" in request.headers:
|
|
|
|
|
auth = request.headers["Authorization"]
|
|
|
|
|
try:
|
|
|
|
|
scheme, credentials = auth.split()
|
|
|
|
|
if scheme.lower() == 'basic':
|
|
|
|
|
decoded = base64.b64decode(credentials).decode("ascii")
|
|
|
|
|
username, _, password = decoded.partition(":")
|
|
|
|
|
request.state.user = await authenticate_user(username, password)
|
|
|
|
|
except (ValueError, UnicodeDecodeError, binascii.Error):
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
|
detail="Invalid basic auth credentials"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response = await call_next(request)
|
|
|
|
|
return response
|
|
|
|
|
|
2021-07-21 14:38:05 +03:00
|
|
|
# ================= Routers inclusion from src directory ===============
|
2022-07-26 13:46:19 +07:00
|
|
|
app.include_router(post)
|
|
|
|
|
app.include_router(router)
|
2022-08-02 14:25:51 +07:00
|
|
|
app.include_router(history)
|
|
|
|
|
app.include_router(post_save)
|