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
|
||||||
|
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
async def main_route(request, path=None):
|
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))
|
result = execute_handle(handle, serialize_request(request))
|
||||||
if result.exit_code != 0:
|
if result.exit_code != 0:
|
||||||
return error_response(result.error)
|
return error_response(result.error)
|
||||||
|
|
||||||
return html(result.html, status=200)
|
return html(result.html, status=200)
|
||||||
|
|
||||||
|
return main_route
|
||||||
|
|
Reference in a new issue