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 option to access-http-server.py #18

Merged
merged 3 commits into from
Oct 20, 2023
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
18 changes: 13 additions & 5 deletions scripts/access-http-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ class AccessHTTPServer(object):
STORE_HANDLER = None
STORE_TSERVER = None
STATIC_PATH = None
CORS = False

def __init__(self, host, port, static_path, fetch_handler, store_handler):
def __init__(self, host, port, static_path, fetch_handler, store_handler, cors=False):
self.host = host
self.port = port

Expand All @@ -67,26 +68,28 @@ def __init__(self, host, port, static_path, fetch_handler, store_handler):
AccessHTTPServer.STORE_TSERVER = TServer.TServer(
store_processor, None, None, None, store_pfactory, store_pfactory)

AccessHTTPServer.CORS = cors

def serve(self):
bottle.run(host=self.host, port=self.port)


@bottle.post('/fetch_http_endpoint/')
def fetch_http_endpoint():
return thrift_endpoint(AccessHTTPServer.FETCH_TSERVER)
return thrift_endpoint(AccessHTTPServer.FETCH_TSERVER, cors=AccessHTTPServer.CORS)


@bottle.post('/store_http_endpoint/')
def store_http_endpoint():
return thrift_endpoint(AccessHTTPServer.STORE_TSERVER)
return thrift_endpoint(AccessHTTPServer.STORE_TSERVER, cors=AccessHTTPServer.CORS)


@bottle.route('/<filepath:path>')
def server_static(filepath):
return bottle.static_file(filepath, root=AccessHTTPServer.STATIC_PATH)


def thrift_endpoint(tserver):
def thrift_endpoint(tserver, cors=False):
"""Thrift RPC endpoint
"""
itrans = TTransport.TFileObjectTransport(bottle.request.body)
Expand All @@ -103,6 +106,8 @@ def thrift_endpoint(tserver):
headers = dict()
headers['Content-Length'] = len(bytestring)
headers['Content-Type'] = "application/x-thrift"
if cors:
headers['Access-Control-Allow-Origin'] = "*"
return bottle.HTTPResponse(bytestring, **headers)


Expand All @@ -120,6 +125,8 @@ def main():
parser.add_argument('-l', '--loglevel', '--log-level',
help='Logging verbosity level threshold (to stderr)',
default='info')
parser.add_argument('--cors', action='store_true',
help='Enable CORS in HTTP server')
parser.add_argument('--static-path', default='.',
help='Path where HTML files are stored')
parser.add_argument('--store-path', default='.',
Expand Down Expand Up @@ -148,7 +155,8 @@ def main():
logging.info('Fetch endpoint: http://{}:{}/fetch_http_endpoint/'.format(args.host, args.port))
logging.info('Store endpoint: http://{}:{}/store_http_endpoint/'.format(args.host, args.port))

ahs = AccessHTTPServer(args.host, args.port, args.static_path, fetch_handler, store_handler)
ahs = AccessHTTPServer(args.host, args.port, args.static_path, fetch_handler, store_handler,
cors=args.cors)
ahs.serve()


Expand Down