-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
105 lines (89 loc) · 2.65 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
104
105
# Python imports
import os
import sys
# Flask
from flask import jsonify, make_response, send_from_directory
from flask_graphql import GraphQLView
from flask_cors import CORS
# Local
from modules import logger
from modules.app import create_app
from modules.app.database import db_session, init_db
from modules.app.schema import schema
app = create_app()
app.debug = True
default_query = '''
{
Users {
edges {
node {
id,
username,
email,
password
blog {
id,
title,
text
},
role {
id,
name
}
}
}
}
}
'''.strip()
"""
:ROOT_PATH : Set root path
:PUBLIC_PATH : Always for joining react public index.html to ROOT_PATH
"""
ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
os.environ.update({'ROOT_PATH': ROOT_PATH})
PUBLIC_PATH = os.path.join(ROOT_PATH, 'modules', 'client', 'public')
''' Set the view for Graphiql '''
view_func = GraphQLView.as_view(
'graphql', schema=schema, graphiql=True)
""" logger object to output info and debug information """
LOG = logger.get_root_logger(os.environ.get(
'ROOT_LOGGER', 'root'), filename=os.path.join(ROOT_PATH, 'output.log'))
PORT = os.environ.get('PORT')
app.add_url_rule('/graphql', view_func=view_func)
""" Sets routes for Flask """
@app.errorhandler(404)
def not_found(error):
""" error handler """
LOG.error(error)
return make_response(jsonify({'error': 'Not Found'}), 404)
@app.route('/')
def index():
""" serve index.html """
return send_from_directory(PUBLIC_PATH, 'index.html')
@app.route('/login')
def login():
""" serve index.html """
return send_from_directory(PUBLIC_PATH, 'index.html')
@app.route('/register')
def register():
""" serve route to register to index.html """
return send_from_directory(PUBLIC_PATH, 'index.html')
@app.route('/<path:path>')
def static_proxy(path):
"""
static folder serve
:param path: to load all static files in `public`
:return: the directory `public` with filename i.e. 404.html
"""
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.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
if __name__ == "__main__":
init_db()
CORS(app, resources={r'/graphql': {'origins': '*'}})
LOG.info('running environment: {}'.format(os.environ.get('ENV')))
app.config['DEBUG'] = os.environ.get('ENV') == 'development'
app.run(host='0.0.0.0', port=int(PORT))