本项目是一个使用 Flask 实现的简单 API 网关。它作为中间件,用于验证令牌并将请求转发到目标服务。用于避免 Ollama 暴露在公网,而没有鉴权。
Ollama API 的调用参考: Ollama API 使用指南
Authorization 令牌。GET、POST、PUT、DELETE、PATCH)用于保护路由。pip install -r requirements.txt
2运行应用:
python main.py
API 网关期望在请求头中包含 Authorization 字段,并使用 Bearer 令牌。令牌必须为 TDP 才能通过验证。
示例:
curl -H "Authorization: Bearer TDP" http://localhost:5000/api/protected
网关将请求转发到预定义的目标 URL(https://www.example.com/)。你可以修改代码中的 target_url 变量,将其指向你所需的服务。
GET 请求:
curl -X GET -H "Authorization: Bearer TDP" http://localhost:5000/api/protected
POST 请求:
curl -X POST -H "Authorization: Bearer TDP" -d '{"key":"value"}' http://localhost:5000/api/protected
PUT 请求:
curl -X PUT -H "Authorization: Bearer TDP" -d '{"key":"value"}' http://localhost:5000/api/protected
DELETE 请求:
curl -X DELETE -H "Authorization: Bearer TDP" http://localhost:5000/api/protected
PATCH 请求:
curl -X PATCH -H "Authorization: Bearer TDP" -d '{"key":"value"}' http://localhost:5000/api/protected
protected_route 函数中的 target_url 变量,将其指向你所需的服务。validate_token 函数中的令牌验证逻辑。401 Unauthorized 错误。502 Bad Gateway 错误。欢迎贡献代码!如果有任何改进或 bug 修复,请提交 issue 或 pull request。
本项目基于 MIT 许可证。详情请参阅 LICENSE 文件。