一:项目简介
Flask是Python编写的轻量级Web开发框架,因其良好的可扩展性,意味着,你可以写出你自己随心所欲的代码。
其使用jinja模板引擎和Werkzeug WSGI 套件,在安全性方面效果明显。
详细文档收藏地址:https://dormousehole.readthedocs.io/en/latest/
本项目实现了密码加密,token认证,自定义orm框架,自定义持久化存储等的功能,结合linux环境部署使用。
二:执行文件目录
主目录文件夹如下:
[root@rainsty flaskApped]# ll
total 60
-rw-r--r-- 1 root root 1235 Jan 10 16:02 appManage.py
drwxr-xr-x 3 root root 4096 Jan 10 16:38 config
drwxr-xr-x 3 root root 4096 Jan 10 16:38 controller
drwxr-xr-x 5 root root 4096 Jan 10 16:38 file
drwxr-xr-x 2 root root 4096 Jan 10 16:38 html
drwxr-xr-x 3 root root 4096 Jan 10 16:38 lib
drwxr-xr-x 2 root root 4096 Jan 10 16:38 logs
drwxr-xr-x 3 root root 4096 Jan 10 16:38 middleWare
drwxr-xr-x 3 root root 4096 Jan 10 16:38 module
-rw-r--r-- 1 root root 210 Dec 29 20:35 nohup.out
drwxr-xr-x 2 root root 4096 Jan 10 16:38 python
drwxr-xr-x 5 root root 4096 Jan 10 16:38 rainDB
drwxr-xr-x 2 root root 4096 Jan 10 16:38 register
-rw-r--r-- 1 root root 854 Dec 29 20:35 startApp.sh
-rw-r--r-- 1 root root 705 Dec 29 20:35 stopApp.sh
----appManage.py:主程序。
----config:配置信息目录,主要是日志配置文件。
----controller:对应mvc的controller层控制目录。
----file:静态文件目录,主要是前端页面的js,css,静态文件。
----html:html文件,对应mvc的views层展示目录。
----lib:原创orm包目录。
----log:日志文件目录。
----middleWare:自定义安全过滤层控制代码,在主程序中当作装饰器使用。
----module:对应mvc的module层模型目录。
----nohup.out:nohup后台启动输出文件。
----python安装目录,这里暂为空文件。
----rainDB:自定义数据存储引擎及目录。
----register:用户注册接口存放目录,注册功能不对外开发。
----startApp.sh:主程序启动脚本。
----stopApp.sh:主程序停止脚本。
目录树如下:
[root@rainsty flaskApped]# tree
.
├── appManage.py
├── config
│ ├── __pycache__
│ │ └── rainConfig.cpython-36.pyc
│ └── rainConfig.py
├── controller
│ ├── __pycache__
│ │ └── rainController.cpython-36.pyc
│ └── rainController.py
├── file
│ ├── css
│ │ └── entrance.css
│ ├── img
│ │ └── entrance.png
│ └── js
│ ├── entrance.js
│ └── min.js
├── html
│ ├── entergoin.html
│ ├── entrance.html
│ └── error.html
├── lib
│ └── raindborm
│ ├── __init__.py
│ ├── __main.py
│ └── __pycache__
│ ├── __init__.cpython-36.pyc
│ ├── __main.cpython-36.pyc
│ └── main.cpython-36.pyc
├── logs
│ └── rainLog.log
├── middleWare
│ ├── __pycache__
│ │ └── rainMiddleWare.cpython-36.pyc
│ └── rainMiddleWare.py
├── module
│ ├── __pycache__
│ │ └── rainModuleUser.cpython-36.pyc
│ └── rainModuleUser.py
├── nohup.out
├── python
│ └── info.txt
├── rainDB
│ ├── bin
│ │ └── rainDB-shell
│ ├── data
│ │ └── rain
│ │ └── rainModuleUser.rdb
│ └── logs
│ └── rain
│ └── rainModuleUser.log
├── register
│ └── rainRegister.py
├── startApp.sh
└── stopApp.sh
25 directories, 30 files
三:主程序介绍
----appManage.py, 原创代码:
#!./python/bin/python3
import os
import sys
import logging
from flask import Flask, request
from controller.rainController import *
from middleWare import rainMiddleWare as Ware
from config import rainConfig
app = Flask(__name__, static_url_path='', static_folder='file', template_folder='html')
app.config['SECRET_KEY'] = os.urandom(24)
app.config['RAIN_USER'] = './db/rainUser.db'
app.config['RAINDB_CONF'] = {'path': './rainDB', 'database': 'rain'}
app.logger = logging
app.logger.basicConfig(level=logging.INFO, **rainConfig.logConf())
@app.route('/rainenrollment', methods=['POST'])
@Ware.rainEnrollmentMW(request)
def enrollment():
return rainEnrollment(request.json)
@app.route('/rain', methods=['GET', 'POST'])
@Ware.rainEntranceMW(request)
def entrance():
return rainEntrance(request.form.to_dict())
@app.route('/rainentergoin', methods=['GET', 'POST'])
@Ware.rainEntergoinMW(request, "input_entergoin")
def entergoin():
return rainVerifyInfo(request.form.to_dict())
@app.errorhandler(404)
def page_not_found(error):
return render_template('error.html', error=error), 404
def main(args):
app.run('0.0.0.0', int(args))
if __name__ == '__main__':
main(sys.argv[1])
四:项目使用
项目部署当然是越简单也好,把复杂的命令转化为一条命令,或者一个执行文件,对用户体验好感度有很好的提升。下面是本demo的使用方式:
[root@rainsty flaskApp]# ll
total 56
-rwxr-xr-x 1 root root 1235 Jan 2 09:48 appManage.py
drwxr-xr-x 3 root root 4096 Jan 2 09:51 config
drwxr-xr-x 3 root root 4096 Jan 2 09:52 controller
drwxr-xr-x 5 root root 4096 Dec 20 22:13 file
drwxr-xr-x 2 root root 4096 Dec 20 22:13 html
drwxr-xr-x 3 root root 4096 Dec 27 19:07 lib
drwxr-xr-x 2 root root 4096 Jan 10 16:59 logs
drwxr-xr-x 3 root root 4096 Jan 2 09:53 middleWare
drwxr-xr-x 3 root root 4096 Jan 2 09:53 module
drwxr-xr-x 7 root root 4096 Dec 20 20:58 python
drwxr-xr-x 5 root root 4096 Dec 28 18:04 rainDB
drwxr-xr-x 2 root root 4096 Jan 2 09:56 register
-rwxr-xr-x 1 root root 854 Dec 29 20:30 startApp.sh
-rwxr-xr-x 1 root root 705 Dec 29 20:30 stopApp.sh
[root@rainsty flaskApp]# ps -ef | grep python
root 788 1 0 2018 ? 00:02:39 /usr/bin/python -Es /usr/sbin/tuned -l -P
root 30587 29281 0 17:00 pts/0 00:00:00 grep --color=auto python
[root@rainsty flaskApp]# ./startApp.sh
python/bin/python3 appManage.py 8001
nohup: appending output to ‘nohup.out’
App service startup successful!
[root@rainsty flaskApp]# ps -ef | grep python
root 788 1 0 2018 ? 00:02:39 /usr/bin/python -Es /usr/sbin/tuned -l -P
root 30601 1 2 17:00 pts/0 00:00:00 ./python/bin/python3 appManage.py 8001
root 30613 29281 0 17:00 pts/0 00:00:00 grep --color=auto python
[root@rainsty flaskApp]# ./stopApp.sh
App service stop successful!
[root@rainsty flaskApp]# ps -ef | grep python
root 788 1 0 2018 ? 00:02:39 /usr/bin/python -Es /usr/sbin/tuned -l -P
root 30649 29281 0 17:00 pts/0 00:00:00 grep --color=auto python
[root@rainsty flaskApp]#
----startApp.sh启动脚本如下:
#!/bin/bash
PORT=$1
if [ ! $PORT ];then
PORT=8001
fi
processInfo="python/bin/python3 appManage.py $PORT"
function start(){
if [ -e 'nohup.out' ];then
date=`date +%Y%m%d%H%M%S`
mv nohup.out ./logs/nohup.out.bak${date}
unset date
fi
enterPrt=$"\n"
echo ${processInfo}
nohup ./${processInfo} &
enterPrint=$(echo -e $enterPrt)
echo $enterPrint
unset enterPrt
unset enterPrint
}
function verify(){
process=`ps -ef | grep "${processInfo}" | grep -v grep`
if [[ -z $process ]];then
return 1
else
return 0
fi
}
verify
if [ $? -eq 0 ];then
echo "App has started, please stop first!"
exit 1
fi
start
verify
if [ ! $? -eq 0 ];then
echo "App service startup failed!"
exit 2
else
echo "App service startup successful!"
fi
unset processInfo
五:源码地址分享
源码地址:Github:[https://github.com/Rainstyed/rainsty/tree/master/FlaskApp/flaskApp]