-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
69 lines (48 loc) · 1.61 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
#!/usr/bin/env python
import os
from dotenv import load_dotenv
from flask import Flask
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from flask_jwt_extended.exceptions import NoAuthorizationError
from flask_sockets import Sockets
from graphql import GraphQLError
from mongoengine import connect
from graph.overrides import (
OverriddenView,
CustomBackend,
GeventSubscriptionServerOverride,
)
from graph.schema import schema
from graph.auth import init_auth
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
load_dotenv(f".env.{os.environ.get('ENV', 'development')}")
app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = os.environ["SECRET_KEY"]
db = connect(host=os.environ["CONNECTION_STRING"])
jwt = JWTManager(app)
cors = CORS(app, resources={r"/graphql": {"origins": "*"}})
app.add_url_rule(
"/graphql",
view_func=OverriddenView.as_view(
"graphql", schema=schema, backend=CustomBackend(),
),
)
sockets = Sockets(app)
subscription_server = GeventSubscriptionServerOverride(schema)
app.app_protocol = lambda environ_path_info: 'graphql-ws'
@sockets.route('/subscriptions')
def echo_socket(ws):
subscription_server.handle(ws)
return []
@app.errorhandler(NoAuthorizationError)
def handle_auth_error(e):
raise GraphQLError(str(e + "123"), extensions={"code": "CUSTOM"})
init_auth(jwt)
server = pywsgi.WSGIServer(('0.0.0.0', 5000), app, handler_class=WebSocketHandler)
# For serverless deployment.
application = server.application
if __name__ == '__main__':
print("Running on localhost (127.0.0.1:5000)")
server.serve_forever()