-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
43 lines (36 loc) · 1.44 KB
/
server.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
import { Application, Router } from "https://deno.land/x/oak@v9.0.1/mod.ts";
import {xterm} from './static/xterm/packed.ts';
import { getHTML, getRenderOptionsFromPostData, testText } from "./snapper.ts";
export const defaultPort = 7777;
export function buildServer(): Application {
const router = new Router();
router
.get("/", (context) => {
context.response.body = getHTML({text: 'use POST method\n' + testText});
})
.post("/", async (context) => {
const body = await context.request.body({ type: 'json'});
const formData = await body.value;
const renderOptions = getRenderOptionsFromPostData(formData);
context.response.body = getHTML(renderOptions);
})
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
app.use(async (context) => {
if(context.request.url.toString().indexOf('/static') > 0) {
const split = context.request.url.toString().split('/static/xterm/');
context.response.body = (xterm as any)[split.at(-1) as any];
context.response.type = (split.at(-1) as any).split('.')[1];
}
});
return app;
}
export function startServer(opts?: {port?: number}) {
const controller = new AbortController();
const { signal } = controller;
return {
listen: buildServer().listen({port: opts?.port || defaultPort, signal}),
abort: () => controller.abort()
};
}