-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstocker.py
86 lines (66 loc) · 2.52 KB
/
stocker.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
import os
import json
import logging
from logging.handlers import TimedRotatingFileHandler
from datetime import datetime
from flask import (request, jsonify, make_response)
from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint
from app import create_app, db
from app.utils.slack_manager import SlackManager
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
HOST = os.environ.get('HOST', '0.0.0.0')
PORT = os.environ.get('PORT')
app.config['JSON_AS_ASCII'] = False
# Logger setup
logger = logging.getLogger()
BASIC_FORMAT = '%(asctime)s %(levelname)- 8s in %(module)s: %(message)s'
DATE_FORMAT = '%Y-%m-%d %H:%M'
formatter = logging.Formatter(BASIC_FORMAT, DATE_FORMAT)
@app.route("/spec")
def spec():
base_path = os.path.join(app.root_path, 'docs')
swag = swagger(app, from_file_keyword="swagger_from_file",
base_path=base_path)
swag['info']['version'] = "v0.1"
swag['info']['title'] = "Stocker API"
swag['info']['$ref'] = 'resources.yml'
return jsonify(swag)
# @app.after_request
# def setup_access_conrtol(response):
# response.headers['Access-Control-Allow-Origin'] = 'https://localhost:3001'
# response.headers['Access-Control-Allow-Credentials'] = 'true'
# response.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
# response.headers['Access-Control-Allow-Headers'] =\
# 'Content-Type, Authorization'
# return response
@app.errorhandler(404)
def pageNotfound(error):
logging.info('Page not found: %s', (request.path))
return make_response(json.dumps('404 not found'), 404)
@app.errorhandler(500)
def internalServerError(error):
error_log = f'Server Error: {error}'
SlackManager().push_notification('Stocker', error_log)
logger.error(error_log)
return make_response(json.dumps('500 server error'), 500)
@app.shell_context_processor
def make_shell_context():
return dict(db=db)
if __name__ == '__main__':
app.debug = True
basedir = os.path.abspath(os.path.dirname(__file__))
logdir = os.path.join(basedir, 'log')
if os.path.exists(logdir) is False:
os.mkdir(logdir)
log_filename = datetime.now().strftime("log/app %Y-%m-%d.log")
fileHandler = TimedRotatingFileHandler(
log_filename, when='D', interval=1,
backupCount=30, encoding='UTF-8', delay=False, utc=False)
fileHandler.setFormatter(formatter)
fileHandler.setLevel(logging.WARNING)
logger.addHandler(fileHandler)
app.run(
host=HOST,
port=PORT,
threaded=True)