Add a first cut at an express-based backend
This commit is contained in:
77
express/routes.ts
Normal file
77
express/routes.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/// <reference lib="dom" />
|
||||
|
||||
import { contentTypes } from "./content-types";
|
||||
import { multiHandler } from "./handlers";
|
||||
import { HttpCode, httpCodes } from "./http-codes";
|
||||
import { services } from "./services";
|
||||
import { Call, ProcessedRoute, Result, Route } from "./types";
|
||||
|
||||
// FIXME: Obviously put this somewhere else
|
||||
const okText = (result: string): Result => {
|
||||
const code = httpCodes.success.OK;
|
||||
|
||||
const retval: Result = {
|
||||
code,
|
||||
result,
|
||||
contentType: contentTypes.text.plain,
|
||||
};
|
||||
|
||||
return retval;
|
||||
};
|
||||
|
||||
const routes: Route[] = [
|
||||
{
|
||||
path: "/slow",
|
||||
methods: ["GET"],
|
||||
handler: async (_call: Call): Promise<Result> => {
|
||||
console.log("starting slow request");
|
||||
|
||||
await services.misc.sleep(2);
|
||||
|
||||
console.log("finishing slow request");
|
||||
const retval = okText("that was slow");
|
||||
|
||||
return retval;
|
||||
},
|
||||
},
|
||||
{
|
||||
path: "/list",
|
||||
methods: ["GET"],
|
||||
handler: async (call: Call): Promise<Result> => {
|
||||
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", "POST", "PUT"],
|
||||
handler: multiHandler,
|
||||
},
|
||||
{
|
||||
path: "/alsook",
|
||||
methods: ["GET"],
|
||||
handler: async (_req): Promise<Result> => {
|
||||
const code = httpCodes.success.OK;
|
||||
return {
|
||||
code,
|
||||
result: "it is also ok",
|
||||
contentType: contentTypes.text.plain,
|
||||
};
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export { routes };
|
||||
Reference in New Issue
Block a user