-
XXXProject
-
manage.py
-
import os
from ZpcProject import create_app
from flask_script import Manager
env = os.environ.get("FLASK_PORJECT") or "default"
app = create_app(env)
manager = Manager(app)
if __name__ == '__main__':
manager.run()
-
-
XXXProject
-
__init__
-
创建Flask对象
-
加载配置
-
加载各种第三方插件(除路由)
-
加载中间件
-
加载路由,对外暴漏入口,暴漏路由
from flask import Flask
from ZpcProject.config import envs
from ZpcProject.extension import init_ext
from ZpcProject.middleware import load_middleware
from ZpcProject.route import init_route
def create_app(env):
app = Flask(__name__,static_folder="../static",template_folder="../templates")
# 加载配置
app.config.from_object(envs.get(env))
# 加载第三方插件
init_ext(app)
# 加载中间件
load_middleware(app)
# 加载路由
init_route(app)
return app
-
-
config 或 settings
def get_db_uri(db_info):
database = db_info.get("DATABASE")
driver = db_info.get("DRIVER")
user = db_info.get("USER")
password = db_info.get("PASSWORD")
host = db_info.get("HOST")
port = db_info.get("PORT")
name = db_info.get("NAME")
return "{}+{}://{}:{}@{}:{}/{}".format(database,driver,user,password,host,port,name)
class BaseConfig:
DEBUG = False
TESTING = False
SECRET_KEY = "SDFGHGSAGDFHSDFHVNrtgsdhgsd"
SQLALCHEMY_TRACK_MODIFICATIONS = False
class DevelopConfig(BaseConfig):
DEBUG = True
db_info = {
"DATABASE":"mysql",
"DRIVER":"pymysql",
"USER":"root",
"PASSWORD":"",
"HOST":"localhost",
"PORT":"3306",
"NAME":"zpc"
}
SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)
class TestingConfig(BaseConfig):
TESTING = True
db_info = {
"DATABASE":"mysql",
"DRIVER":"pymysql",
"USER":"root",
"PASSWORD":"",
"HOST":"localhost",
"PORT":"3306",
"NAME":"zpc"
}
SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)
class StaginConfig(BaseConfig):
db_info = {
"DATABASE":"mysql",
"DRIVER":"pymysql",
"USER":"root",
"PASSWORD":"",
"HOST":"localhost",
"PORT":"3306",
"NAME":"zpc"
}
SQLALCHEMY_DATABASE_URI = get_db_uri(db_info)
class OnlineConfig(BaseConfig):
db_info = {
"DATABASE":"mysql",
"DRIVER":"pymysql",
"USER":"root",
"PASSWORD":"",
"HOST":"localhost",
-
-