From 134eb654d2ba78d729fbe667c51358b6b9cba449 Mon Sep 17 00:00:00 2001 From: samhza Date: Fri, 17 Dec 2021 23:44:53 -0500 Subject: [PATCH] allow custom port for API server (#204) * allow custom port for API server * add .editorconfig --- .editorconfig | 3 +++ api/IMPLEMENTATION.md | 4 ++-- api/index.js | 6 +++--- package-lock.json | 1 + utils/imageConnection.js | 22 ++++++++++++---------- utils/misc.js | 4 ++-- 6 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..0f3bb618 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,3 @@ +[*.js] +indent_style = space +indent_size = 2 diff --git a/api/IMPLEMENTATION.md b/api/IMPLEMENTATION.md index 4664b834..da269e69 100644 --- a/api/IMPLEMENTATION.md +++ b/api/IMPLEMENTATION.md @@ -1,5 +1,5 @@ # esmBot Image API -The esmBot image API is a combined HTTP and WebSocket API running on port 3762. The API supports very basic authentication, which is defined on the server via the PASS environment variable and is sent from the client via the Authentication header in both HTTP and WS requests. +The esmBot image API is a combined HTTP and WebSocket API. 3762 is the default port to access the API. The API supports very basic authentication, which is defined on the server via the PASS environment variable and is sent from the client via the Authentication header in both HTTP and WS requests. ## HTTP @@ -45,4 +45,4 @@ The job object is formatted like this: ... } } -``` \ No newline at end of file +``` diff --git a/api/index.js b/api/index.js index 64b09501..6c96e58c 100644 --- a/api/index.js +++ b/api/index.js @@ -230,8 +230,8 @@ httpServer.on("upgrade", (req, sock, head) => { httpServer.on("error", (e) => { console.error("An HTTP error occurred: ", e); }); - -httpServer.listen(3762, () => { +const port = parseInt(process.env.PORT) || 3762; +httpServer.listen(port, () => { log("HTTP and WS listening on port 3762"); }); @@ -269,4 +269,4 @@ const runJob = (job, ws) => { reject(e); }); }); -}; \ No newline at end of file +}; diff --git a/package-lock.json b/package-lock.json index 133855c3..6cdc75ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1166,6 +1166,7 @@ "node_modules/erlpack": { "version": "0.1.3", "resolved": "git+ssh://git@github.com/abalabahaha/erlpack.git#f7d730debe32c416d1b55b4217f8aef2ade05874", + "hasInstallScript": true, "license": "MIT", "optional": true, "dependencies": { diff --git a/utils/imageConnection.js b/utils/imageConnection.js index 4b71d92f..0dc9c37a 100644 --- a/utils/imageConnection.js +++ b/utils/imageConnection.js @@ -25,8 +25,10 @@ const Rinit = 0x08; class ImageConnection { constructor(host, auth, tls = false) { this.requests = new Map(); + if(!host.includes(":")){ + host += ":3762"; + } this.host = host; - this.port = 3762; this.auth = auth; this.tag = null; this.disconnected = false; @@ -39,19 +41,19 @@ class ImageConnection { } else { this.wsproto = "ws"; } - this.sockurl = `${this.wsproto}://${host}:${this.port}/sock`; - this.conn = new WebSocket(this.sockurl, { - headers: { - "Authentication": auth && auth !== "" ? auth : undefined - } - }); + this.sockurl = `${this.wsproto}://${host}/sock`; + let headers = {}; + if(auth){ + headers.Authentication = auth; + } + this.conn = new WebSocket(this.sockurl, {headers}); let httpproto; if (tls) { httpproto = "https"; } else { httpproto = "http"; } - this.httpurl = `${httpproto}://${host}:${this.port}/image`; + this.httpurl = `${httpproto}://${host}/image`; this.conn.on("message", (msg) => this.onMessage(msg)); this.conn.once("error", (err) => this.onError(err)); this.conn.once("close", () => this.onClose()); @@ -87,7 +89,7 @@ class ImageConnection { } this.requests.clear(); if (!this.disconnected) { - logger.warn(`Lost connection to ${this.host}:${this.port}, attempting to reconnect in 5 seconds...`); + logger.warn(`Lost connection to ${this.host}, attempting to reconnect in 5 seconds...`); await setTimeout(5000); this.conn = new WebSocket(this.sockurl, { headers: { @@ -164,4 +166,4 @@ class ImageConnection { } } -export default ImageConnection; \ No newline at end of file +export default ImageConnection; diff --git a/utils/misc.js b/utils/misc.js index c110d404..8644f910 100644 --- a/utils/misc.js +++ b/utils/misc.js @@ -27,8 +27,8 @@ export async function clean(text) { const imageServers = JSON.parse(fs.readFileSync("./servers.json", { encoding: "utf8" })).image; for (const { server, auth } of imageServers) { - text = text.replaceAll(server, ""); - text = text.replaceAll(auth, ""); + text = optionalReplace(server); + text = optionalReplace(auth); } for (const env of Object.keys(parsed)) { -- 2.51.2