-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapp.py
More file actions
42 lines (32 loc) · 1.47 KB
/
app.py
File metadata and controls
42 lines (32 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import asyncio
import poll_loop
from proxy import exports
from proxy.types import Ok
from proxy.imports import types, incoming_handler
from proxy.imports.types import Method_Get, IncomingRequest, ResponseOutparam, OutgoingResponse, Fields, OutgoingBody
from poll_loop import Stream, Sink, PollLoop
class IncomingHandler(exports.IncomingHandler):
def handle(self, request: IncomingRequest, response_out: ResponseOutparam):
# Dispatch the request using `asyncio`, backed by a custom event loop
# based on `wasi:io/poll#poll-list`.
loop = PollLoop()
asyncio.set_event_loop(loop)
loop.run_until_complete(handle_async(request, response_out))
async def handle_async(request: IncomingRequest, response_out: ResponseOutparam):
method = request.method()
path = request.path_with_query()
if isinstance(method, Method_Get) and path == "/hello":
response = OutgoingResponse(200, Fields(
[("content-type", b"text/plain")]))
response_body = response.write()
ResponseOutparam.set(response_out, Ok(response))
sink = Sink(response_body)
await sink.send(b"Hello, world!")
sink.close()
elif isinstance(method, Method_Get):
# Delegate to spin-fileserver component.
incoming_handler.handle(request, response_out)
else:
response = OutgoingResponse(400, Fields([]))
ResponseOutparam.set(response_out, Ok(response))
OutgoingBody.finish(response.write(), None)