api路由增加版本,响应头增加实例ID
This commit is contained in:
parent
8d54f9e8fd
commit
be95b7aa5b
16
app.py
16
app.py
|
@ -9,6 +9,8 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
||||||
from database.mongodb import initialize_db
|
from database.mongodb import initialize_db
|
||||||
from routes import blueprint_api
|
from routes import blueprint_api
|
||||||
from settings.dev import LOGGING
|
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.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__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=5001, debug=True)
|
app.run(host='0.0.0.0', port=5001, debug=True)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
|
|
||||||
|
VERSION = "v1"
|
||||||
|
INSTANCE = "9e5da0b3-565f-11ec-b095-18c04d9eb245"
|
||||||
|
|
||||||
# API static token 允许请求的静态token
|
# API static token 允许请求的静态token
|
||||||
X_TOKEN_SET = (
|
X_TOKEN_SET = (
|
||||||
"51ab21fa323a11eca1af000c2993d583", #
|
"51ab21fa323a11eca1af000c2993d583", #
|
||||||
|
|
|
@ -5,9 +5,9 @@ from asset import views
|
||||||
|
|
||||||
|
|
||||||
# 当前app的蓝图,以app名为前缀
|
# 当前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.HostViews, '/host/', endpoint="host")
|
||||||
api.add_resource(views.HostDetailViews, '/host/<string:pk>/', endpoint="host-detail")
|
api.add_resource(views.HostDetailViews, '/host/<string:pk>/', endpoint="host-detail")
|
||||||
|
|
|
@ -5,10 +5,10 @@ from project.views import views, operation
|
||||||
|
|
||||||
|
|
||||||
# 当前app的蓝图,以app名为前缀
|
# 当前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.ProjectViews, '/item/', endpoint="project")
|
||||||
api.add_resource(views.ProjectDetailViews, '/item/<string:pk>/', endpoint="project-detail")
|
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")
|
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")
|
||||||
|
|
|
@ -1,10 +1,19 @@
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from project.routes import project
|
from project.routes import project_v1
|
||||||
from asset.routes import asset
|
# from project.routes import project_v2
|
||||||
|
from asset.routes import asset_v1
|
||||||
|
|
||||||
blueprint_api = Blueprint('api-main', __name__, url_prefix='/api')
|
blueprint_api = Blueprint('api-main', __name__, url_prefix='/api')
|
||||||
|
|
||||||
# 注册子蓝图,嵌套
|
# 接口版本v1
|
||||||
blueprint_api.register_blueprint(asset)
|
api_v1 = Blueprint('api-v1', __name__, url_prefix='/v1')
|
||||||
blueprint_api.register_blueprint(project)
|
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)
|
||||||
|
|
Loading…
Reference in New Issue