From c0d963628b78a4aa15e49fd242120316ea5bede9 Mon Sep 17 00:00:00 2001 From: zhengxinonly Date: Sun, 23 Jan 2022 01:09:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=A7=BB=E9=99=A4=E7=94=9F=E4=BA=A7?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E6=96=87=E4=BB=B6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 141 +------------------- applications/__init__.py | 19 ++- applications/api/__init__.py | 15 --- applications/{api => }/rights/__init__.py | 0 applications/{api => }/rights/department.py | 0 applications/{api => }/rights/rights.py | 0 applications/{api => }/rights/roles.py | 0 applications/{api => }/system/__init__.py | 0 applications/{api => }/system/file.py | 0 applications/{api => }/system/passport.py | 0 applications/{api => }/users/__init__.py | 0 applications/{api => }/users/profile.py | 0 applications/{api => }/users/user.py | 0 gunicorn.conf.py | 19 --- start.sh | 3 - 15 files changed, 20 insertions(+), 177 deletions(-) delete mode 100644 applications/api/__init__.py rename applications/{api => }/rights/__init__.py (100%) rename applications/{api => }/rights/department.py (100%) rename applications/{api => }/rights/rights.py (100%) rename applications/{api => }/rights/roles.py (100%) rename applications/{api => }/system/__init__.py (100%) rename applications/{api => }/system/file.py (100%) rename applications/{api => }/system/passport.py (100%) rename applications/{api => }/users/__init__.py (100%) rename applications/{api => }/users/profile.py (100%) rename applications/{api => }/users/user.py (100%) delete mode 100644 gunicorn.conf.py delete mode 100644 start.sh diff --git a/README.md b/README.md index d643681..6463eba 100644 --- a/README.md +++ b/README.md @@ -113,141 +113,8 @@ flask db upgrade flask init-db ``` ++ 项目启动 -## 服务器部署 -### wsgi -默认的 flask 程序启动一个应用,一个应用只能用于测试环境下。生成环境采用 gunicorn 开启多进行+多线程启动程序,可以提升程序的并发量。详细配置可以查看 gunicorn 的官方文档,本项目的配置文件请查看 `gunicorn.conf.py` 。 - -```python -# filename: gunicorn.conf.py -import os -import multiprocessing - -bind = '0.0.0.0:8000' # 默认部署地址,如果用了nginx的反向代理,建议改成 127.0.0.1:8000 -backlog = 512 -chdir = os.path.dirname(os.path.abspath(__file__)) -timeout = 30 -worker_class = 'sync' - -workers = multiprocessing.cpu_count() * 2 + 1 # 开启进程数为 CPU核心数 * 2 + 1 -threads = 2 # 每个进程开启两个线程 -loglevel = 'info' # 日志的等级 -access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' -# 日志存放位置 -if not os.path.exists('logs'): - os.mkdir('logs') - -accesslog = os.path.join(chdir, "logs/gunicorn_access.log") -errorlog = os.path.join(chdir, "logs/gunicorn_error.log") - -``` - -因为是在服务器部署,而且一般做服务器部署的时候都会采用虚拟环境 + 命令行启动。为了能在命令行模式下从虚拟环境启动程序,所以需要专门编写一个 shell 文件进行启动。详细看看 `start.sh` - -```shell -cd /home/ubuntu/pear-admin-flask # 进入到项目的根目录 -source venv/bin/activate # 激活虚拟环境 -exec gunicorn -c gunicorn.conf.py "applications:create_app('development')" # 运行 gunicorn 指令启动程序 -``` - -注意:如果使用的是全局环境,就可以不用激活虚拟环境。 - - - -### 守护进程 - -程序在服务器环境下,一般是 7*24 小时不间断运行。有时候服务器会因为一些特殊原因宕机自动重启,或者是晚上定时重启电脑让内存处于最佳状态下。在这种情况下如果想要只要是电脑在正常运行,程序就提供服务,就需要开启守护进程。 - - - -使用下面的命令安装Supervisor: -```shell script -$ sudo apt install supervisor -``` - -编辑配置文件 - -```shell -$ sudo vim /etc/supervisor/conf.d/pear.conf -``` - -写入项目配置 -```shell script -[program:pear] -command=bash /home/ubuntu/pear-admin-flask/start.sh -user=root -autostart=true -autorestart=true -stopasgroup=true -killasgroup=true -``` - -管理项目进程 -```shell script -$ sudo supervisorctl -``` - - - -注意:关于 supervisor 的具体功法参考 [官方文档](http://supervisord.org/index.html) - - - -### Nginx - -gunicorn 虽然提供了 Web服务器的功能,但是功能比较局限,拓展性也不强。而 Nginx 就能很好的弥补这部分的不足。所以在服务器一般采用 Nginx + gunicorn 的模式做项目部署。 - -Nginx 是一个高性能的 HTTP 和 反向代理 web服务器。在后期的项目性能优化中,可以提供非常多的帮助。例如可以对返回的网页、静态文件进行压缩提升页面的加载速度,当性能达到瓶颈之后可以通过负载均衡让后端直接可以通过加机器解决问题。 - - - -使用下面的命令安装Nginx - -```shell -$ sudo apt install nginx -``` - -可以直接在 Nginx 的默认配置文件(/etc/nginx/nginx.conf)中写入程序配置,但通常情况下,为了便于组织,我们可以在/etc/nginx/sites-enabled/或是/etc/nginx/conf.d/目录下为我们的Flask程序创建单独的Nginx配置文件。 - -```shell -$ sudo rm /etc/nginx/sites-enabled/default # 删除默认的示例 -$ sudo vi /etc/nginx/sites-enabled/pear # 添加自己的示例 -``` - - - -> /etc/nginx/sites-enabled/pear - -```shell -server { - listen 80 default_server; # 监听 80 端口 - # server_name example.com; # 域名解析 - access_log /var/log/nginx/access.log; - error_log /var/log/nginx/error.log; - - location / { - proxy_pass http://127.0.0.1:8000; # 转发的地址,即Gunicorn运行的地址 - proxy_redirect off; - - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - } -} -``` - -更新配置文件后,我们可以通过下面的命令来测试语法正确性: - -```shell -$ sudo nginx -t -``` - -如果一切正常,那么现在可以重启Nginx让配置生效: - -```shell -$ sudo service nginx restart -``` - -当使用反向代理服务器后,Gunicorn不需要再监听外部请求,而是直接监听本地机的某个端口。我们可以使用默认值,即本地机的8000端口,默认绑定 `127.0.0.1:8000` 。 - +```bash +flask run +``` \ No newline at end of file diff --git a/applications/__init__.py b/applications/__init__.py index c2add1e..1f2f719 100644 --- a/applications/__init__.py +++ b/applications/__init__.py @@ -1,16 +1,29 @@ import os -from flask import Flask +from flask import Flask, Blueprint from common.flask_uploads import configure_uploads from common.utils.upload import photos from extensions import init_plugs from applications.view import init_view -from applications.api import init_api import config +api_bp: Blueprint = Blueprint('api', __name__, url_prefix='/api/v1') -def create_app(): +from .rights import register_rights_api +from .system import register_system_api +from .users import register_users_api + +register_rights_api(api_bp) +register_users_api(api_bp) +register_system_api(api_bp) + + +def init_api(app: Flask) -> None: + app.register_blueprint(api_bp) + + +def create_app() -> Flask: app = Flask('pear-admin-flask') # 引入数据库配置 diff --git a/applications/api/__init__.py b/applications/api/__init__.py deleted file mode 100644 index 1fbe596..0000000 --- a/applications/api/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -from flask import Blueprint, Flask - -api_bp = Blueprint('api', __name__, url_prefix='/api/v1') - -from .rights import register_rights_api -from .system import register_system_api -from .users import register_users_api - -register_rights_api(api_bp) -register_users_api(api_bp) -register_system_api(api_bp) - - -def init_api(app: Flask): - app.register_blueprint(api_bp) diff --git a/applications/api/rights/__init__.py b/applications/rights/__init__.py similarity index 100% rename from applications/api/rights/__init__.py rename to applications/rights/__init__.py diff --git a/applications/api/rights/department.py b/applications/rights/department.py similarity index 100% rename from applications/api/rights/department.py rename to applications/rights/department.py diff --git a/applications/api/rights/rights.py b/applications/rights/rights.py similarity index 100% rename from applications/api/rights/rights.py rename to applications/rights/rights.py diff --git a/applications/api/rights/roles.py b/applications/rights/roles.py similarity index 100% rename from applications/api/rights/roles.py rename to applications/rights/roles.py diff --git a/applications/api/system/__init__.py b/applications/system/__init__.py similarity index 100% rename from applications/api/system/__init__.py rename to applications/system/__init__.py diff --git a/applications/api/system/file.py b/applications/system/file.py similarity index 100% rename from applications/api/system/file.py rename to applications/system/file.py diff --git a/applications/api/system/passport.py b/applications/system/passport.py similarity index 100% rename from applications/api/system/passport.py rename to applications/system/passport.py diff --git a/applications/api/users/__init__.py b/applications/users/__init__.py similarity index 100% rename from applications/api/users/__init__.py rename to applications/users/__init__.py diff --git a/applications/api/users/profile.py b/applications/users/profile.py similarity index 100% rename from applications/api/users/profile.py rename to applications/users/profile.py diff --git a/applications/api/users/user.py b/applications/users/user.py similarity index 100% rename from applications/api/users/user.py rename to applications/users/user.py diff --git a/gunicorn.conf.py b/gunicorn.conf.py deleted file mode 100644 index 4aa61bf..0000000 --- a/gunicorn.conf.py +++ /dev/null @@ -1,19 +0,0 @@ -import os -import multiprocessing - -bind = '0.0.0.0:8000' -backlog = 512 -chdir = os.path.dirname(os.path.abspath(__file__)) -timeout = 30 -worker_class = 'sync' - -workers = multiprocessing.cpu_count() * 2 + 1 -threads = 2 -loglevel = 'info' -access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' - -if not os.path.exists('logs'): - os.mkdir('logs') - -accesslog = os.path.join(chdir, "logs/gunicorn_access.log") -errorlog = os.path.join(chdir, "logs/gunicorn_error.log") diff --git a/start.sh b/start.sh deleted file mode 100644 index 79a3b6c..0000000 --- a/start.sh +++ /dev/null @@ -1,3 +0,0 @@ -cd /home/ubuntu/pear-admin-flask -source venv/bin/activate -exec gunicorn -c gunicorn.conf.py "applications:create_app('development')"