api路由增加版本,响应头增加实例ID

This commit is contained in:
chenzuoqing 2021-12-06 15:30:21 +08:00
parent 8d54f9e8fd
commit be95b7aa5b
5 changed files with 46 additions and 10 deletions

16
app.py
View File

@ -9,6 +9,8 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from database.mongodb import initialize_db
from routes import blueprint_api
from settings.dev import LOGGING
from settings.common import INSTANCE, VERSION
from common.utils import make_response
# 日志配置
@ -26,6 +28,20 @@ initialize_db(app)
app.register_blueprint(blueprint_api)
@app.after_request
def mark_global_header(resp):
"""全局 header """
# 标记当前提供服务的实例
resp.headers["Instance"] = INSTANCE
return resp
@app.route('/info')
def app_info():
"""返回实例信息实例版本、实例ID"""
return make_response(200, 1000, "success", version=VERSION, instance=INSTANCE)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True)

View File

@ -1,4 +1,7 @@
VERSION = "v1"
INSTANCE = "9e5da0b3-565f-11ec-b095-18c04d9eb245"
# API static token 允许请求的静态token
X_TOKEN_SET = (
"51ab21fa323a11eca1af000c2993d583", #

View File

@ -5,9 +5,9 @@ from asset import views
# 当前app的蓝图以app名为前缀
asset = Blueprint('asset', __name__, url_prefix="/asset")
asset_v1 = Blueprint('asset', __name__, url_prefix="/asset")
# 增加路由
api = Api(asset)
api = Api(asset_v1)
api.add_resource(views.HostViews, '/host/', endpoint="host")
api.add_resource(views.HostDetailViews, '/host/<string:pk>/', endpoint="host-detail")

View File

@ -5,10 +5,10 @@ from project.views import views, operation
# 当前app的蓝图以app名为前缀
project = Blueprint('project', __name__, url_prefix="/project")
project_v1 = Blueprint('project', __name__, url_prefix="/project")
# 增加路由
api = Api(project)
api = Api(project_v1)
# 项目模型的视图
api.add_resource(views.ProjectViews, '/item/', endpoint="project")
api.add_resource(views.ProjectDetailViews, '/item/<string:pk>/', endpoint="project-detail")
@ -25,3 +25,11 @@ api.add_resource(operation.ServerSyncView, '/server/sync/', endpoint="server-syn
# 机器信息
api.add_resource(operation.AgentInfo, '/agent/info/<string:agent_id>/', endpoint="agent-info")
# # 接口版本v2
# project_v2 = Blueprint('project', __name__, url_prefix="/project")
# # 增加路由
# api = Api(project_v2)
# # 项目模型的视图
# api.add_resource(views.ProjectViews, '/item/', endpoint="project_v2")

View File

@ -1,10 +1,19 @@
from flask import Blueprint
from project.routes import project
from asset.routes import asset
from project.routes import project_v1
# from project.routes import project_v2
from asset.routes import asset_v1
blueprint_api = Blueprint('api-main', __name__, url_prefix='/api')
# 注册子蓝图,嵌套
blueprint_api.register_blueprint(asset)
blueprint_api.register_blueprint(project)
# 接口版本v1
api_v1 = Blueprint('api-v1', __name__, url_prefix='/v1')
api_v1.register_blueprint(asset_v1)
api_v1.register_blueprint(project_v1)
# 接口版本v2
# api_v2 = Blueprint('api-v2', __name__, url_prefix='/v2')
# api_v2.register_blueprint(project_v2)
# 注册子蓝图,嵌套版本
blueprint_api.register_blueprint(api_v1)
# blueprint_api.register_blueprint(api_v2)