-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (79 loc) · 2.72 KB
/
main.py
File metadata and controls
93 lines (79 loc) · 2.72 KB
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
from eve import Eve
from prometheus_flask_exporter import PrometheusMetrics
from prometheus_client import Counter
from flask import jsonify, session, request
from flask import jsonify
from flask_jwt_extended import JWTManager
from auth import JWTAuth, auth
import settings
from flask_cors import CORS
from flask_compress import Compress
if settings.USE_AUTH:
app = Eve('api', auth=JWTAuth, media=settings.MEDIA_STORAGE)
else:
app = Eve('api', media=settings.MEDIA_STORAGE)
CORS(app)
Compress(app)
# DEBUG must be off or DEBUG_METRICS on
metrics = PrometheusMetrics(
app,
group_by='url_rule',
excluded_paths=['/favicon.ico'],
defaults_prefix='#no_prefix',
default_labels={
'isError': lambda r: r.status_code>=400,
'type': lambda: request.scheme,
'addr': lambda: request.url_rule if request.url_rule!= None else '/' + request.path.split('/')[1] + '/<param>'
}
)
metrics.info(
'application_info',
"records static application info such as it's semantic version number",
version=app.config['APP_VERSION'],
app=app.name
)
metrics_request_size = Counter(
"response_size_bytes",
"counts the size of each http response",
["type", "status", "isError", "method", "addr"],
registry=metrics.registry
)
app.register_blueprint(settings.swagger)
app.register_blueprint(auth)
__jwt_manager__ = JWTManager(app)
@__jwt_manager__.user_claims_loader
def add_claims_to_access_token(user):
"""
Create a function that will be called whenever create_access_token
is used. It will take whatever object is passed into the
create_access_token method, and lets us define what custom claims
should be added to the access token.
"""
return {'role': user.role, 'session': session.get('data')}
@__jwt_manager__.user_identity_loader
def user_identity_lookup(user):
"""
Create a function that will be called whenever create_access_token
is used. It will take whatever object is passed into the
create_access_token method, and lets us define what the identity
of the access token should be.
"""
return user.user_id
@app.route('/favicon.ico', methods=['GET'])
def favicon():
"""
Just a positive favicon
"""
return jsonify(status='OK'), 200
def after_request(response):
metrics_request_size.labels(
request.scheme,
response.status_code,
response.status_code>=400,
request.method,
request.url_rule if request.url_rule!= None else '/' + request.path.split('/')[1] + '/<param>',
).inc(int(response.headers.get("Content-Length", 0)))
return response
app.after_request(after_request)
if __name__ == '__main__':
app.run(debug=settings.DEBUG, host='0.0.0.0', port=settings.PORT)