-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
103 lines (75 loc) · 2.27 KB
/
app.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
# -*- coding: utf-8 -*-
from flask import request
from flask import Flask
from flask import g
import flask
import md5
import json
import logging
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import os
import random
import redis
from libs.response_meta import ResponseMeta
from libs.mysql import Mysql
from views import auth
from views import qr
from views import contact
from views import avatar
import config
app = Flask(__name__)
app.debug = config.DEBUG
rds = redis.StrictRedis(host=config.REDIS_HOST, password=config.REDIS_PASSWORD,
port=config.REDIS_PORT, db=config.REDIS_DB)
def before_request():
logging.debug("before request")
g.rds= rds
cnf = config.MYSQL
db = getattr(g, '_db', None)
if db is None:
g._db = Mysql(*cnf)
def app_teardown(exception):
logging.debug('app_teardown')
# 捕获teardown时的mysql异常
try:
db = getattr(g, '_db', None)
if db:
db.close()
except Exception:
pass
def http_error_handler(err):
logging.error(err)
return ResponseMeta(code=err.code, http_code=err.code)
def response_meta_handler(response_meta):
return response_meta.get_response()
def generic_error_handler(err):
logging.exception(err)
return ResponseMeta(http_code=500, description='Server Internal Error!')
def init_logger(logger):
root = logger
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(filename)s:%(lineno)d - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
# 初始化接口
def init_app(app):
app.teardown_appcontext(app_teardown)
app.before_request(before_request)
for error in range(400, 420) + range(500, 506):
app.error_handler_spec[None][error] = http_error_handler
app.register_error_handler(ResponseMeta, response_meta_handler)
app.register_error_handler(Exception, generic_error_handler)
app.register_blueprint(auth.app)
app.register_blueprint(qr.app)
app.register_blueprint(contact.app)
app.register_blueprint(avatar.app)
random.seed()
log = logging.getLogger('')
init_logger(log)
init_app(app)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8888)