31 lines
634 B
Python
31 lines
634 B
Python
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
import uvicorn
|
||
import random
|
||
|
||
|
||
app = FastAPI()
|
||
|
||
# 添加CORS中间件,允许所有来源
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=["*"], # 在生产环境中应该指定具体的域名
|
||
allow_credentials=True,
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
@app.get("/")
|
||
async def root():
|
||
return {"message": "Hello World"}
|
||
|
||
|
||
@app.get("/hello/{name}")
|
||
async def say_hello(name: str):
|
||
return {"message": f"Hello {name}"}
|
||
|
||
@app.get("/api/random")
|
||
async def random_number():
|
||
return {"number": random.randint(1, 100)}
|
||
|