Archived
1

Added cli args

This commit is contained in:
Jake Howard 2017-04-11 21:27:19 +01:00
parent c0309e47b5
commit a04b8aab4c
3 changed files with 26 additions and 8 deletions

9
lantern/args.py Normal file
View File

@ -0,0 +1,9 @@
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("handler", help="Executable to run", type=str)
parser.add_argument("--port", help="Port to listen on", type=int, default=8000)
parser.add_help = True
return parser.parse_args()

View File

@ -1,6 +1,7 @@
from sanic import Sanic
from sanic_useragent import SanicUserAgent
from lantern.route import main_route
from lantern.args import parse_args
from lantern.route import build_main_route
import os
app = Sanic(__name__)
@ -8,7 +9,9 @@ SanicUserAgent.init_app(app)
def main():
args = parse_args()
workers = os.cpu_count() or 1
main_route = build_main_route(args)
app.add_route(main_route, "/<path>")
app.add_route(main_route, "/")
app.run(host="0.0.0.0", port=8000, workers=workers)
app.run(host="0.0.0.0", port=args.port, workers=workers)

View File

@ -5,10 +5,16 @@ from lantern.request import serialize_request
import os
async def main_route(request, path=None):
handle = os.path.join(os.path.dirname(__file__), 'test_handle.py')
result = execute_handle(handle, serialize_request(request))
if result.exit_code != 0:
return error_response(result.error)
def build_main_route(args):
handle = os.path.abspath(args.handler)
if not os.path.isfile(handle) or not os.access(handle, os.X_OK):
raise FileNotFoundError("Can't find handle at {}".format(handle))
return html(result.html, status=200)
async def main_route(request, path=None):
result = execute_handle(handle, serialize_request(request))
if result.exit_code != 0:
return error_response(result.error)
return html(result.html, status=200)
return main_route