allow custom port for API server (#204)

* allow custom port for API server

* add .editorconfig
This commit is contained in:
samhza 2021-12-17 23:44:53 -05:00 committed by GitHub
parent 5db8873807
commit 134eb654d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 17 deletions

3
.editorconfig Normal file
View file

@ -0,0 +1,3 @@
[*.js]
indent_style = space
indent_size = 2

View file

@ -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:
...
}
}
```
```

View file

@ -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);
});
});
};
};

1
package-lock.json generated
View file

@ -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": {

View file

@ -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;
export default ImageConnection;

View file

@ -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, "<redacted>");
text = text.replaceAll(auth, "<redacted>");
text = optionalReplace(server);
text = optionalReplace(auth);
}
for (const env of Object.keys(parsed)) {