File size: 1,943 Bytes
d585c70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

# 创建 FastAPI 应用实例
app = FastAPI(
    title="Simple Calculator API",
    description="一个用于验证部署流程的最简API,接收两个数字并返回它们的和。",
    version="0.1.0"
)

# --- CORS 中间件 ---
# 允许所有来源的请求,为未来的前端集成做准备
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# --- 核心计算函数 (替代MIDI处理) ---
def simple_add(number1: float, number2: float) -> float:
    """

    这是一个简单的计算函数,模拟你的核心业务逻辑。

    它接收两个浮点数,返回它们的和。

    这个函数未来可以被替换为 process_midi()。

    """
    print(f"正在计算: {number1} + {number2}")
    return number1 + number2


# --- API 端点 ---

@app.get("/", tags=["General"])
def read_root():
    """根路径,提供欢迎信息和文档链接。"""
    return {"message": "Welcome to the Simple Calculator API!", "docs_url": "/docs"}


@app.get("/calculate", tags=["Calculation"])
def calculate_sum(

    a: float = Query(..., description="第一个数字"),

    b: float = Query(..., description="第二个数字")

):
    """

    接收两个查询参数 'a' 和 'b',调用计算函数,并返回结果。

    示例调用: /calculate?a=10&b=22

    """
    # 调用核心计算函数
    result = simple_add(a, b)
    
    # 以JSON格式返回结果
    return {
        "input": {
            "a": a,
            "b": b
        },
        "operation": "addition",
        "result": result
    }

# 这部分是为了方便在本地直接运行测试,Docker部署时不会用到
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)