2018-11-28 07:42:22 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import http.server
|
|
|
|
import socketserver
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
TEST_PATH = pathlib.Path(__file__).parents[0].resolve()
|
|
|
|
BUILD_PATH = TEST_PATH / '..' / 'build'
|
|
|
|
|
2018-12-07 17:21:56 +00:00
|
|
|
|
2018-11-28 07:42:22 +00:00
|
|
|
class Handler(http.server.SimpleHTTPRequestHandler):
|
|
|
|
def end_headers(self):
|
|
|
|
self.send_header('Access-Control-Allow-Origin', '*')
|
|
|
|
super().end_headers()
|
|
|
|
|
2018-12-07 17:21:56 +00:00
|
|
|
|
2018-11-28 07:42:22 +00:00
|
|
|
Handler.extensions_map['.wasm'] = 'application/wasm'
|
|
|
|
|
2018-12-07 17:21:56 +00:00
|
|
|
|
2018-11-28 07:42:22 +00:00
|
|
|
def make_parser(parser):
|
2018-12-07 17:21:56 +00:00
|
|
|
parser.description = ('Start a server with the supplied '
|
|
|
|
'build_dir and port.')
|
|
|
|
parser.add_argument('--build_dir', action='store', type=str,
|
|
|
|
default=BUILD_PATH, help='set the build directory')
|
|
|
|
parser.add_argument('--port', action='store', type=int,
|
|
|
|
default=8000, help='set the PORT number')
|
2018-11-28 07:42:22 +00:00
|
|
|
return parser
|
|
|
|
|
2018-12-07 17:21:56 +00:00
|
|
|
|
2018-11-28 07:42:22 +00:00
|
|
|
def server(port):
|
|
|
|
httpd = socketserver.TCPServer(('', port), Handler)
|
|
|
|
return httpd
|
|
|
|
|
2018-12-07 17:21:56 +00:00
|
|
|
|
2018-11-28 07:42:22 +00:00
|
|
|
def main(args):
|
|
|
|
build_dir = args.build_dir
|
|
|
|
port = args.port
|
|
|
|
httpd = server(port)
|
|
|
|
try:
|
|
|
|
os.chdir(build_dir)
|
|
|
|
print("serving from {0} at localhost:".format(build_dir) + str(port))
|
|
|
|
httpd.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("\n...shutting down http server")
|
|
|
|
httpd.shutdown()
|
|
|
|
sys.exit()
|
|
|
|
|
2018-12-07 17:21:56 +00:00
|
|
|
|
2018-11-28 07:42:22 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = make_parser(argparse.ArgumentParser())
|
|
|
|
args = parser.parse_args()
|
2018-12-07 17:21:56 +00:00
|
|
|
main(args)
|