Skip to content

Commit

Permalink
Merge pull request #529 from threefoldtech/development_cors_header
Browse files Browse the repository at this point in the history
Add cors header and options to gedis http server
  • Loading branch information
abom authored Jan 3, 2021
2 parents 7594998 + b65f05f commit 8012769
Showing 1 changed file with 23 additions and 1 deletion.
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"]
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

0 comments on commit 8012769

Please sign in to comment.