Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cors header and options to gedis http server #529

Merged
merged 3 commits into from
Jan 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion jumpscale/servers/gedis_http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
class GedisHTTPServer(Base):
host = fields.String(default="127.0.0.1")
port = fields.Integer(default=8000)
allow_cors = fields.Boolean(default=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._app = Bottle()
self._client = None
self._app.route("/<package>/<actor>/<method>", ["GET", "POST"], self.handler)
http_methods = ["GET", "POST"]
ranatrk marked this conversation as resolved.
Show resolved Hide resolved
if self.allow_cors:
http_methods.extend(["OPTIONS", "PUT", "DELETE"])
self._app.route("/<package>/<actor>/<method>", http_methods, self.enable_cors(self.handler, self.allow_cors))

@property
def client(self):
Expand All @@ -30,6 +34,24 @@ def make_response(self, code, content):
response.content_type = "application/json"
return json.dumps(content)

def enable_cors(self, fn, allow_cors=True):
def _enable_cors(*args, **kwargs):
# set CORS headers
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, OPTIONS, DELETE"
response.headers[
"Access-Control-Allow-Headers"
] = "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token"

if request.method != "OPTIONS":
# actual request; reply with the actual response
return fn(*args, **kwargs)

if allow_cors:
return _enable_cors
else:
return fn

def handler(self, package, actor, method):
actors = self.client.actors

Expand Down