Spaces:
Sleeping
Sleeping
Commit ·
0c62be0
1
Parent(s): b5d550f
add flask apis.
Browse files- fast_app/fast_app.py +77 -4
- requirements.txt +2 -0
fast_app/fast_app.py
CHANGED
|
@@ -1,11 +1,84 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
@app.get("/")
|
| 6 |
-
def
|
| 7 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
@app.get("/hello/")
|
| 10 |
def hello_json():
|
| 11 |
return {"msg": "/hello/ : Hello, FastAPI Node World!"}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Optional
|
| 5 |
+
import time
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
app = FastAPI(
|
| 9 |
+
title="FastAPI Service",
|
| 10 |
+
description="高性能 FastAPI 服务",
|
| 11 |
+
version="1.0.0",
|
| 12 |
+
docs_url="/docs",
|
| 13 |
+
redoc_url="/redoc",
|
| 14 |
+
openapi_url="/openapi.json"
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
# CORS 配置
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"],
|
| 21 |
+
allow_credentials=True,
|
| 22 |
+
allow_methods=["*"],
|
| 23 |
+
allow_headers=["*"],
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# 数据模型
|
| 27 |
+
class Item(BaseModel):
|
| 28 |
+
name: str
|
| 29 |
+
description: Optional[str] = None
|
| 30 |
+
price: float
|
| 31 |
+
tax: Optional[float] = None
|
| 32 |
+
|
| 33 |
+
class ComputeRequest(BaseModel):
|
| 34 |
+
numbers: list[float]
|
| 35 |
+
operation: str
|
| 36 |
+
|
| 37 |
+
class ComputeResponse(BaseModel):
|
| 38 |
+
result: float
|
| 39 |
+
operation: str
|
| 40 |
+
timestamp: float
|
| 41 |
+
|
| 42 |
+
# 健康检查
|
| 43 |
+
@app.get("/health")
|
| 44 |
+
async def health_check():
|
| 45 |
+
return {
|
| 46 |
+
"status": "healthy",
|
| 47 |
+
"service": "fastapi",
|
| 48 |
+
"version": "1.0.0",
|
| 49 |
+
"timestamp": time.time(),
|
| 50 |
+
"environment": os.getenv("ENVIRONMENT", "development")
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# 根路径
|
| 54 |
@app.get("/")
|
| 55 |
+
async def root():
|
| 56 |
+
return {
|
| 57 |
+
"message": "欢迎使用 FastAPI 服务",
|
| 58 |
+
"docs": "/docs",
|
| 59 |
+
"redoc": "/redoc",
|
| 60 |
+
"endpoints": [
|
| 61 |
+
"/health",
|
| 62 |
+
"/items",
|
| 63 |
+
"/compute",
|
| 64 |
+
"/info"
|
| 65 |
+
]
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
# 获取服务信息
|
| 69 |
+
@app.get("/info")
|
| 70 |
+
async def get_info():
|
| 71 |
+
return {
|
| 72 |
+
"service": "fastapi",
|
| 73 |
+
"port": os.getenv("PORT", 8000),
|
| 74 |
+
"python_version": os.sys.version,
|
| 75 |
+
"environment": os.getenv("ENVIRONMENT", "development"),
|
| 76 |
+
"startup_time": app.extra.get("startup_time", time.time())
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
|
| 80 |
|
| 81 |
+
# 路由测试
|
| 82 |
@app.get("/hello/")
|
| 83 |
def hello_json():
|
| 84 |
return {"msg": "/hello/ : Hello, FastAPI Node World!"}
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
fastapi
|
|
|
|
|
|
|
| 2 |
flask
|
| 3 |
flask-cors
|
| 4 |
flask-restx
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
pydantic
|
| 3 |
+
python-multipart
|
| 4 |
flask
|
| 5 |
flask-cors
|
| 6 |
flask-restx
|