diff --git a/chat-app/.env.sample b/chat-app/.env.sample new file mode 100644 index 0000000..9138653 --- /dev/null +++ b/chat-app/.env.sample @@ -0,0 +1,2 @@ +APP_URL="chat.freedoom.party" +APP_PORT=3000 diff --git a/chat-app/.gitignore b/chat-app/.gitignore index 506e4c3..7021adb 100644 --- a/chat-app/.gitignore +++ b/chat-app/.gitignore @@ -1,2 +1,3 @@ # deps node_modules/ +.env diff --git a/chat-app/Dockerfile b/chat-app/Dockerfile new file mode 100644 index 0000000..e69de29 diff --git a/chat-app/build.ts b/chat-app/build.ts index f31d630..914b7c6 100644 --- a/chat-app/build.ts +++ b/chat-app/build.ts @@ -11,7 +11,13 @@ async function fileExists(path: string): Promise { } async function main() { - const results = await Bun.build({ ...config, verbose: true }); + const results = await Bun.build({ + ...config, + verbose: true, + define: { + 'process.env.APP_URL': process.env.APP_URL, + } + }); if (!results.success) { for (const message of results.logs) { diff --git a/chat-app/frontend/websocket.js b/chat-app/frontend/websocket.js index 0c104ed..0db784c 100644 --- a/chat-app/frontend/websocket.js +++ b/chat-app/frontend/websocket.js @@ -1,8 +1,11 @@ export function createWebSocket() { - try { - return new WebSocket("ws://localhost:3000/chat-service"); - } catch (e) { - console.error(e); - throw e; + let appUrl = process.env.APP_URL; + + if (appUrl.includes('localhost')) { + appUrl = `//${appUrl}/chat-service`; + } else { + appUrl = `//${appUrl}/chat-service`; } + + return new WebSocket(appUrl); } diff --git a/chat-app/justfile b/chat-app/justfile deleted file mode 100644 index 98fdd71..0000000 --- a/chat-app/justfile +++ /dev/null @@ -1,8 +0,0 @@ -build: - bun build.ts - -clean: - rm -r static/js/* - -test: - bun test diff --git a/chat-app/src/index.ts b/chat-app/src/index.ts index 09d841d..e1183df 100644 --- a/chat-app/src/index.ts +++ b/chat-app/src/index.ts @@ -59,5 +59,6 @@ app.use("/*", serveStatic({ root: "/static" })); Bun.serve({ fetch: app.fetch, + port: parseInt(process.env.APP_PORT as string) || 3000, websocket }); diff --git a/chat-app/static/js/index.js b/chat-app/static/js/index.js index 5069bc6..ce2af26 100644 --- a/chat-app/static/js/index.js +++ b/chat-app/static/js/index.js @@ -1721,7 +1721,13 @@ var import_mithril = __toESM(require_mithril(), 1); // frontend/websocket.js function createWebSocket() { - return new WebSocket("ws://localhost:3000/chat-service"); + let appUrl = "localhost:3000"; + if (appUrl.includes("localhost")) { + appUrl = `//${appUrl}/chat-service`; + } else { + appUrl = `//${appUrl}/chat-service`; + } + return new WebSocket(appUrl); } // frontend/index.js