Browse Source

Merge pull request #17 from pedromcunha/master

Migrate primitive config values to env variables
master
Jeffery Russell 4 years ago
committed by GitHub
parent
commit
91e4e6afde
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 39 additions and 52 deletions
  1. +5
    -0
      .env
  2. +1
    -6
      conf.json
  3. +4
    -22
      configManager.js
  4. +12
    -1
      fileIO.js
  5. +1
    -0
      package.json
  6. +3
    -3
      routes/system/index.js
  7. +2
    -4
      routes/video.js
  8. +1
    -3
      routes/watch.js
  9. +5
    -5
      server.js
  10. +5
    -8
      videoManager.js

+ 5
- 0
.env View File

@ -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

+ 1
- 6
conf.json View File

@ -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"
]
}

+ 4
- 22
configManager.js View File

@ -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;
}
};

+ 12
- 1
fileIO.js View File

@ -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()

+ 1
- 0
package.json View File

@ -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",

+ 3
- 3
routes/system/index.js View File

@ -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();

+ 2
- 4
routes/video.js View File

@ -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);

+ 1
- 3
routes/watch.js View File

@ -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

+ 5
- 5
server.js View File

@ -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}!`)
);

+ 5
- 8
videoManager.js View File

@ -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()

Loading…
Cancel
Save