- 修复config.py中auth配置读取路径问题 - 在main.py中添加uvicorn自动启动功能 - 禁用FastAPI文档页面增强安全性 - 使服务器端口和监听地址可配置 - 添加完整的配置验证和测试
47 lines
1.0 KiB
Python
47 lines
1.0 KiB
Python
from hashlib import sha256
|
|
import hmac
|
|
from loguru import logger
|
|
from fastapi import FastAPI
|
|
from pydantic import BaseModel, Field
|
|
import uvicorn
|
|
|
|
import config
|
|
|
|
app = FastAPI(docs_url=None, redoc_url=None)
|
|
|
|
|
|
class SignRequest(BaseModel):
|
|
"""
|
|
签名请求
|
|
"""
|
|
header_str: str = Field(alias="headerStr", description="请求头字符串")
|
|
|
|
secret: str = Field(description="密钥")
|
|
|
|
|
|
class SignResponse(BaseModel):
|
|
"""
|
|
签名响应
|
|
"""
|
|
signature: str = Field(description="签名结果")
|
|
|
|
|
|
@app.post("/sign")
|
|
def sign(request: SignRequest):
|
|
# 对请求进行签名
|
|
logger.info(f"Signing request: {request}")
|
|
|
|
if request.secret not in config.avaliable_secrets:
|
|
# return 502
|
|
return {"error": "Invalid secret"}, 502
|
|
|
|
appsecret = config.secret.encode()
|
|
data = request.header_str.encode()
|
|
signature = hmac.new(appsecret, data, digestmod=sha256).hexdigest()
|
|
|
|
return SignResponse(signature=signature)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app, host=config.server_host, port=config.server_port)
|