Spaces:
Running
on
Zero
Running
on
Zero
df
Browse files- mysite/asgi.py +6 -48
mysite/asgi.py
CHANGED
@@ -1,17 +1,14 @@
|
|
1 |
import os
|
2 |
from django.core.asgi import get_asgi_application
|
3 |
-
from fastapi import FastAPI,
|
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,
|
15 |
from mysite.routers.database import setup_database_routes
|
16 |
from mysite.config.asgi_config import init_django_app
|
17 |
from interpreter import interpreter
|
@@ -19,24 +16,6 @@ import mysite.interpreter.interpreter_config # インポートするだけで
|
|
19 |
# ロガーの設定
|
20 |
from mysite.logger import logger
|
21 |
|
22 |
-
security = HTTPBasic()
|
23 |
-
|
24 |
-
# ベーシック認証のユーザー名とパスワード
|
25 |
-
users = {
|
26 |
-
"username1": "password1",
|
27 |
-
"username2": "password2"
|
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(
|
35 |
-
status_code=status.HTTP_401_UNAUTHORIZED,
|
36 |
-
detail="Invalid credentials",
|
37 |
-
headers={"WWW-Authenticate": "Basic"},
|
38 |
-
)
|
39 |
-
return credentials.username
|
40 |
|
41 |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
|
42 |
application = get_asgi_application()
|
@@ -55,20 +34,6 @@ app.add_middleware(
|
|
55 |
allow_headers=["*"],
|
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 |
-
|
70 |
-
app.add_middleware(BasicAuthMiddleware)
|
71 |
-
|
72 |
# Gradioインターフェースの設定
|
73 |
gradio_interfaces = setup_gradio_interfaces()
|
74 |
|
@@ -79,20 +44,13 @@ setup_webhook_routes(app)
|
|
79 |
# データベースルートの設定
|
80 |
setup_database_routes(app)
|
81 |
|
82 |
-
# 静的ファイルのマウント
|
83 |
-
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
84 |
-
|
85 |
# Gradioアプリのマウント
|
86 |
-
app
|
|
|
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 |
-
|
|
|
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
|
|
|
16 |
# ロガーの設定
|
17 |
from mysite.logger import logger
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
|
21 |
application = get_asgi_application()
|
|
|
34 |
allow_headers=["*"],
|
35 |
)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# Gradioインターフェースの設定
|
38 |
gradio_interfaces = setup_gradio_interfaces()
|
39 |
|
|
|
44 |
# データベースルートの設定
|
45 |
setup_database_routes(app)
|
46 |
|
|
|
|
|
|
|
47 |
# Gradioアプリのマウント
|
48 |
+
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
49 |
+
app = gr.mount_gradio_app(app, gradio_interfaces, "/")
|
50 |
|
51 |
# テンプレートファイルが格納されているディレクトリを指定
|
52 |
templates = Jinja2Templates(directory="static")
|
53 |
|
|
|
|
|
|
|
|
|
54 |
@app.get("/test")
|
55 |
def get_some_page(request: Request):
|
56 |
return templates.TemplateResponse("index.html", {"request": request})
|
|