Skip to content

Commit

Permalink
Add CORS option to access-http-server.py (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccmaymay authored Oct 20, 2023
1 parent 209c986 commit 82b50f5
Showing 1 changed file with 13 additions and 5 deletions.
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

0 comments on commit 82b50f5

Please sign in to comment.