-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
52 lines (43 loc) · 1.11 KB
/
main.ts
File metadata and controls
52 lines (43 loc) · 1.11 KB
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 { Application, Router } from "@oak/oak";
import {
getVolume,
monitorOff,
monitorOn,
setVolume,
} from "./util/library-wrapper.ts";
const router = new Router();
router.post("/monitor-off", () => {
monitorOff();
});
router.post("/monitor-on", () => {
monitorOn();
});
router.post("/volume", (ctx) => {
const volume = ctx.request.url.searchParams.get("volume") ?? "0.0";
const volumeNumber = Number.parseFloat(volume);
setVolume(volumeNumber / 100);
});
router.get("/volume", (ctx) => {
ctx.response.body = getVolume() * 100;
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
app.use(async (ctx) => {
try {
await ctx.send({
root: `${Deno.cwd()}/assets`,
index: "index.html",
});
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
ctx.throw(404);
console.warn("Not found file was requested");
}
}
});
app.addEventListener("listen", ({ port, secure }) => {
const protocol = secure ? "https://" : "http://";
console.log(`The server started: ${protocol}localhost:${port}`);
});
app.listen({ port: 8080 });