diff --git a/Dockerfile b/Dockerfile index d663693..ce9732a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,8 +8,10 @@ RUN npm ci COPY data.json . -COPY out . +COPY views views + +COPY out out EXPOSE 8080 -CMD ["node", "index.js"] \ No newline at end of file +CMD ["node", "out/index.js"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a660c93 --- /dev/null +++ b/docker-compose.yml @@ -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: \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a28843e..78dfe6f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; diff --git a/src/routes/addIP.ts b/src/routes/addIP.ts index d4b3528..539571e 100644 --- a/src/routes/addIP.ts +++ b/src/routes/addIP.ts @@ -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" })); diff --git a/src/routes/removeIP.ts b/src/routes/removeIP.ts index 8bd8909..6869f41 100644 --- a/src/routes/removeIP.ts +++ b/src/routes/removeIP.ts @@ -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" })); diff --git a/src/routes/root.ts b/src/routes/root.ts index 2c555e8..5cddefe 100644 --- a/src/routes/root.ts +++ b/src/routes/root.ts @@ -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', {