Skip to content

Commit 375bee5

Browse files
committed
feat: 通过环境变量区别加载不同的配置文件
1 parent 90ca4f6 commit 375bee5

5 files changed

Lines changed: 83 additions & 25 deletions

File tree

app/api/cms/user.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
:copyright: © 2019 by the Lin team.
55
:license: MIT, see LICENSE for more details.
66
"""
7-
import os
87
from operator import and_
98

10-
from flask import jsonify, current_app
9+
from flask import jsonify
1110
from flask_jwt_extended import create_access_token, get_jwt_identity, get_current_user, \
1211
create_refresh_token, verify_jwt_refresh_token_in_request
1312
from lin.core import manager, route_meta, Log

app/app.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@ def log_response(resp):
6262
return resp
6363

6464

65-
def create_app(register_all=True):
65+
def create_app(register_all=True, environment='production'):
6666
app = Flask(__name__, static_folder='./assets')
67-
app.config.from_object('app.config.setting')
68-
app.config.from_object('app.config.secure')
67+
app.config['ENV'] = environment
68+
env = app.config.get('ENV')
69+
if env == 'production':
70+
app.config.from_object('app.config.setting.ProductionConfig')
71+
app.config.from_object('app.config.secure.ProductionSecure')
72+
elif env == 'development':
73+
app.config.from_object('app.config.setting.DevelopmentConfig')
74+
app.config.from_object('app.config.secure.DevelopmentSecure')
6975
app.config.from_object('app.config.log')
7076
if register_all:
7177
register_blueprints(app)

app/config/secure.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
"""
55

66
# 安全性配置
7-
SQLALCHEMY_DATABASE_URI = 'mysql+cymysql://root:123456@localhost:3306/lin-cms'
7+
from app.config.setting import BaseConfig
88

9-
SQLALCHEMY_ECHO = False
109

11-
SECRET_KEY = '\x88W\xf09\x91\x07\x98\x89\x87\x96\xa0A\xc68\xf9\xecJJU\x17\xc5V\xbe\x8b\xef\xd7\xd8\xd3\xe6\x95*4'
10+
class DevelopmentSecure(BaseConfig):
11+
"""
12+
开发环境安全性配置
13+
"""
14+
SQLALCHEMY_DATABASE_URI = 'mysql+cymysql://root:123456@localhost:3306/lin-cms'
15+
16+
SQLALCHEMY_ECHO = False
17+
18+
SECRET_KEY = '\x88W\xf09\x91\x07\x98\x89\x87\x96\xa0A\xc68\xf9\xecJJU\x17\xc5V\xbe\x8b\xef\xd7\xd8\xd3\xe6\x95*4'
19+
20+
21+
class ProductionSecure(BaseConfig):
22+
"""
23+
生产环境安全性配置
24+
"""
25+
SQLALCHEMY_DATABASE_URI = 'mysql+cymysql://root:123456@localhost:3306/lin-cms'
26+
27+
SQLALCHEMY_ECHO = False
28+
29+
SECRET_KEY = '\x88W\xf09\x91\x07\x98\x89\x87\x96\xa0A\xc68\xf9\xecJJU\x17\xc5V\xbe\x8b\xef\xd7\xd8\xd3\xe6\x95*4'

app/config/setting.py

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,54 @@
55

66
from datetime import timedelta
77

8-
# 分页配置
9-
COUNT_DEFAULT = 10
10-
PAGE_DEFAULT = 0
11-
12-
# 令牌配置
13-
JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=1)
14-
15-
# 屏蔽 sql alchemy 的 FSADeprecationWarning
16-
SQLALCHEMY_TRACK_MODIFICATIONS = False
17-
18-
# 插件模块暂时没有开启,以下配置可忽略
19-
# plugin config写在字典里面
20-
PLUGIN_PATH = {
21-
'poem': {'path': 'app.plugins.poem', 'enable': True, 'version': '0.0.1', 'limit': 20},
22-
'oss': {'path': 'app.plugins.oss', 'enable': True, 'version': '0.0.1', 'access_key_id': 'not complete', 'access_key_secret': 'not complete', 'endpoint': 'http://oss-cn-shenzhen.aliyuncs.com', 'bucket_name': 'not complete', 'upload_folder': 'app', 'allowed_extensions': ['jpg', 'gif', 'png', 'bmp']}
23-
}
8+
9+
class BaseConfig(object):
10+
"""
11+
基础配置
12+
"""
13+
# 分页配置
14+
COUNT_DEFAULT = 10
15+
PAGE_DEFAULT = 0
16+
17+
# 屏蔽 sql alchemy 的 FSADeprecationWarning
18+
SQLALCHEMY_TRACK_MODIFICATIONS = False
19+
20+
21+
class DevelopmentConfig(BaseConfig):
22+
"""
23+
开发环境普通配置
24+
"""
25+
DEBUG = True
26+
27+
# 令牌配置
28+
JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=1)
29+
30+
# 插件模块暂时没有开启,以下配置可忽略
31+
# plugin config写在字典里面
32+
PLUGIN_PATH = {
33+
'poem': {'path': 'app.plugins.poem', 'enable': True, 'version': '0.0.1', 'limit': 20},
34+
'oss': {'path': 'app.plugins.oss', 'enable': True, 'version': '0.0.1', 'access_key_id': 'not complete',
35+
'access_key_secret': 'not complete', 'endpoint': 'http://oss-cn-shenzhen.aliyuncs.com',
36+
'bucket_name': 'not complete', 'upload_folder': 'app',
37+
'allowed_extensions': ['jpg', 'gif', 'png', 'bmp']}
38+
}
39+
40+
41+
class ProductionConfig(BaseConfig):
42+
"""
43+
生产环境普通配置
44+
"""
45+
DEBUG = False
46+
47+
# 令牌配置
48+
JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=1)
49+
50+
# 插件模块暂时没有开启,以下配置可忽略
51+
# plugin config写在字典里面
52+
PLUGIN_PATH = {
53+
'poem': {'path': 'app.plugins.poem', 'enable': True, 'version': '0.0.1', 'limit': 20},
54+
'oss': {'path': 'app.plugins.oss', 'enable': True, 'version': '0.0.1', 'access_key_id': 'not complete',
55+
'access_key_secret': 'not complete', 'endpoint': 'http://oss-cn-shenzhen.aliyuncs.com',
56+
'bucket_name': 'not complete', 'upload_folder': 'app',
57+
'allowed_extensions': ['jpg', 'gif', 'png', 'bmp']}
58+
}

starter.py

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

66
from app.app import create_app
77

8-
app = create_app()
8+
app = create_app(environment='development')
99

1010

1111
@app.route('/', methods=['GET'], strict_slashes=False)

0 commit comments

Comments
 (0)