From 748d9de02ac37646ec5618d9d47ecd509ca7d377 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 6 Dec 2024 06:57:10 +0000 Subject: [PATCH] Added basic docker support. --- .gitignore | 3 ++- Dockerfile | 17 +++++++++++++++++ app.js | 2 +- config/configuration.js | 10 ++++++++-- services/logging.js | 6 +++++- utils/docker.js | 13 +++++++++++++ 6 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 Dockerfile create mode 100644 utils/docker.js diff --git a/.gitignore b/.gitignore index 6fcf7a9..dc90221 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ logs/ config/* !config/configuration.js !config.schema.js -credentials.* \ No newline at end of file +credentials.* +config*.yml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d9e0dbf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM node:18.19.0 + +ENV APP_DIR=/app +ENV CONFIG_DIR=/config +ENV LOGS_DIR=/logs +ENV WEB_PORT=9011 + +RUN mkdir -p ${APP_DIR}/node_modules + +WORKDIR ${APP_DIR} +COPY package*.json ./ +RUN npm install +COPY . . + +EXPOSE ${WEB_PORT} + +CMD [ "node", "app.js" ] \ No newline at end of file diff --git a/app.js b/app.js index 9f67bd4..d7aabb4 100644 --- a/app.js +++ b/app.js @@ -36,7 +36,7 @@ const limiter = rateLimit({ app.use(helmet()); app.use(limiter); -const PORT = process.env.PORT || config.web.port || 9111; +const PORT = config.web.port || 9011; app.listen(PORT, () => { logger.info("Listening to port " + PORT + "."); }); \ No newline at end of file diff --git a/config/configuration.js b/config/configuration.js index 10788b6..b10fd33 100644 --- a/config/configuration.js +++ b/config/configuration.js @@ -1,7 +1,9 @@ const Ajv = require("ajv"); const fs = require("fs"); +const docker = require("../utils/docker"); const logger = require("../services/logging"); const yaml = require("js-yaml"); +const { exit } = require("process"); const configurationBase = { plex: { @@ -30,11 +32,12 @@ const configurationBase = { } }; -const configurationFile = yaml.load(fs.readFileSync('config/config.yml'), yaml.JSON_SCHEMA); +const isDocker = docker.isRunningOnDocker(); +const configPath = isDocker ? `${process.env.CONFIG_DIR}/config.yml` : "config.yml"; +const configurationFile = yaml.load(fs.readFileSync(configPath), yaml.JSON_SCHEMA); const ajv = new Ajv({ allErrors: true }); const schema = require("./config.schema"); -const { exit } = require("process"); const validation = ajv.compile(schema); const valid = validation(configurationFile); @@ -44,4 +47,7 @@ if (!valid) { } const configuration = { ...configurationBase, ...configurationFile } + +configuration.web.port ||= process.env.WEB_PORT; + module.exports = configuration; \ No newline at end of file diff --git a/services/logging.js b/services/logging.js index 7ede090..340525c 100644 --- a/services/logging.js +++ b/services/logging.js @@ -1,5 +1,9 @@ +const docker = require("../utils/docker"); +const path = require("path"); const pino = require("pino"); -const logger = pino(pino.destination({ dest: 'logs/.log', sync: false })); + +const directory = docker.isRunningOnDocker() ? process.env.LOGS_DIR : "logs"; +const logger = pino(pino.destination({ dest: path.join(directory, '.log'), sync: false })); const environment = process.env.NODE_ENV || 'development'; if (environment == "production") { diff --git a/utils/docker.js b/utils/docker.js new file mode 100644 index 0000000..4c7f2c8 --- /dev/null +++ b/utils/docker.js @@ -0,0 +1,13 @@ +const fs = require("fs"); + +function isRunningOnDocker() { + try { + return fs.existsSync("/.dockerenv"); + } catch { } + return false; +} + + +module.exports = { + isRunningOnDocker +} \ No newline at end of file