-
Notifications
You must be signed in to change notification settings - Fork 45
/
index.py
54 lines (39 loc) · 1.49 KB
/
index.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
""" index file for REST APIs using Flask """
import os
import sys
import requests
from flask import jsonify, request, make_response, send_from_directory
ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
os.environ.update({'ROOT_PATH': ROOT_PATH})
sys.path.append(os.path.join(ROOT_PATH, 'modules'))
PUBLIC_PATH = os.path.join(ROOT_PATH, 'public')
import logger # noqa
from app import app # noqa
# Create a logger object to log the info and debug
LOG = logger.get_root_logger(os.environ.get(
'ROOT_LOGGER', 'root'), filename=os.path.join(ROOT_PATH, 'output.log'))
# Port variable to run the server on.
PORT = os.environ.get('PORT')
@app.errorhandler(404)
def not_found(error):
""" error handler """
LOG.error(error)
return make_response(jsonify({'error': 'Not found'}), 404)
@app.route('/')
def index():
""" static files serve """
return send_from_directory(PUBLIC_PATH, 'index.html')
@app.route('/<path:path>')
def static_proxy(path):
""" static folder serve """
file_name = path.split('/')[-1]
dir_name = os.path.join(PUBLIC_PATH, '/'.join(path.split('/')[:-1]))
return send_from_directory(dir_name, file_name)
@app.route('/api/v1.0/ping', methods=['GET'])
def dummy_endpoint():
""" Testing endpoint """
return jsonify({'data': 'Server running'}), 200
if __name__ == '__main__':
LOG.info('running environment: %s', os.environ.get('ENV'))
app.config['DEBUG'] = os.environ.get('ENV') == 'development'
app.run(host='0.0.0.0', port=int(PORT))