From f9f2ac44af58568f95fccf92c7f716548d56bbd0 Mon Sep 17 00:00:00 2001 From: zhizhisheng Date: Sat, 8 Oct 2022 18:18:19 +0800 Subject: [PATCH 1/2] updates: 1. change the web structure to Flask. 2. fix some bugs about tone-agent-client task running. --- app/__init__.py | 44 ++++++++++-------------------------- app/agent/blue.py | 9 -------- app/agent/views.py | 7 ------ app/api/__init__.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ app/blue.py | 11 --------- core/result.py | 3 +++ core/task.py | 4 ++++ main.py | 14 ++++-------- requirements.txt | 2 +- 9 files changed, 78 insertions(+), 70 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 6c3b97b..fc55632 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,41 +1,11 @@ # coding: utf-8 from __future__ import annotations - -from typing import Optional - -from sanic import Sanic - -from app.blue import BLUE_TUPLE +from flask import Flask from conf import SERVICE_CONFIG - -from core.common import log from core.common.singleton import CONFIG -def init_app(env: Optional[str] = None) -> Sanic: - """创建app""" - - # app创建 - app: Sanic = Sanic(__name__, log_config=getattr(log, "LOGGER_CONFIG")) - - # 应用配置 - app.config.update_config(CONFIG) - - # 注册蓝图 - register_blueprint(app) - - return app - - -def register_blueprint(app: Sanic, prefix: Optional[str] = '') -> None: - """注册蓝图""" - for blueprint in BLUE_TUPLE: - blueprint.url_prefix = '/'.join(url for url in (prefix, blueprint.url_prefix) if url) - app.blueprint(blueprint) - return None - - prompt_info = """ ┌───────────────────────────────────────────────────────────────────┐ │ tone-agent │ @@ -51,4 +21,14 @@ prompt_info = """ tsn=SERVICE_CONFIG.get('tsn').ljust(34), mode=SERVICE_CONFIG.get('mode').ljust(34), proxy=SERVICE_CONFIG.get('proxy').ljust(34) -) \ No newline at end of file +) + + +app = Flask(__name__) + +@app.route('/') +def index(): + return 'Hello World' + +def init_app(): + return app diff --git a/app/agent/blue.py b/app/agent/blue.py index c7ab372..cab973e 100644 --- a/app/agent/blue.py +++ b/app/agent/blue.py @@ -1,10 +1 @@ # coding: utf-8 - -from sanic import Blueprint - -from app.agent.views import index - -bp: Blueprint = Blueprint('agent', url_prefix='') - - -bp.add_route(index, '/', ('GET',)) diff --git a/app/agent/views.py b/app/agent/views.py index 178f2ac..4450d34 100644 --- a/app/agent/views.py +++ b/app/agent/views.py @@ -1,10 +1,3 @@ # coding: utf-8 from __future__ import annotations - -from sanic import Request -from sanic.response import json, HTTPResponse - - -async def index(request: Request) -> HTTPResponse: - return json({'message': 'hello world!'}) diff --git a/app/api/__init__.py b/app/api/__init__.py index e69de29..c52751d 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -0,0 +1,54 @@ +# coding: utf-8 + +from __future__ import annotations + +from typing import Optional + +# from sanic import Sanic + +from app.blue import BLUE_TUPLE +from conf import SERVICE_CONFIG + +from core.common import log +from core.common.singleton import CONFIG + + +def init_app(env: Optional[str] = None) -> Sanic: + """创建app""" + + # app创建 + app: Sanic = Sanic(__name__, log_config=getattr(log, "LOGGER_CONFIG")) + + # 应用配置 + app.config.update_config(CONFIG) + + # 注册蓝图 + register_blueprint(app) + + return app + + +def register_blueprint(app: Sanic, prefix: Optional[str] = '') -> None: + """注册蓝图""" + for blueprint in BLUE_TUPLE: + blueprint.url_prefix = '/'.join(url for url in (prefix, blueprint.url_prefix) if url) + app.blueprint(blueprint) + return None + + +prompt_info = """ +┌───────────────────────────────────────────────────────────────────┐ +│ tone-agent │ +│ http://0.0.0.0:{port}│ +├──────────────────────┬────────────────────────────────────────────┤ +│ ▄██ ████ ██▄ │ tsn: {tsn}│ +│ ██ │ mode: {mode}│ +│ ██ │ proxy: {proxy}│ +│ ██ │ version: 1.1.0 │ +└──────────────────────┴────────────────────────────────────────────┘ +""".format( + port=str(CONFIG.APP_PORT).ljust(31), + tsn=SERVICE_CONFIG.get('tsn').ljust(34), + mode=SERVICE_CONFIG.get('mode').ljust(34), + proxy=SERVICE_CONFIG.get('proxy').ljust(34) +) \ No newline at end of file diff --git a/app/blue.py b/app/blue.py index 217e0ab..4450d34 100644 --- a/app/blue.py +++ b/app/blue.py @@ -1,14 +1,3 @@ # coding: utf-8 from __future__ import annotations - -from sanic import Blueprint - -from app.agent.blue import bp as agent_bp -from app.api.blue import bp as api_bp - - -BLUE_TUPLE: tuple[Blueprint, ...] = ( - agent_bp, - api_bp -) diff --git a/core/result.py b/core/result.py index c26533c..2128ab7 100644 --- a/core/result.py +++ b/core/result.py @@ -31,6 +31,9 @@ class Result(BaseObject): 将结果文件写入到本地 """ json_data: dict = self.to_dict() + path_result = os.path.split(self.result_file)[0] + if not os.path.exists(path_result): + os.makedirs(path_result) with open(self.result_file, 'w') as f: json.dump(json_data, f) return True diff --git a/core/task.py b/core/task.py index ba08263..dab0137 100644 --- a/core/task.py +++ b/core/task.py @@ -1,5 +1,6 @@ # coding: utf-8 +import os import base64 import subprocess from threading import Timer @@ -55,6 +56,9 @@ class Task(BaseObject): """ logger.info(f'execute task: {self.to_dict()}') # 将脚本写入临时文件 + path_script = os.path.split(self.script_file)[0] + if not os.path.exists(path_script): + os.makedirs(path_script) with open(self.script_file, 'w') as f: f.write(base64.b64decode(self.script).decode()) diff --git a/main.py b/main.py index 9b8b3d9..5050095 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,4 @@ -# # coding: utf-8 - -import os -from sanic import Sanic from app import init_app, prompt_info - from core.common.enum import AgentMode from core.common.log import logger from conf import SERVICE_CONFIG @@ -11,20 +6,19 @@ from core.scheduler import run_scheduler_forever, run_by_threaded def main(): - app: Sanic = init_app() - logger.info(prompt_info) + app = init_app() + if SERVICE_CONFIG.get('mode') == AgentMode.ACTIVE: run_by_threaded(run_scheduler_forever) app.run( host='0.0.0.0', port=app.config.get('APP_PORT'), - workers=app.config.get('WORKS_NUM'), - auto_reload=True + debug=True ) if __name__ == '__main__': - main() + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index fe52a21..d8e2dc8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -sanic +flask schedule pyyaml requests -- Gitee From f6c53a56cba92f31e6d92a198fbf2f9381d5db1d Mon Sep 17 00:00:00 2001 From: zhizhisheng Date: Sun, 9 Oct 2022 08:43:22 +0800 Subject: [PATCH 2/2] updates: 1. make code more accord with PEP-8 rule. --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 5050095..1050f68 100644 --- a/main.py +++ b/main.py @@ -21,4 +21,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() -- Gitee