Added cli args
This commit is contained in:
parent
c0309e47b5
commit
a04b8aab4c
3 changed files with 26 additions and 8 deletions
9
lantern/args.py
Normal file
9
lantern/args.py
Normal 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()
|
|
@ -1,6 +1,7 @@
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic_useragent import SanicUserAgent
|
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
|
import os
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
|
@ -8,7 +9,9 @@ SanicUserAgent.init_app(app)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
args = parse_args()
|
||||||
workers = os.cpu_count() or 1
|
workers = os.cpu_count() or 1
|
||||||
|
main_route = build_main_route(args)
|
||||||
app.add_route(main_route, "/<path>")
|
app.add_route(main_route, "/<path>")
|
||||||
app.add_route(main_route, "/")
|
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)
|
||||||
|
|
|
@ -5,10 +5,16 @@ from lantern.request import serialize_request
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
async def main_route(request, path=None):
|
def build_main_route(args):
|
||||||
handle = os.path.join(os.path.dirname(__file__), 'test_handle.py')
|
handle = os.path.abspath(args.handler)
|
||||||
result = execute_handle(handle, serialize_request(request))
|
if not os.path.isfile(handle) or not os.access(handle, os.X_OK):
|
||||||
if result.exit_code != 0:
|
raise FileNotFoundError("Can't find handle at {}".format(handle))
|
||||||
return error_response(result.error)
|
|
||||||
|
|
||||||
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
|
||||||
|
|
Reference in a new issue