-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.mjs
35 lines (29 loc) · 1.32 KB
/
app.mjs
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
import { handle as spinFileserverHandle } from "wasi:http/[email protected]"
import { OutgoingResponse, ResponseOutparam, OutgoingBody, Fields } from "wasi:http/[email protected]"
const encoder = new TextEncoder()
const disposeSymbol = Symbol.dispose || Symbol.for('dispose')
function handle(request, responseOut) {
const method = request.method()
const path = request.pathWithQuery()
if (method.tag === "get") {
if (path === "/hello") {
const fields = new Fields()
fields.append("content-type", encoder.encode("text/plain"))
const response = new OutgoingResponse(fields)
const responseBody = response.write()
ResponseOutparam.set(responseOut, { tag: "ok", val: response })
const responseStream = responseBody.write()
responseStream.blockingWriteAndFlush(encoder.encode("Hello, world!"))
responseStream[disposeSymbol]()
OutgoingBody.finish(responseBody)
} else {
spinFileserverHandle(request, responseOut)
}
} else {
const response = new OutgoingResponse(new Fields([]))
response.setSatusCode(400)
ResponseOutparam.set(responseOut, { tag: "ok", val: response })
OutgoingBody.finish(response.write())
}
}
export const incomingHandler = { handle }