Skip to content

Commit e709ced

Browse files
committed
chore:调整create_app导入路径
1 parent d866d6c commit e709ced

6 files changed

Lines changed: 66 additions & 70 deletions

File tree

app/__init__.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,65 @@
22
:copyright: © 2020 by the Lin team.
33
:license: MIT, see LICENSE for more details.
44
"""
5+
6+
from dotenv import load_dotenv
7+
from flask import Flask
8+
9+
10+
def register_blueprints(app):
11+
from app.api.cms import create_cms
12+
from app.api.v1 import create_v1
13+
14+
app.register_blueprint(create_v1(), url_prefix="/v1")
15+
app.register_blueprint(create_cms(), url_prefix="/cms")
16+
17+
18+
def register_api(app):
19+
from lin.apidoc import api
20+
21+
api.register(app)
22+
23+
24+
def apply_cors(app):
25+
from flask_cors import CORS
26+
27+
CORS(app)
28+
29+
30+
def load_app_config(app):
31+
"""
32+
根据指定配置环境自动加载对应环境变量和配置类到app config
33+
"""
34+
# 根据传入环境加载对应配置
35+
env = app.config.get("ENV")
36+
# 读取 .env
37+
load_dotenv(".{env}.env".format(env=env))
38+
# 读取配置类
39+
app.config.from_object(
40+
"app.config.{env}.{Env}Config".format(env=env, Env=env.capitalize())
41+
)
42+
43+
44+
def set_global_config(**kwargs):
45+
from lin.config import global_config
46+
47+
# 获取config_*参数对象并挂载到脱离上下文的global config
48+
for k, v in kwargs.items():
49+
if k.startswith("config_"):
50+
global_config[k[7:]] = v
51+
52+
53+
def create_app(register_all=True, **kwargs):
54+
# http wsgi server托管启动需指定读取环境配置
55+
load_dotenv(".flaskenv")
56+
app = Flask(__name__, static_folder="../assets")
57+
load_app_config(app)
58+
if register_all:
59+
from lin import Lin
60+
61+
set_global_config(**kwargs)
62+
register_blueprints(app)
63+
register_api(app)
64+
apply_cors(app)
65+
Lin(app, **kwargs)
66+
return app

app/app.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

app/cli/plugin/init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import subprocess
88
from importlib import import_module
99

10-
from app.app import create_app
10+
from app import create_app
1111

1212
"""
1313
插件初始化流程:

app/plugin/poem/app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def initial_data():
1010
from lin.db import db
1111

12-
from app.app import create_app
12+
from app import create_app
1313

1414
app = create_app()
1515
with app.app_context():

starter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
:license: MIT, see LICENSE for more details.
44
"""
55

6-
from app.app import create_app
6+
from app import create_app
77
from app.cli import db_cli, plugin_cli
88
from app.config.code_message import MESSAGE
99
from app.config.http_status_desc import DESC

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from app.app import create_app
6+
from app import create_app
77
from app.model.lin import (
88
Group,
99
GroupPermission,

0 commit comments

Comments
 (0)