{
await visibility();
const endpointKey = "ENDPOINT_EXAMPLE_3";
const {
port,
baseUrl,
close: closeChannel
} = await WebRunHttp.newRemoteRelayChannel();
invalidation.then(closeChannel);
const serviceBaseUrl = `${baseUrl}~${endpointKey}`;
let data = {
firstName: "John",
lastName: "Smith"
};
const div = htl.html`<div>`;
function updateDiv() {
div.innerText = JSON.stringify(data);
}
updateDiv();
const httpHandler = async (request) => {
const { url, method } = request;
const path = url.slice(serviceBaseUrl.length);
if (method === "POST") {
const f = await request.formData();
for (const [name, value] of f.entries()) {
data[name] = value;
}
updateDiv();
return replyWithHtmlPage(`
<h3>Success!</h3>
<p>Recieved data:</p>
<pre>${JSON.stringify(data, null, 2)}</pre>
<p>Return <a href="./index.html">back</a>.</p>
`);
} else if (method === "GET") {
return replyWithHtmlPage(`
<h3>Submit Form</h3>
<form action="./post" method="post" enctype="multipart/form-data">
<div>
<label>
First Name:
<input name="firstName" type="text" value=${JSON.stringify(
data.firstName
)}></input>
</label>
</div>
<div>
<label>
Last Name:
<input name="lastName" type="text" value=${JSON.stringify(
data.lastName
)}></input>
</label>
</div>
<input type="submit" value="Go!"></input>
</form>
<p>
Click <a href="./index.html">here to refresh this page</a>.
</p>
`);
}
};
const close = await WebRunHttp.initHttpService(httpHandler, {
key: endpointKey,
port
});
invalidation.then(close);
const endpointUrl = `${serviceBaseUrl}/index.html`;
const label = `/~${endpointKey}/index.html`;
const iframe = renderIframe(endpointUrl, label);
return htl.html`<div>${div}${iframe}`;
function replyWithHtmlPage(content) {
return new Response(
`<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
${content}
</body>
</html>
`,
{
status: 200,
headers: {
"Content-Type": "text/html"
}
}
);
}
}