From f360aad9aec639fd4c9d8cc7d1f8e4aeb7606236 Mon Sep 17 00:00:00 2001 From: Pedro Lucas Da Silva Cunha Date: Sun, 4 Oct 2020 10:40:21 -0400 Subject: [PATCH] Migrate primitive config values to env variables --- .env | 5 +++++ conf.json | 7 +------ configManager.js | 26 ++++---------------------- fileIO.js | 13 ++++++++++++- package.json | 1 + routes/system/index.js | 6 +++--- routes/video.js | 6 ++---- routes/watch.js | 4 +--- server.js | 10 +++++----- videoManager.js | 13 +++++-------- 10 files changed, 39 insertions(+), 52 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..458ba1e --- /dev/null +++ b/.env @@ -0,0 +1,5 @@ +PORT=5000 +SESSION_SECRET=super_duper_secret_secret +SERVER_URL=http://localhost:5000 +PRIVATE_DIR=/home/jeff/private/videos +PUBLIC_DIR=/home/jeff/public/videos \ No newline at end of file diff --git a/conf.json b/conf.json index 66e3d2a..867e35d 100644 --- a/conf.json +++ b/conf.json @@ -1,6 +1,4 @@ { - "port": 5000, - "sessionSecret": "super_duper_secret_secret", "users": [ { "username": "admin", @@ -10,8 +8,5 @@ "admin": true, "api": "e8d4fd1267e14660bde678be259b86af413be4aca234bafa773a7c9fd535e9e2" } - ], - "serverURL": "http://localhost:5000", - "privateDir": "/home/jeff/private/videos", - "publicDir": "/home/jeff/public/videos" + ] } \ No newline at end of file diff --git a/configManager.js b/configManager.js index c0c0597..4b21944 100644 --- a/configManager.js +++ b/configManager.js @@ -15,35 +15,17 @@ module.exports= fileIO.writeJSONToFile(CONFIG_FILE_NAME, config); }, - getRootDirectory: function() - { - return config.privateDir; - }, - - getPublicDirectory: function() - { - return config.publicDir; - }, - - getServerURL: function() - { - return config.serverURL; - }, - updateSystem: function(host, publicDir, privateDir) { - config.serverURL = host; - config.privateDir = privateDir; - config.publicDir = publicDir; + process.env.SERVER_URL = host; + process.env.PRIVATE_DIR = privateDir; + process.env.PUBLIC_DIR = publicDir; - module.exports.syncToDisk(); + fileIO.syncEnv(); }, getUserCount: function() { return (config.hasOwnProperty('users')) ? config.users.length : 0; } - - - }; \ No newline at end of file diff --git a/fileIO.js b/fileIO.js index 85a9668..15aa90f 100644 --- a/fileIO.js +++ b/fileIO.js @@ -4,7 +4,18 @@ const fs = require('fs'); module.exports = { - writeJSONToFile: function(fileName, jsonObject) + syncEnv: function() + { + const envVars = ["PORT", "SESSION_SECRET", "SERVER_URL", "PRIVATE_DIR", "PUBLIC_DIR"]; + const data = envVars.map(function(envVar) { + return `${envVar}=${process.env[envVar]}` + }).join("\r\n"); + fs.writeFile('.env', data, 'utf8', function() { + console.log("Wrote to .env file"); + }); + }, + + writEnvToFile: function(fileName, jsonObject) { const json = JSON.stringify(jsonObject, null, 4); fs.writeFile(fileName, json, 'utf8', function() diff --git a/package.json b/package.json index 7580710..5c697d8 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "homepage": "https://github.com/jrtechs/HomeBrewPlex#readme", "dependencies": { "crypto": "^1.0.1", + "dotenv": "^8.2.0", "download-file": "^0.1.5", "express": "^4.16.4", "express-session": "^1.15.6", diff --git a/routes/system/index.js b/routes/system/index.js index f691faf..02b7c57 100644 --- a/routes/system/index.js +++ b/routes/system/index.js @@ -14,9 +14,9 @@ const videoManager = require("../../videoManager"); function getSystemInformation(templateContext, request) { - templateContext.serverURL = configLoader.getServerURL(); - templateContext.privateDir = configLoader.getRootDirectory(); - templateContext.publicDir = configLoader.getPublicDirectory(); + templateContext.serverURL = process.env.SERVER_URL; + templateContext.privateDir = process.env.PRIVATE_DIR; + templateContext.publicDir = process.env.PUBLIC_DIR; templateContext.publicVideoCount = videoManager.getPublicVideoCount(); templateContext.privateVideoCount = videoManager.getPrivateVideoCount(); templateContext.userCount = configLoader.getUserCount(); diff --git a/routes/video.js b/routes/video.js index 302d769..0fa7508 100644 --- a/routes/video.js +++ b/routes/video.js @@ -4,8 +4,6 @@ const utils = require("../utils"); const userUtils = require("../user"); -const configManager = require("../configManager"); - const fs = require('fs'); const videoManager = require("../videoManager"); @@ -19,8 +17,8 @@ routes.get('/', (request, result) => videoManager.isPublicVideo(videoID)) { const rootDir= (videoManager.isPublicVideo(videoID)) ? - configManager.getPublicDirectory() : - configManager.getRootDirectory(); + process.env.PUBLIC_DIR : + process.env.PRIVATE_DIR; const path = rootDir + videoID; const stat = fs.statSync(path); diff --git a/routes/watch.js b/routes/watch.js index 626adb8..8ced57a 100644 --- a/routes/watch.js +++ b/routes/watch.js @@ -2,14 +2,12 @@ const routes = require('express').Router(); const utils = require("../utils"); -const configManager = require("../configManager"); - const videoManager = require("../videoManager"); function getVideoTemplateInfo(templateContext, request) { templateContext.api = request.session.API; - templateContext.serverURL = configManager.getServerURL(); + templateContext.serverURL = process.env.SERVER_URL; templateContext.videoURL = request.query.v.split(" ").join("%20"); if(utils.checkPrivilege(request) === utils.PRIVILEGE.NOBODY diff --git a/server.js b/server.js index 58e1792..0b6e9cd 100644 --- a/server.js +++ b/server.js @@ -1,16 +1,16 @@ /** express app for routing */ const express = require("express"); +const dotenv = require("dotenv"); +dotenv.config(); /**session data for login and storing preferences*/ const session = require('express-session'); -const configLoader = require('./configManager.js'); - const app = express(); /**Initializes sessions for login */ app.use(session( - { secret: configLoader.getConfiguration().sessionSecret, + { secret: process.env.SESSION_SECRET, cookie: { maxAge: 6000000 }} )); @@ -26,6 +26,6 @@ app.use('/', routes); -app.listen(configLoader.getConfiguration().port, () => - console.log(`App listening on port ${configLoader.getConfiguration().port}!`) +app.listen(process.env.PORT, () => + console.log(`App listening on port ${process.env.PORT}!`) ); \ No newline at end of file diff --git a/videoManager.js b/videoManager.js index 6042c8d..1ed545a 100644 --- a/videoManager.js +++ b/videoManager.js @@ -1,5 +1,3 @@ -const configManager = require('./configManager'); - const recursive = require('./recursiveTraversal'); const generatePreview = require('ffmpeg-generate-video-preview') @@ -91,12 +89,12 @@ module.exports = if(templateKey === "public") { videos = publicVideos; - rootDir = configManager.getPublicDirectory(); + rootDir = process.env.PUBLIC_DIR } else { videos = privateVideos; - rootDir = configManager.getRootDirectory(); + rootDir = process.env.PRIVATE_DIR } if(videos === null) @@ -126,8 +124,7 @@ module.exports = if(publicVideos == null) { publicVideos = []; - rootDir = configManager.getPublicDirectory(); - module.exports.indexVideos(rootDir, publicVideos, "public").then(function() + module.exports.indexVideos(process.env.PUBLIC_DIR, publicVideos, "public").then(function() { for(var i = 0; i < publicVideos.length; i++) { @@ -163,8 +160,8 @@ module.exports = { publicVideos = []; privateVideos = []; - module.exports.indexVideos(configManager.getPublicDirectory(), publicVideos, "public"); - module.exports.indexVideos(configManager.getRootDirectory(), privateVideos, "private"); + module.exports.indexVideos(process.env.PUBLIC_DIR, publicVideos, "public"); + module.exports.indexVideos(process.env.PRIVATE_DIR, privateVideos, "private"); }, getPublicVideoCount: function()