forked from wtx358/wtxlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
230 lines (175 loc) · 6.26 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
import os
import logging
from hashlib import md5
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
THEME = 'imtx'
SITE_NAME = u'wtxlog'
# 是否启用博客模式
BLOG_MODE = True
BODY_FORMAT = 'html' # html or markdown
SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
SQLALCHEMY_RECORD_QUERIES = True
SQLALCHEMY_POOL_RECYCLE = 10
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
MAIL_SERVER = os.environ.get('MAIL_SERVER')
MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25)
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
# gmail的话带下面那行的注释去掉
#MAIL_USE_TLS = True
# qq企业邮箱的话带下面那行的注释去掉
#MAIL_USE_SSL = True
APP_MAIL_SUBJECT_PREFIX = '[%s]' % SITE_NAME
APP_MAIL_SENDER = '%s Admin <%s>' % (SITE_NAME, MAIL_USERNAME)
APP_ADMIN = os.environ.get('APP_ADMIN')
# cache keys
_PREFIX = md5(SECRET_KEY).hexdigest()[7:15]
MEMCACHE_KEY = _PREFIX + '_view_%s'
STATIC_KEY = _PREFIX + '_static_%s'
# QiNiu Cloud Storage
QINIU_AK = os.environ.get('QINIU_AK')
QINIU_SK = os.environ.get('QINIU_SK')
QINIU_BUCKET = os.environ.get('QINIU_BUCKET')
@staticmethod
def get_mailhandler():
# email errors to the administrators
from app.ext import MySMTPHandler
credentials = None
secure = None
use_ssl = False
if getattr(Config, 'MAIL_USERNAME', None) is not None:
credentials = (Config.MAIL_USERNAME, Config.MAIL_PASSWORD)
if getattr(Config, 'MAIL_USE_TLS', None):
secure = ()
use_ssl = getattr(Config, 'MAIL_USE_SSL', False)
mail_handler = MySMTPHandler(
mailhost=(Config.MAIL_SERVER, Config.MAIL_PORT),
fromaddr=Config.APP_MAIL_SENDER,
toaddrs=[Config.APP_ADMIN],
subject=Config.APP_MAIL_SUBJECT_PREFIX + ' Application Error',
credentials=credentials,
secure=secure,
use_ssl=use_ssl
)
mail_handler.setLevel(logging.ERROR)
return mail_handler
@staticmethod
def init_app(app):
pass
class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_ECHO = True
SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
'sqlite:///' + os.path.join(basedir, 'data_dev_sqlite.db')
@classmethod
def init_app(cls, app):
Config.init_app(app)
mail_handler = Config.get_mailhandler()
app.logger.addHandler(mail_handler)
class BAEConfig(Config):
BAE_AK = ''
BAE_SK = ''
# BAE MEMCACHE
CACHE_USER = BAE_AK
CACHE_PWD = BAE_SK
CACHE_ADDR = 'cache.duapp.com:20243'
CACHE_ID = ''
# mysql config
MYSQL_USER = BAE_AK
MYSQL_PASS = BAE_SK
MYSQL_HOST = 'sqld.duapp.com'
MYSQL_PORT = '4050'
MYSQL_DB = ''
SQLALCHEMY_DATABASE_URI = 'mysql://%s:%s@%s:%s/%s' % (
MYSQL_USER, MYSQL_PASS, MYSQL_HOST, MYSQL_PORT, MYSQL_DB
)
@classmethod
def init_app(cls, app):
Config.init_app(app)
mail_handler = Config.get_mailhandler()
app.logger.addHandler(mail_handler)
def _create_logger(bufcount=256):
_logger = logging.getLogger()
_logger.setLevel(logging.DEBUG)
from bae_log import handlers
_handler = handlers.BaeLogHandler(
ak=app.config.get('BAE_AK'),
sk=app.config.get('BAE_SK'),
bufcount=bufcount,
)
return _handler
_logger = _create_logger(1)
app.logger.addHandler(_logger)
class SAEConfig(Config):
try:
# 如果是SAE环境,请把下面3行的注释去掉
from sae.const import (
MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASS, MYSQL_DB
)
SQLALCHEMY_DATABASE_URI = 'mysql://%s:%s@%s:%s/%s' % (
MYSQL_USER, MYSQL_PASS, MYSQL_HOST, MYSQL_PORT, MYSQL_DB
)
except:
pass
@classmethod
def init_app(cls, app):
Config.init_app(app)
mail_handler = Config.get_mailhandler()
app.logger.addHandler(mail_handler)
_handler = logging.StreamHandler()
app.logger.addHandler(_handler)
class JAEConfig(Config):
# mysql config
MYSQL_USER = ''
MYSQL_PASS = ''
MYSQL_HOST = ''
MYSQL_PORT = ''
MYSQL_DB = ''
SQLALCHEMY_DATABASE_URI = 'mysql://%s:%s@%s:%s/%s' % (
MYSQL_USER, MYSQL_PASS, MYSQL_HOST, MYSQL_PORT, MYSQL_DB
)
@classmethod
def init_app(cls, app):
Config.init_app(app)
mail_handler = Config.get_mailhandler()
app.logger.addHandler(mail_handler)
# http://jae.jd.com/help/create_app.html?targ=94#a32
# logfile path: /home/vcap/app/logs/jae.log
# this logfile would be clean when app rebuild or restart
_log_file = os.path.join(os.getenv('HOME'), 'logs/jae.log')
file_handler = logging.FileHandler(_log_file)
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s',
'%Y-%m-%d %H:%M:%S')
file_handler.setFormatter(formatter)
app.logger.addHandler(file_handler)
class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'sqlite:///' + os.path.join(basedir, 'data_sqlite.db')
# mysql config
MYSQL_USER = ''
MYSQL_PASS = ''
MYSQL_HOST = ''
MYSQL_PORT = ''
MYSQL_DB = ''
if len(MYSQL_USER)>0 and len(MYSQL_PASS)>0 and len(MYSQL_HOST)>0 \
and len(MYSQL_PORT)>0 and len(MYSQL_DB)>0:
SQLALCHEMY_DATABASE_URI = 'mysql://%s:%s@%s:%s/%s' % (
MYSQL_USER, MYSQL_PASS, MYSQL_HOST, MYSQL_PORT, MYSQL_DB
)
@classmethod
def init_app(cls, app):
Config.init_app(app)
mail_handler = Config.get_mailhandler()
app.logger.addHandler(mail_handler)
config = {
'bae': BAEConfig,
'default': DevelopmentConfig,
'development': DevelopmentConfig,
'jae': JAEConfig,
'local': DevelopmentConfig,
'production': ProductionConfig,
'sae': SAEConfig,
}