add previous fix bug forgotting commit-push
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
from contextlib import asynccontextmanager
|
||||
import os
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from starlette.datastructures import Headers
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.middleware.sessions import SessionMiddleware
|
||||
import sqladmin
|
||||
|
||||
from app.admin import mount_admin
|
||||
from app.api.v1.routes import router as v1_router
|
||||
@@ -9,13 +15,59 @@ from app.core.config import settings
|
||||
from app.db.init_db import init_db
|
||||
|
||||
|
||||
class ForceHTTPSMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request, call_next):
|
||||
request.scope["scheme"] = "https"
|
||||
response = await call_next(request)
|
||||
return response
|
||||
|
||||
|
||||
class ForwardedProtoMiddleware:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
if scope["type"] in {"http", "websocket"}:
|
||||
headers = Headers(scope=scope)
|
||||
forwarded_proto = headers.get("x-forwarded-proto")
|
||||
if forwarded_proto:
|
||||
proto = forwarded_proto.split(",", 1)[0].strip()
|
||||
if proto:
|
||||
new_scope = dict(scope)
|
||||
new_scope["scheme"] = proto
|
||||
return await self.app(new_scope, receive, send)
|
||||
|
||||
return await self.app(scope, receive, send)
|
||||
|
||||
|
||||
origins = [
|
||||
"http://localhost:8040",
|
||||
"https://ai.sriphat.com",
|
||||
"http://ai.sriphat.com",
|
||||
]
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI):
|
||||
init_db()
|
||||
yield
|
||||
|
||||
|
||||
sqladmin_dir = os.path.dirname(sqladmin.__file__)
|
||||
statics_path = os.path.join(sqladmin_dir, "statics")
|
||||
|
||||
app = FastAPI(title=settings.APP_NAME, root_path=settings.ROOT_PATH, lifespan=lifespan)
|
||||
app.add_middleware(ForceHTTPSMiddleware)
|
||||
app.add_middleware(SessionMiddleware, secret_key=settings.ADMIN_SECRET_KEY)
|
||||
app.add_middleware(ForwardedProtoMiddleware)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
app.include_router(v1_router)
|
||||
app.mount("/admin/statics", StaticFiles(directory=statics_path), name="admin_statics")
|
||||
app.mount("/apiservice/admin/statics", StaticFiles(directory=statics_path), name="proxy_admin_statics")
|
||||
mount_admin(app)
|
||||
|
||||
Reference in New Issue
Block a user