OU / app.py
Johnny0619's picture
Update app.py
d1676f4 verified
import uvicorn
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.responses import FileResponse
import os
app = FastAPI()
security = HTTPBasic()
# ユーザー名とパスワードの設定
def authenticate(credentials: HTTPBasicCredentials = Depends(security)):
correct_username = "admin"
correct_password = "password123"
if credentials.username != correct_username or credentials.password != correct_password:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.username
# ルート (/) - index.html を表示
@app.get("/")
async def read_index(username: str = Depends(authenticate)):
if os.path.exists('index.html'):
return FileResponse('index.html')
else:
return "index.htmlが見つかりません。ファイルをアップロードしてください。"
# 業務ポータル (/bussiness_portal)
@app.get("/bussiness_portal")
async def read_bussiness_portal(username: str = Depends(authenticate)):
file_path = 'bussiness_portal.html'
if os.path.exists(file_path):
return FileResponse(file_path)
else:
return f"{file_path}が見つかりません。"
# ------------------------------------------------------------------
# リンク切れ対策 (/entry_info.html)
# ------------------------------------------------------------------
@app.get("/entry_info.html")
async def read_entry_info(username: str = Depends(authenticate)):
file_path = 'entry_info.html'
if os.path.exists(file_path):
return FileResponse(file_path)
else:
return f"{file_path}が見つかりません。"
# ガイドページ (/guide.html)
@app.get("/guide.html")
async def read_guide(username: str = Depends(authenticate)):
file_path = 'guide.html'
if os.path.exists(file_path):
return FileResponse(file_path)
else:
return f"{file_path}が見つかりません。"
# ------------------------------------------------------------------
# 【追加箇所】pagesフォルダ内のファイルへのルーティング
# ------------------------------------------------------------------
# 講義マップ (/lecture_map.html)
@app.get("/lecture_map.html")
async def read_lecture_map(username: str = Depends(authenticate)):
# pagesフォルダの中を参照するようにパスを指定
file_path = 'pages/lecture_map.html'
if os.path.exists(file_path):
return FileResponse(file_path)
else:
return f"{file_path}が見つかりません。pagesフォルダに配置してください。"
# 講義テキスト (/lecture_texts.html)
@app.get("/lecture_texts.html")
async def read_lecture_texts(username: str = Depends(authenticate)):
# pagesフォルダの中を参照するようにパスを指定
file_path = 'pages/lecture_texts.html'
if os.path.exists(file_path):
return FileResponse(file_path)
else:
return f"{file_path}が見つかりません。pagesフォルダに配置してください。"
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)