Spaces:
Running
on
Zero
Running
on
Zero
df
Browse files- mysite/asgi.py +26 -22
mysite/asgi.py
CHANGED
@@ -1,24 +1,23 @@
|
|
1 |
import os
|
2 |
from django.core.asgi import get_asgi_application
|
3 |
-
from fastapi import FastAPI,Request
|
|
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
from fastapi.templating import Jinja2Templates
|
6 |
-
|
7 |
from starlette.middleware.cors import CORSMiddleware
|
8 |
-
|
|
|
9 |
import gradio as gr
|
|
|
|
|
10 |
from mysite.routers.gradio import setup_gradio_interfaces
|
11 |
-
from mysite.routers.fastapi import setup_webhook_routes,include_routers
|
12 |
from mysite.routers.database import setup_database_routes
|
13 |
from mysite.config.asgi_config import init_django_app
|
14 |
from interpreter import interpreter
|
15 |
import mysite.interpreter.interpreter_config # インポートするだけで設定が適用されます
|
16 |
# ロガーの設定
|
17 |
from mysite.logger import logger
|
18 |
-
from fastapi import Depends, HTTPException, status
|
19 |
-
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
20 |
-
from starlette.middleware.base import BaseHTTPMiddleware
|
21 |
-
import secrets
|
22 |
|
23 |
security = HTTPBasic()
|
24 |
|
@@ -29,7 +28,7 @@ users = {
|
|
29 |
}
|
30 |
|
31 |
def authenticate(credentials: HTTPBasicCredentials = Depends(security)):
|
32 |
-
correct_username =
|
33 |
correct_password = secrets.compare_digest(credentials.password, users[credentials.username]) if correct_username else False
|
34 |
if not (correct_username and correct_password):
|
35 |
raise HTTPException(
|
@@ -57,19 +56,14 @@ app.add_middleware(
|
|
57 |
)
|
58 |
|
59 |
# 認証用ミドルウェア
|
60 |
-
|
61 |
-
@app.get("/")
|
62 |
-
def read_root(credentials: HTTPBasicCredentials = Depends(authenticate)):
|
63 |
-
return {"message": f"Hello, {credentials.username}"}
|
64 |
-
|
65 |
-
# ミドルウェアで認証を追加
|
66 |
class BasicAuthMiddleware(BaseHTTPMiddleware):
|
67 |
async def dispatch(self, request, call_next):
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
73 |
response = await call_next(request)
|
74 |
return response
|
75 |
|
@@ -85,13 +79,23 @@ setup_webhook_routes(app)
|
|
85 |
# データベースルートの設定
|
86 |
setup_database_routes(app)
|
87 |
|
88 |
-
#
|
89 |
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
90 |
-
|
|
|
|
|
91 |
|
92 |
# テンプレートファイルが格納されているディレクトリを指定
|
93 |
templates = Jinja2Templates(directory="static")
|
94 |
|
|
|
|
|
|
|
|
|
95 |
@app.get("/test")
|
96 |
def get_some_page(request: Request):
|
97 |
return templates.TemplateResponse("index.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from django.core.asgi import get_asgi_application
|
3 |
+
from fastapi import FastAPI, Request, Depends, HTTPException, status
|
4 |
+
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
5 |
from fastapi.staticfiles import StaticFiles
|
6 |
from fastapi.templating import Jinja2Templates
|
|
|
7 |
from starlette.middleware.cors import CORSMiddleware
|
8 |
+
from starlette.middleware.base import BaseHTTPMiddleware
|
9 |
+
from starlette.responses import JSONResponse, RedirectResponse
|
10 |
import gradio as gr
|
11 |
+
import secrets
|
12 |
+
|
13 |
from mysite.routers.gradio import setup_gradio_interfaces
|
14 |
+
from mysite.routers.fastapi import setup_webhook_routes, include_routers
|
15 |
from mysite.routers.database import setup_database_routes
|
16 |
from mysite.config.asgi_config import init_django_app
|
17 |
from interpreter import interpreter
|
18 |
import mysite.interpreter.interpreter_config # インポートするだけで設定が適用されます
|
19 |
# ロガーの設定
|
20 |
from mysite.logger import logger
|
|
|
|
|
|
|
|
|
21 |
|
22 |
security = HTTPBasic()
|
23 |
|
|
|
28 |
}
|
29 |
|
30 |
def authenticate(credentials: HTTPBasicCredentials = Depends(security)):
|
31 |
+
correct_username = credentials.username in users
|
32 |
correct_password = secrets.compare_digest(credentials.password, users[credentials.username]) if correct_username else False
|
33 |
if not (correct_username and correct_password):
|
34 |
raise HTTPException(
|
|
|
56 |
)
|
57 |
|
58 |
# 認証用ミドルウェア
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
class BasicAuthMiddleware(BaseHTTPMiddleware):
|
60 |
async def dispatch(self, request, call_next):
|
61 |
+
if request.url.path.startswith("/gradio"):
|
62 |
+
credentials = security(request)
|
63 |
+
try:
|
64 |
+
authenticate(credentials)
|
65 |
+
except HTTPException as exc:
|
66 |
+
return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail})
|
67 |
response = await call_next(request)
|
68 |
return response
|
69 |
|
|
|
79 |
# データベースルートの設定
|
80 |
setup_database_routes(app)
|
81 |
|
82 |
+
# 静的ファイルのマウント
|
83 |
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
84 |
+
|
85 |
+
# Gradioアプリのマウント
|
86 |
+
app = gr.mount_gradio_app(app, gradio_interfaces, "/gradio")
|
87 |
|
88 |
# テンプレートファイルが格納されているディレクトリを指定
|
89 |
templates = Jinja2Templates(directory="static")
|
90 |
|
91 |
+
@app.get("/")
|
92 |
+
def read_root():
|
93 |
+
return RedirectResponse(url="/gradio")
|
94 |
+
|
95 |
@app.get("/test")
|
96 |
def get_some_page(request: Request):
|
97 |
return templates.TemplateResponse("index.html", {"request": request})
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
import uvicorn
|
101 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|