Commit 0a72c4ef authored by Madhur Tandon's avatar Madhur Tandon

Expose Port 8000 for easy testing

Add ``serve`` module as a command line subcommand which enables us to start a server within the docker environment.
This is helpful in testing the ``pyodide`` builds running within the docker environment using external browser
programs of the same machine.
parent a134831a
......@@ -4,6 +4,7 @@ import argparse
from . import buildall
from . import buildpkg
from . import pywasmcross
from . import serve
def main():
......@@ -12,7 +13,8 @@ def main():
for command_name, module in (("buildpkg", buildpkg),
("buildall", buildall),
("pywasmcross", pywasmcross)):
("pywasmcross", pywasmcross),
("serve", serve)):
parser = module.make_parser(subparsers.add_parser(command_name))
parser.set_defaults(func=module.main)
......
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'
class Handler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
super().end_headers()
Handler.extensions_map['.wasm'] = 'application/wasm'
def make_parser(parser):
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')
return parser
def server(port):
httpd = socketserver.TCPServer(('', port), Handler)
return httpd
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()
if __name__ == "__main__":
parser = make_parser(argparse.ArgumentParser())
args = parser.parse_args()
main(args)
\ No newline at end of file
#!/bin/sh
docker run --rm -v $PWD:/src --user root -e NB_UID=$UID -e NB_GID=$GID -it iodide/pyodide-env:0.3.1 /bin/bash
docker run -p 8000:8000 --rm -v $PWD:/src --user root -e NB_UID=$UID -e NB_GID=$GID -it iodide/pyodide-env:0.3.1 /bin/bash
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment