#!/usr/bin/env python3
"""
FastAPI + Django 統合アプリケーション for Hugging Face Spaces
"""
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
import uvicorn
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
# 環境変数読み込み
load_dotenv()
# プロジェクトルートをパスに追加
project_root = Path(__file__).parent
sys.path.append(str(project_root))
# FastAPIアプリケーションの作成
app = FastAPI(
title="FastAPI Django Main Live",
description="高性能なFastAPI + Django統合アプリケーション",
version="1.0.0",
)
# CORS設定
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ルートエンドポイント
@app.get("/", response_class=HTMLResponse)
async def read_root():
return """
FastAPI Django Main Live
🚀 FastAPI Django Main Live
高性能なWeb アプリケーション
FastAPI + Django統合アプリケーションがHugging Face Spacesで稼働中です!
🛠️ 技術スタック
- 🐍 Python 3.9+
- ⚡ FastAPI
- 🎯 Django
- 🐳 Docker
- ☁️ Hugging Face Spaces
"""
@app.get("/health")
async def health_check():
"""ヘルスチェックエンドポイント"""
return {
"status": "healthy",
"service": "FastAPI Django Main Live",
"platform": "Hugging Face Spaces",
"docker": True,
"python_version": sys.version,
"environment": os.environ.get("SPACE_ID", "local")
}
@app.get("/status")
async def get_status():
"""アプリケーションステータス"""
return {
"application": "FastAPI Django Main Live",
"version": "1.0.0",
"framework": "FastAPI",
"platform": "Hugging Face Spaces",
"features": [
"REST API",
"Auto Documentation",
"CORS Support",
"Health Monitoring"
],
"endpoints": {
"root": "/",
"health": "/health",
"status": "/status",
"docs": "/docs",
"redoc": "/redoc"
}
}
@app.get("/api/hello")
async def hello_api():
"""シンプルなAPI例"""
return {"message": "Hello from FastAPI!", "success": True}
@app.get("/api/info")
async def app_info():
"""アプリケーション情報"""
return {
"name": "FastAPI Django Main Live",
"description": "高性能なFastAPI + Django統合アプリケーション",
"author": "kenken999",
"deployment": "Hugging Face Spaces",
"container": "Docker",
"python_version": sys.version.split()[0],
"working_directory": str(project_root),
"pid": os.getpid()
}
# サーバー起動
if __name__ == "__main__":
# Hugging Face Spacesの場合はポート7860を使用
port = int(os.environ.get("PORT", 7860))
host = os.environ.get("HOST", "0.0.0.0")
print(f"🚀 FastAPI Django Main Live 起動中...")
print(f"📡 URL: http://{host}:{port}")
print(f"📚 API Docs: http://{host}:{port}/docs")
print(f"💚 Health Check: http://{host}:{port}/health")
uvicorn.run(
app,
host=host,
port=port,
log_level="info",
access_log=True
)