OU / app.py
Johnny0619's picture
Update app.py
368c336 verified
raw
history blame
2.6 kB
# app.py (Hugging Face Space用)
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) - bussiness_portal.html を表示
@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_info04) - entry_info04.html を表示
@app.get("/entry_info04")
async def read_entry_info(username: str = Depends(authenticate)):
file_path = 'entry_info04.html'
if os.path.exists(file_path):
return FileResponse(file_path)
else:
return f"{file_path}が見つかりません。"
# ------------------------------------------------------------------
# 【修正箇所】
# 1. URLを "/guide" から "/guide.html" に変更(HTML側のリンクと合わせるため)
# 2. 関数名を "read_entry_info" から "read_guide" に変更(重複エラー回避)
# ------------------------------------------------------------------
@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}が見つかりません。"
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)