flask 项目开发实战
前言
组内需要做一个项目,由于项目规模较小,就选择了小巧灵活的flask作为框架。此文记录了flask项目从开发到部署的一些经历。
所使用到的库
flask
flasgger:用于自动生成api文档
mysqlclient:mysql驱动
flask_sqlalchemy:mysql orm封装
sqlalchemy:mysql orm封装
flask_cors:跨域
elasticsearch:es驱动
pandas:数据处理,excel处理
requests:网络请求库
项目结构
|-- README.md |-- app |-- api api文件 |-- bin 定时任务、可执行文件 |-- lib 通用库 |-- model 模型 |-- run.py api入口文件 |-- test 测试文件 |-- bin 启停脚本 |-- config nginx配置、uwsgi配置 |-- document 存放生成到文档 |-- log 日志目录,包括项目自身日志、nginx日志、uwsgi日志 |-- requirements.txt |-- .gitignore
版本管理
git
__pycache__/ *.py[cod] *$py.class .idea/ *.log .env .venv .DS_Store config.py
- 请首先创建个人分支
- 每次提交代码前请合并master分支的代码到当前分支
- 测试请合并到 test分支,合并到test 分支后会自动触发构建并部署到测试服务器
- 测试无问题后请合并到master分支进行线上发布
配置管理
通过环境变量区分线上环境与测试环境
线上环境配置环境
check_env=$(grep "export FLASK_ENV=production" /etc/profile;echo $?) if [ "$check_env" -eq 0 ];then echo "FLASK_ENV=production exists in /etc/profile" else echo "export FLASK_ENV=production">>/etc/profile fi
通过环境变量选择不同的配置
CONFIG_OBJECT_MAPPER = { 'development': 'app.config.DevelopConfig', 'production': 'app.config.ProductionConfig', }
配置文件的样式,通过类的继承进行配置复用
class Config(object): DEBUG = False class ProductionConfig(Config): pass class DevelopConfig(Config): DEBUG = True TESTING = True PRESERVE_CONTEXT_ON_EXCEPTION = True ENV = "development" SQLALCHEMY_ECHO = True
部署
nginx+uwsgi
uwsgi配置
[uwsgi] module = app:name_app master = true #protocol = http reload-mercy = 8 max-requests = 1000 touch-reload = /tmp/reload.txt processes = 10 chdir = /xx/xx socket = 0.0.0.0:8000 logto = /xx/xx/uwsgi.log chmod-socket = 660 vacuum = true stats = /var/uwsgi.status pidfile = /var/uwsgi.pid
nginx配置
server { listen 80; server_name xx.com; access_log /xx/log/nginx_access.log main; error_log /xx/log/nginx_err.log; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; } location /document { root /xx/; } }
启动脚本,start.sh,凑合可用
#!/bin/bash set -x -u -o pipefail UWSGI_BIN=/xx/uwsgi NGINX_BIN=/xx/nginx NGINX_CONF=/xx/nginx.conf TGW_GOT_NGINX_CONF=/dxx/product_nginx_tgw_got.conf UWSGI_INI=/xx/product_uwsgi.ini DOCUMENT_PATH=/xx/document check_env=$(grep "export FLASK_ENV=production" /etc/profile;echo $?) if [ "$check_env" -eq 0 ];then echo "FLASK_ENV=production exists in /etc/profile" else echo "export FLASK_ENV=production">>/etc/profile fi # 如果uwsgi已经启动,则只进行重载配置;如果没有启动则启动 if pidof uwsgi then echo "uwsgi has been started, reload the config" touch /tmp/reload.txt else echo "start uwsgi ..." $UWSGI_BIN -d --ini $UWSGI_INI fi sleep 5 $UWSGI_BIN --connect-and-read /var/uwsgi.status ln -s /data/apps/tgwgot/config/product_nginx_tgw_got.conf $TGW_GOT_NGINX_CONF file $TGW_GOT_NGINX_CONF # 如果nginx已经启动,则只进行重载配置;如果没有启动则启动 if pidof nginx then echo "nginx has been started, reload the config" $NGINX_BIN -s reload -c $NGINX_CONF else echo "start nginx ..." $NGINX_BIN -c $NGINX_CONF fi chmod -R 755 $DOCUMENT_PATH pidof nginx
本作品采用《CC 协议》,转载必须注明作者和本文链接
分枝里的 “枝”,不是这个 “枝” 吧?