File size: 5,789 Bytes
1e13199 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
#!/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 """
<html>
<head>
<title>FastAPI Django Main Live</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.container {
max-width: 800px;
margin: 0 auto;
text-align: center;
}
.card {
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
padding: 20px;
margin: 20px 0;
backdrop-filter: blur(10px);
}
.button {
display: inline-block;
padding: 10px 20px;
margin: 10px;
background: #4CAF50;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s;
}
.button:hover {
background: #45a049;
}
.api-button {
background: #2196F3;
}
.api-button:hover {
background: #1976D2;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 FastAPI Django Main Live</h1>
<div class="card">
<h2>高性能なWeb アプリケーション</h2>
<p>FastAPI + Django統合アプリケーションがHugging Face Spacesで稼働中です!</p>
</div>
<div class="card">
<h3>🔗 利用可能なエンドポイント</h3>
<a href="/docs" class="button api-button">📚 API ドキュメント</a>
<a href="/health" class="button">💚 ヘルスチェック</a>
<a href="/status" class="button">📊 ステータス</a>
</div>
<div class="card">
<h3>🛠️ 技術スタック</h3>
<ul style="list-style: none; padding: 0;">
<li>🐍 Python 3.9+</li>
<li>⚡ FastAPI</li>
<li>🎯 Django</li>
<li>🐳 Docker</li>
<li>☁️ Hugging Face Spaces</li>
</ul>
</div>
</div>
</body>
</html>
"""
@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
)
|