-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some more typings and better /api/status route
- Loading branch information
Showing
9 changed files
with
146 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
- [ ] Better Offline mode using "faker" library or self written (probably self written) | ||
- [ ] HA compatibility | ||
- [ ] Add automatic notifications when container state changes, according to selected level for notification service | ||
- [ ] Image update and update notifications | ||
- [ ] trigger container restart / stop / start via backend routes | ||
- [ ] Add more logging | ||
- [ ] Strucuture code differently | ||
- [ ] Write new README and make the docs better | ||
- [ ] Update more files to correct TS syntax => remove "any" | ||
- [ ] Websockets | ||
- [X] Better /api/status endpoint with connection status of each host |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
"port": "2375" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as fs from "fs"; | ||
import * as net from "net"; | ||
import logger from "../config/loggerConfig"; | ||
|
||
const filePath: string = "./src/config/dockerConfig.json"; | ||
|
||
interface Host { | ||
name: string; | ||
url: string; | ||
port: string; | ||
} | ||
|
||
interface StatusResponse { | ||
ApiReachable: boolean; | ||
online: { [key: string]: boolean }; | ||
} | ||
|
||
async function checkHostStatus(hosts: Host[]): Promise<StatusResponse> { | ||
const results: { [key: string]: boolean } = {}; | ||
for (const host of hosts) { | ||
const { name, url, port } = host; | ||
|
||
const isOnline = await checkPort(url, parseInt(port, 10)); | ||
|
||
results[name] = isOnline ? true : false; | ||
|
||
if (results[name] == true) { | ||
logger.debug(`${host.url}:${port} is online`); | ||
} else { | ||
logger.debug(`${host.url}:${port} is unreachable`); | ||
} | ||
} | ||
|
||
return { | ||
ApiReachable: true, | ||
online: results, | ||
}; | ||
} | ||
|
||
function checkPort(host: string, port: number): Promise<boolean> { | ||
return new Promise((resolve) => { | ||
const socket = new net.Socket(); | ||
socket.setTimeout(3000); | ||
|
||
socket.on("connect", () => { | ||
socket.end(); | ||
resolve(true); | ||
}); | ||
|
||
socket.on("timeout", () => { | ||
socket.destroy(); | ||
resolve(false); | ||
}); | ||
|
||
socket.on("error", () => { | ||
socket.destroy(); | ||
resolve(false); | ||
}); | ||
|
||
socket.connect(port, host); | ||
}); | ||
} | ||
|
||
async function checkReachability(): Promise<StatusResponse | undefined> { | ||
try { | ||
const data = fs.readFileSync(filePath, "utf-8"); | ||
const parsedData = JSON.parse(data); | ||
const hosts: Host[] = parsedData.hosts; | ||
const resp = await checkHostStatus(hosts); | ||
return resp; | ||
} catch (error) { | ||
logger.error(`Error reading file: ${error}`); | ||
return undefined; | ||
} | ||
} | ||
|
||
export default checkReachability; |