Serve files
This commit is contained in:
@@ -43,14 +43,16 @@ urlpattern = Combine(OneOrMore(p_pattern | p_path))(name="urlpattern")
|
|||||||
# Body
|
# Body
|
||||||
body = (Suppress('{') + SkipTo(Combine(LineStart() + '}' + LineEnd()))(name="body"))
|
body = (Suppress('{') + SkipTo(Combine(LineStart() + '}' + LineEnd()))(name="body"))
|
||||||
|
|
||||||
# Endpoint
|
|
||||||
endpoint = (Optional(method_spec + Suppress(White()),
|
endpoint = (Optional(method_spec + Suppress(White()),
|
||||||
default='*')(name="method")
|
default='*')(name="method")
|
||||||
+ urlpattern
|
+ urlpattern
|
||||||
+ Suppress(White())
|
+ Suppress(White()))
|
||||||
+ body)(name="endpoint")
|
|
||||||
|
|
||||||
kapow_program = OneOrMore(endpoint)
|
# Endpoint
|
||||||
|
code_ep = (endpoint + body)(name="code_ep")
|
||||||
|
path_ep = (endpoint + '=' + SkipTo(LineEnd())(name="path"))(name="path_ep")
|
||||||
|
|
||||||
|
kapow_program = OneOrMore(code_ep | path_ep)
|
||||||
|
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
@@ -261,26 +263,49 @@ def generate_endpoint(code):
|
|||||||
return endpoint
|
return endpoint
|
||||||
|
|
||||||
|
|
||||||
|
def path_server(path):
|
||||||
|
# At initialization check
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
raise NotImplementedError("Cannot serve whole directories yet.")
|
||||||
|
|
||||||
|
async def serve_path(request):
|
||||||
|
# Per request check
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
raise NotImplementedError("Cannot serve whole directories yet.")
|
||||||
|
return web.FileResponse(path)
|
||||||
|
return serve_path
|
||||||
|
|
||||||
########################################################################
|
########################################################################
|
||||||
# Webserver #
|
# Webserver #
|
||||||
########################################################################
|
########################################################################
|
||||||
|
|
||||||
def register_endpoint(app, methods, pattern, code):
|
def register_code_endpoint(app, methods, pattern, code):
|
||||||
print(f"Registering methods={methods!r} pattern={pattern!r}")
|
print(f"Registering [code] methods={methods!r} pattern={pattern!r}")
|
||||||
endpoint = generate_endpoint(code)
|
endpoint = generate_endpoint(code)
|
||||||
for method in methods: # May be '*'
|
for method in methods: # May be '*'
|
||||||
app.add_routes([web.route(method, pattern, endpoint)])
|
app.add_routes([web.route(method, pattern, endpoint)])
|
||||||
|
|
||||||
|
def register_path_endpoint(app, methods, pattern, path):
|
||||||
|
print(f"Registering [path] methods={methods!r} pattern={pattern!r}")
|
||||||
|
for method in methods: # May be '*'
|
||||||
|
app.add_routes([web.route(method, pattern, path_server(path))])
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.argument('program', type=click.File())
|
@click.argument('program', type=click.File())
|
||||||
def main(program):
|
def main(program):
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
for ep, _, _ in kapow_program.scanString(program.read()):
|
for ep, _, _ in kapow_program.scanString(program.read()):
|
||||||
register_endpoint(app,
|
if ep.body:
|
||||||
ep.method.asList()[0].split('|'),
|
register_code_endpoint(app,
|
||||||
''.join(ep.urlpattern),
|
ep.method.asList()[0].split('|'),
|
||||||
ep.body)
|
''.join(ep.urlpattern),
|
||||||
|
ep.body)
|
||||||
|
else:
|
||||||
|
register_path_endpoint(app,
|
||||||
|
ep.method.asList()[0].split('|'),
|
||||||
|
''.join(ep.urlpattern),
|
||||||
|
ep.path)
|
||||||
web.run_app(app)
|
web.run_app(app)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user