-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
44 lines (36 loc) · 1.39 KB
/
main.ts
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
43
44
import { serve } from "https://deno.land/std/http/server.ts";
interface ControlMessage {
text: string;
number: number;
}
/**
* Process the control message.
* Output the accepted control message to the console.
*/
function processMessage(message: ControlMessage): void {
console.log(`Accepted Control message, Text: ${message.text}, Number: ${message.number}`);
}
/**
* Start the server and listen for incoming requests.
*/
const port = 3030;
const hostname = "localhost";
console.log(`Server is started at http://${hostname}:${port}`);
async function handleRequest(request: Request): Promise<Response> {
const params = new URLSearchParams(await request.text());
const text = params.get("text");
const number = Number(params.get("number"));
if (request.url.indexOf("/admin") > -1 && !text || isNaN(number)) {
// Invalid request: missing text or number
return new Response(`Bad Request`, { status: 400 });
}
if (request.url.indexOf("/admin") > -1 && request.method === "POST") {
// Process valid control message
const message: ControlMessage = { text, number };
processMessage(message);
return new Response(`OK, Accepted Control message, Text: ${text}, Number: ${number}`, { status: 200 });
}
// Requested GET or something
return new Response(`OK: It's a GET method or url not in"/admin"`, { status: 200 });
}
serve((_req) => handleRequest(_req), { port, hostname });