diff options
author | metamuffin <metamuffin@yandex.com> | 2022-01-23 14:06:14 +0100 |
---|---|---|
committer | metamuffin <metamuffin@yandex.com> | 2022-01-23 14:06:14 +0100 |
commit | a383c334a2e4ccb246a4a1092b1d053ccad19bd7 (patch) | |
tree | c747e39e25b94944da077df0a3ea20a2465540a8 /source/server/index.ts | |
parent | 16fa95ac191287fe7f82b9f37c31342fe3b4b65a (diff) | |
download | keks-meet-a383c334a2e4ccb246a4a1092b1d053ccad19bd7.tar keks-meet-a383c334a2e4ccb246a4a1092b1d053ccad19bd7.tar.bz2 keks-meet-a383c334a2e4ccb246a4a1092b1d053ccad19bd7.tar.zst |
ported everything to deno! yay
Diffstat (limited to 'source/server/index.ts')
-rw-r--r-- | source/server/index.ts | 142 |
1 files changed, 55 insertions, 87 deletions
diff --git a/source/server/index.ts b/source/server/index.ts index 88c3a66..1ee6bc9 100644 --- a/source/server/index.ts +++ b/source/server/index.ts @@ -1,103 +1,71 @@ -import Express, { static as estatic, json } from "express"; -import { join } from "path"; -import Webpack from "webpack" -import WebpackDevMiddleware from "webpack-dev-middleware" -import { existsSync, readFile, readFileSync } from "fs"; -import http from "http" -import https from "https" -import expressWs from "express-ws"; -import { CSPacket, SCPacket } from "../client/types"; -import * as ws from "ws" +import { Application, Router, RouterContext, send } from "https://deno.land/x/oak/mod.ts"; +import { api } from "./room.ts"; -type Room = Map<string, ws> -const rooms: Map<string, Room> = new Map() +const app = new Application() -function ws_send(ws: ws, data: SCPacket) { - try { ws.send(JSON.stringify(data)) } - catch (e) { console.warn("i hate express-ws") } -} +const root = new Router() + +let bundleFiles: Record<string, string> = {} -async function main() { - const app_e = Express(); - const app = expressWs(app_e).app +root.get("/", async c => { await c.send({ path: "index.html", root: `${Deno.cwd()}/public` }) }) +root.get("/room/:id", async c => { await c.send({ path: "index.html", root: `${Deno.cwd()}/public` }) }) - if (process.env.ENV == "production") { - console.log("PRODUCTION MODE!!!"); - app.use("/scripts", estatic(join(__dirname, "../../public/dist"))) - } else { - console.log("DEVELOPMENT MODE!!!"); - const webpackConfig = require('../../webpack.dev'); - const compiler = Webpack(webpackConfig) - const devMiddleware = WebpackDevMiddleware(compiler, { - publicPath: webpackConfig.output.publicPath - }) - app.use("/scripts", devMiddleware) +// deno-lint-ignore no-explicit-any +function respondWithType(mimeType: string, f: () => string): (c: RouterContext<any, any, any>) => void { + return c => { + c.response.headers.set("Content-Type", mimeType) + c.response.body = f() } +} - app.disable("x-powered-by"); - app.use(json()); +root.get("/bundle.js", respondWithType("application/javascript", () => bundleFiles["deno:///bundle.js"])) +root.get("/bundle.js.map", respondWithType("application/javascript", () => bundleFiles["deno:///bundle.js.map"])) - app.get("/", (req, res) => { - res.sendFile(join(__dirname, "../../public/index.html")); - }); - app.get("/room/:id", (req, res) => { - res.sendFile(join(__dirname, "../../public/index.html")); - }); +function mountFilesystem(r: Router, route: string, path: string) { + r.get(route + "/(.*)", async (context) => { + await send(context, context.request.url.pathname, { root: Deno.cwd() + path }); + }) - app.use("/static", estatic(join(__dirname, "../../public"))); +} - app.ws("/signaling/:id", (ws, req) => { - const room_name = req.params.id - const room: Map<string, ws> = rooms.get(req.params.id) ?? new Map() - let initialized = false - let user_name = "" +mountFilesystem(root, "/style", "/public") +mountFilesystem(root, "/rnnoise", "/public") - const init = (n: string) => { - if (room.get(n)) return ws.close() - initialized = true - user_name = n - rooms.set(req.params.id, room) - room.forEach(uws => ws_send(uws, { sender: user_name, join: true })) - room.forEach((_, uname) => ws_send(ws, { sender: uname, join: true, stable: true })) - room.set(user_name, ws) - console.log(`[${room_name}] ${user_name} joined`) - } - ws.onclose = () => { - room.delete(user_name) - room.forEach(uws => ws_send(uws, { sender: user_name, leave: true })) - if (room.size == 0) rooms.delete(room_name) - console.log(`[${room_name}] ${user_name} left`) - } - ws.onmessage = ev => { - const message = ev.data.toString() - if (!initialized) return init(message) - let in_packet: CSPacket; - try { in_packet = JSON.parse(message) } - catch (e) { return } +root.use(api.routes()) - console.log(`[${room_name}] ${user_name} -> ${in_packet.receiver ?? "*"}: ${message.substr(0, 100)}`) - const out_packet: SCPacket = { sender: user_name, data: in_packet } +app.use(root.routes()) +app.use(root.allowedMethods()) - if (in_packet.receiver) { - const rws = room.get(in_packet.receiver) - if (rws) ws_send(rws, out_packet) - } else { - room.forEach((uws, uname) => { - if (uname != user_name) ws_send(uws, out_packet) - }) - } - } - }) +app.addEventListener("listen", ({ hostname, port, secure }) => { + console.log(`listening on: ${secure ? "https://" : "http://"}${hostname}:${port}`); +}); - app.use((req, res, next) => { - res.status(404); - res.send("This is an error page"); - }); +app.listen({ + hostname: Deno.env.get("HOSTNAME") ?? "127.0.0.1", + port: parseInt(Deno.env.get("PORT") ?? "8080") +}); - const port = parseInt(process.env.PORT ?? "8080") - app.listen(port, process.env.HOST ?? "127.0.0.1", () => { - console.log(`Server listening on ${process.env.HOST ?? "127.0.0.1"}:${port}`); - }) + +let refresh_needed = false +let refresh_pending = false +async function refresh() { + refresh_needed = true + if (refresh_pending) return + refresh_needed = false + refresh_pending = true + + try { + const { files } = await Deno.emit("source/client/index.ts", { bundle: "module", check: false }) + bundleFiles = files + } catch (e) { console.error(e) } + + refresh_pending = false + if (refresh_needed) refresh() } -main();
\ No newline at end of file +refresh() +for await (const event of Deno.watchFs("source/client")) { + if (event.kind == "modify" || event.kind == "create") { + refresh() + } +} |