Skip to content

常规APP项目初始化

先初始化目录结构


# 将代码目录转化python包
touch app/__init__.py
touch views/__init__.py
touch views/ms_jk/__init__.py
# 初始化代码文件和配置文件
touch instance/config.py
touch instance/.env
touch wsgi.py README.md app/wiews/ms_jk/submit.py

# 目录结构
tree .
.
├── app
│   ├── __init__.py
│   ├── static/         # otter
│   ├── templates/      # otter
│   └── views
│       ├── __init__.py
│       └── ms_jk       # 该目录下开发项目接口
│           ├── __init__.py
│           └── submit.py
├── instance
│   ├── log             # 日志输出目录
│   ├── config.py       # 项目配置文件
│   └── .env            # 项目基本环境变量配置
├── README.md
├── requirements.txt
└── wsgi.py

填充骨架代码

1. app/__init__.py

内容如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
from flask_cors import CORS
from flask import Flask, render_template
from hippo.web import AppInit
from hippo.web import exception_handle_register
from hippo.web import blueprints_dynamic_register

__VERSION = (2, 0, 0)
__VERSION__ = ".".join(map(lambda x: str(x), __VERSION))


def create_app():
    app = Flask(__name__)
    AppInit().prepare(app)

    blueprints_dynamic_register(os.path.dirname(__file__), app)

    # r'/*' 是通配符,让本服务器所有的URL 都允许跨域请求
    CORS(app, resources=r'/*')

    @app.route('/', methods=['GET'])
    @app.route('/tool_detail', methods=['GET'])
    def index():
        return render_template('index.html')

    exception_handle_register(app)
    return app

2. wsgi.py

该文件是用来启动的入口文件,在集群服务中可以使用 gunicorn 代理

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import logging
from app import create_app

flask_app = create_app()

if __name__ == '__main__':
    flask_app.run(
        host=flask_app.config.get("HOST"),
        port=flask_app.config.get("PORT"),
        debug=flask_app.config.get("DEBUG")
    )
else:
    # 使用gunicorn启动时, 将flask应用中的日志绑定到 gunicorn 的日志配置中
    gunicorn_logger = logging.getLogger('gunicorn.error')
    flask_app.logger.handlers = gunicorn_logger.handlers
    flask_app.logger.setLevel(gunicorn_logger.level)

3. app/views/ms_jk/__init__.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask import Blueprint

bp = Blueprint('views_bp', __name__)

from .submit import submit
# 需要将开发的接口导入
from .demo import demo # 需要开发人员开发

3. app/views/ms_jk/submit.py

非任务应用的提交接口,增加了平台日志监控功能(platform_monitor)

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from app.views.ms_jk import bp
from hippo.web import DataAnalysisSubmit
from hippo.log_tracking import platform_monitor


@bp.route("/submit", methods=['POST'])
@platform_monitor
def submit():
    das = DataAnalysisSubmit()
    return das.submit()

配置项目参数 instance/config.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

PORT = 5000

PLATFORM_URL = f"http://192.168.0.147:8004/platform/v3"       # 平台的接口URL
PLATFORM_DATA_DIR = "/home/lx/pub/platform_data_dir/local"  # 一个有读写权限的目录

配置项以实际情况配置,示例仅做演示说明,不可照搬配置项的值,各配置项说明如下:

  • PORT: 项目启动占用的端口,使用k8s时,该端口默认5000,端口由k8s管理
  • PLATFORM_URL: 平台的访问地址,应用与平台通信的接口配置
  • PLATFORM_DATA_DIR: 应用如果有输出,将会使用的共享存储位置(本地模式配置本机路径)

这里对配置项 `PLATFORM_URL` 额外说明一下,目前小工具需要依赖genostack平台运行, 所以开发时需要有本地的genostack平台环境,关于genostack平台的本地测试环境这里不在赘述, 有需要的客户与项目负责人联系。 另外,正式的项目还有一些别的配置项需要配置,这些配置后续再深入阐述。

启动并初步验证

启动

$python wsgi.py
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.0.114:5000
INFO:werkzeug:Press CTRL+C to quit
INFO:werkzeug: * Restarting with stat
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger PIN: 100-862-322

验证,出现版本信息则应用及环境搭建成功

curl http://192.168.0.114:5000/ms_jk/query_app_version
{
  "app_version": "2.0.0", 
  "web_version": "20241210", 
  "hippo_version": "1.6.15"
}