-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_api.py
77 lines (65 loc) · 1.99 KB
/
run_api.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
import os
import sys
import unittest
import arrow
from flask import request
from dotenv import load_dotenv
from flask_migrate import Migrate
load_dotenv()
from api import create_app, db
from api.model import Student, Level, School, Department, Course, Semester, HOD, RevokedToken, Lecturer
from logger import request_logger, stream_logger
if os.environ.get('FLASK_ENV') is None:
print('FLASK_ENV not provided')
sys.exit()
app = create_app(os.environ.get('FLASK_ENV'))
# app.app_context().push()
migrate = Migrate(app, db)
@app.shell_context_processor
def make_shell_context():
return {
'app': app,
'db': db,
'Student': Student,
'Level': Level,
'School': School,
'Department': Department,
'Course': Course,
'Semester': Semester,
'HOD': HOD,
'RevokedToken': RevokedToken,
'Lecturer': Lecturer
}
@app.cli.command()
def run():
app.run(host='0.0.0.0')
@app.cli.command()
def test():
tests = unittest.TestLoader().discover('test', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=2).run(tests)
return result.wasSuccessful()
@app.after_request
def log_info(response):
date = arrow.now('Africa/Lagos')
if os.environ.get('FLASK_ENV') is 'prod':
ip = request.headers.get(app.config['REAL_IP'])
else:
ip = request.remote_addr
log_details = {
'date': date.format(),
'ip': ip,
'browser': request.user_agent.browser,
'device': request.user_agent.platform,
'status_code': response.status_code,
'path': request.path,
'method': request.method,
}
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, x-auth-token')
stream_logger.debug(log_details)
if not app.debug:
request_logger.info(log_details)
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', port='8080')
from utils import cli_utils