-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.ts
52 lines (45 loc) · 1.13 KB
/
http.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
45
46
47
48
49
50
51
52
import { config, data_pipe } from "./main.ts";
import {
Application,
Context,
Router,
ServerSentEvent,
Status,
} from "https://deno.land/x/oak/mod.ts";
import { prometherusRegistryHttphandler } from "./prometheus_exporter.ts";
export async function serve() {
const port = config.port;
const app = new Application();
const router = new Router();
if (config.enable_prometheus) {
router.get("/metrics", prometherusRegistryHttphandler)
}
if (config.enable_api) {
router.get("/sse", async (ctx: Context) => {
ctx.assert(
ctx.request.accepts("text/event-stream"),
Status.UnsupportedMediaType,
);
const target = ctx.sendEvents();
const httpSSEhandler = (e) => {
const evt = new ServerSentEvent(
"message",
{
data: {
pids: e.detail[0],
system: e.detail[1]
}
},
);
target.dispatchEvent(evt);
}
data_pipe.addEventListener("data", httpSSEhandler)
target.addEventListener("close", () => {
data_pipe.removeEventListener("data", httpSSEhandler)
});
});
}
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: port });
}