-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
33 lines (28 loc) · 911 Bytes
/
index.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
export * from "./lib/job";
export * from "./lib/api";
export * from "./lib/task";
import * as express from "express";
import { createTaskHandler, TaskFunction } from "./lib/task";
const tasks: { [key: string]: TaskFunction } = {};
let VERSION : string = "VERSION_NOT_SET";
export function addTask(t: TaskFunction, identifier: string) {
tasks[identifier] = t;
}
export function setVersion(version: string) {
VERSION = version;
}
export function handleRequest(req: express.Request, resp: express.Response) {
const components = req.path.split("/");
const taskId = components[components.length - 1];
if (taskId === '_version') {
resp.send(`VERSION: ${VERSION}`)
return;
}
const taskFunction = tasks[taskId];
if (!taskFunction) {
resp.status(404).send(`Task not found: ${req.path}`);
return;
}
const handler = createTaskHandler(taskFunction, taskId);
handler(req, resp);
}