Adapt docker runtime

For now app DOES NOT run outside docker, unless specifically located at
/usr/src/app, idk how to fix this
This commit is contained in:
Aelita4 2022-08-19 10:30:09 +02:00
parent e2e5c04cf1
commit f795c986a8
Signed by: Aelita4
GPG Key ID: F8EC95519509D1D5
6 changed files with 61 additions and 14 deletions

View File

@ -8,8 +8,10 @@ RUN npm ci
COPY data.json .
COPY out .
COPY views views
COPY out out
EXPOSE 8080
CMD ["node", "index.js"]
CMD ["node", "out/index.js"]

45
docker-compose.yml Normal file
View File

@ -0,0 +1,45 @@
version: '3'
services:
app:
container_name: sshmon_main
restart: always
image: sshmon
ports:
- "8080:8080"
depends_on:
- mysql
networks:
- network
mysql:
container_name: sshmon_mysql
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: 'root'
ports:
- "3306:3306"
volumes:
- volume:/var/lib/mysql
networks:
- network
healthcheck:
test: "/usr/bin/mysql --user=root --password=root --execute \"SHOW DATABASES;\""
interval: 2s
timeout: 20s
retries: 10
pma:
image: phpmyadmin/phpmyadmin
container_name: sshmon_pma
restart: always
environment:
- PMA_ARBITRARY=1
ports:
- "8081:80"
depends_on:
- mysql
networks:
- network
volumes:
volume: {}
networks:
network:

View File

@ -15,7 +15,7 @@ const ping = async (host: string) => {
}
const connection = mysql.createConnection({
host: "localhost",
host: "mysql",
user: "root",
password: "root",
database: "sshmon"
@ -38,14 +38,14 @@ app.use(express.urlencoded());
const pings = new Map();
if(!existsSync("../data.json")) writeFileSync("../data.json", "[]");
let addresses = JSON.parse(readFileSync("../data.json", {encoding:'utf8', flag:'r'}))
if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]");
let addresses = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}))
addresses.forEach((a: string) => pings.set(a, -1));
const timeoutDelay = 60000;
const routes = readdirSync("./routes");
const routes = readdirSync(__dirname + "/routes");
routes.forEach(route => {
const file = require(`./routes/${route}`);
const file = require(`${__dirname}/routes/${route}`);
console.log(`[${file.method}] ${file.url}`);
switch(file.method) {
case "GET": app.get(file.url, file.callback.bind(null, { addresses, ping, pings, timeoutDelay })); break;

View File

@ -5,13 +5,13 @@ module.exports = {
method: "GET",
url: "/addIP/:ip",
callback: async (data: any, req: Request, res: Response) => {
if(!existsSync("../data.json")) writeFileSync("../data.json", "[]");
const json = JSON.parse(readFileSync("../data.json", {encoding:'utf8', flag:'r'}));
if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]");
const json = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}));
json.push(req.params.ip);
writeFileSync("../data.json", JSON.stringify(json))
writeFileSync("/usr/src/app/data.json", JSON.stringify(json))
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ status: "OK" }));

View File

@ -5,12 +5,12 @@ module.exports = {
method: "GET",
url: "/removeIP/:ip",
callback: async (data: any, req: Request, res: Response) => {
if(!existsSync("../data.json")) writeFileSync("../data.json", "[]");
const json = JSON.parse(readFileSync("../data.json", {encoding:'utf8', flag:'r'}));
if(!existsSync("/usr/src/app/data.json")) writeFileSync("/usr/src/app/data.json", "[]");
const json = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}));
json.splice(json.indexOf(req.params.ip.toString()), 1)
writeFileSync("../data.json", JSON.stringify(json))
writeFileSync("/usr/src/app/data.json", JSON.stringify(json))
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ status: "OK" }));

View File

@ -5,7 +5,7 @@ module.exports = {
method: "GET",
url: "/",
callback: async (data: any, req: Request, res: Response) => {
data.addresses = JSON.parse(readFileSync("../data.json", {encoding:'utf8', flag:'r'}));
data.addresses = JSON.parse(readFileSync("/usr/src/app/data.json", {encoding:'utf8', flag:'r'}));
//@ts-ignore
if(!req.session.user) res.redirect("/login");
else res.render('pages/index.ejs', {