-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
34 lines (29 loc) · 821 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
34
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const port = 3011;
const router = new Router();
router.get("/electricity", (ctx) => {
const factor = 0.759;
const kwh = Number(ctx.request.url.searchParams.get("kwh"));
if (kwh) {
const carbon = kwh * factor;
const inTonnes = carbon / 1000;
ctx.response.body = JSON.stringify({
carbon: inTonnes,
unit: "tCO2e",
});
} else {
ctx.response.status = 400;
ctx.response.body = JSON.stringify({
error: {
message: "kWh have not been supplied in the query",
},
});
}
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
app.addEventListener("listen", () =>
console.log(`Listening on http://localhost:${port}`)
);
await app.listen({ port });