91 lines
2.1 KiB
TypeScript
91 lines
2.1 KiB
TypeScript
/// <reference lib="dom" />
|
|
|
|
import { sleep } from "https://deno.land/x/sleep/mod.ts";
|
|
|
|
import { HttpCode, httpCodes } from "./http-codes.ts";
|
|
import { ContentType, contentTypes } from "./content-types.ts";
|
|
import { services } from "./services.ts";
|
|
import {
|
|
DenoRequest,
|
|
Handler,
|
|
Method,
|
|
ProcessedRoute,
|
|
Request,
|
|
Response,
|
|
Route,
|
|
UserRequest,
|
|
} from "./types.ts";
|
|
|
|
// FIXME: Obviously put this somewhere else
|
|
const okText = (out: string) => {
|
|
const code = httpCodes.success.OK;
|
|
|
|
return {
|
|
code,
|
|
result: out,
|
|
contentType: contentTypes.text.plain,
|
|
};
|
|
};
|
|
|
|
const routes: Route[] = [
|
|
{
|
|
path: "/slow",
|
|
methods: ["GET"],
|
|
handler: async (_req: Request) => {
|
|
console.log("starting slow request");
|
|
await sleep(2);
|
|
console.log("finishing slow request");
|
|
return okText("that was slow");
|
|
},
|
|
},
|
|
{
|
|
path: "/list",
|
|
methods: ["GET"],
|
|
handler: (_req: Request) => {
|
|
const code = httpCodes.success.OK;
|
|
const lr = (rr: Route[]) => {
|
|
const ret = rr.map((r: Route) => {
|
|
return r.path;
|
|
});
|
|
|
|
return ret;
|
|
};
|
|
|
|
const listing = lr(routes).join(", ");
|
|
return {
|
|
code,
|
|
result: listing + "\n",
|
|
contentType: contentTypes.text.plain,
|
|
};
|
|
},
|
|
},
|
|
{
|
|
path: "/ok",
|
|
methods: ["GET"],
|
|
handler: (_req: Request) => {
|
|
const code = httpCodes.success.OK;
|
|
const rn = services.random.randomNumber();
|
|
|
|
return {
|
|
code,
|
|
result: "it is ok " + rn,
|
|
contentType: contentTypes.text.plain,
|
|
};
|
|
},
|
|
},
|
|
{
|
|
path: "/alsook",
|
|
methods: ["GET"],
|
|
handler: (_req) => {
|
|
const code = httpCodes.success.OK;
|
|
return {
|
|
code,
|
|
result: "it is also ok",
|
|
contentType: contentTypes.text.plain,
|
|
};
|
|
},
|
|
},
|
|
];
|
|
|
|
export { routes };
|