From be95b7aa5b3f93b714ca3e9f738b47e848ec94ea Mon Sep 17 00:00:00 2001 From: chenzuoqing Date: Mon, 6 Dec 2021 15:30:21 +0800 Subject: [PATCH] =?UTF-8?q?api=E8=B7=AF=E7=94=B1=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=89=88=E6=9C=AC=EF=BC=8C=E5=93=8D=E5=BA=94=E5=A4=B4=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AE=9E=E4=BE=8BID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 16 ++++++++++++++++ settings/common.py | 3 +++ src/asset/routes.py | 4 ++-- src/project/routes.py | 12 ++++++++++-- src/routes.py | 21 +++++++++++++++------ 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 4989b10..c8fa703 100644 --- a/app.py +++ b/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 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) diff --git a/settings/common.py b/settings/common.py index f76d8d3..a1f34bc 100644 --- a/settings/common.py +++ b/settings/common.py @@ -1,4 +1,7 @@ +VERSION = "v1" +INSTANCE = "9e5da0b3-565f-11ec-b095-18c04d9eb245" + # API static token 允许请求的静态token X_TOKEN_SET = ( "51ab21fa323a11eca1af000c2993d583", # diff --git a/src/asset/routes.py b/src/asset/routes.py index b44746c..75e649b 100644 --- a/src/asset/routes.py +++ b/src/asset/routes.py @@ -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//', endpoint="host-detail") diff --git a/src/project/routes.py b/src/project/routes.py index 8d275f0..a4ea2be 100644 --- a/src/project/routes.py +++ b/src/project/routes.py @@ -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//', endpoint="project-detail") @@ -25,3 +25,11 @@ api.add_resource(operation.ServerSyncView, '/server/sync/', endpoint="server-syn # 机器信息 api.add_resource(operation.AgentInfo, '/agent/info//', 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") diff --git a/src/routes.py b/src/routes.py index 5031ee9..0a86620 100644 --- a/src/routes.py +++ b/src/routes.py @@ -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)