From e861452fb26553177ad4e32bfb18b4fd8a5b1816 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Aug 2016 21:34:51 +0200 Subject: [PATCH] Server: put config in constants --- server/controllers/api/v1/clients.js | 7 ++++--- server/controllers/api/v1/videos.js | 5 ++--- server/helpers/logger.js | 10 +++++----- server/helpers/peertube-crypto.js | 29 +++++++++++++++------------- server/helpers/requests.js | 11 ++--------- server/initializers/constants.js | 27 ++++++++++++++++++++++++++ server/initializers/database.js | 8 ++------ server/lib/friends.js | 8 ++------ server/lib/webtorrent.js | 9 ++++----- server/models/video.js | 23 ++++++++-------------- 10 files changed, 72 insertions(+), 65 deletions(-) diff --git a/server/controllers/api/v1/clients.js b/server/controllers/api/v1/clients.js index 0d222634b..5b460db2e 100644 --- a/server/controllers/api/v1/clients.js +++ b/server/controllers/api/v1/clients.js @@ -1,9 +1,10 @@ 'use strict' -const config = require('config') const express = require('express') const mongoose = require('mongoose') +const constants = require('../../../initializers/constants') + const Client = mongoose.model('OAuthClient') const router = express.Router() @@ -12,8 +13,8 @@ router.get('/local', getLocalClient) // Get the client credentials for the PeerTube front end function getLocalClient (req, res, next) { - const serverHost = config.get('webserver.host') - const serverPort = config.get('webserver.port') + const serverHost = constants.CONFIG.WEBSERVER.HOST + const serverPort = constants.CONFIG.WEBSERVER.PORT let headerHostShouldBe = serverHost if (serverPort !== 80 && serverPort !== 443) { headerHostShouldBe += ':' + serverPort diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js index 0a441f146..70d22f139 100644 --- a/server/controllers/api/v1/videos.js +++ b/server/controllers/api/v1/videos.js @@ -1,11 +1,11 @@ 'use strict' -const config = require('config') const express = require('express') const mongoose = require('mongoose') const multer = require('multer') const waterfall = require('async/waterfall') +const constants = require('../../../initializers/constants') const logger = require('../../../helpers/logger') const friends = require('../../../lib/friends') const middlewares = require('../../../middlewares') @@ -20,13 +20,12 @@ const sort = middlewares.sort const utils = require('../../../helpers/utils') const router = express.Router() -const uploads = config.get('storage.uploads') const Video = mongoose.model('Video') // multer configuration const storage = multer.diskStorage({ destination: function (req, file, cb) { - cb(null, uploads) + cb(null, constants.CONFIG.STORAGE.UPLOAD_DIR) }, filename: function (req, file, cb) { diff --git a/server/helpers/logger.js b/server/helpers/logger.js index 8ae90a4b2..590ceaeb6 100644 --- a/server/helpers/logger.js +++ b/server/helpers/logger.js @@ -1,23 +1,23 @@ // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ 'use strict' -const config = require('config') const mkdirp = require('mkdirp') const path = require('path') const winston = require('winston') winston.emitErrs = true -const logDir = path.join(__dirname, '..', '..', config.get('storage.logs')) -const label = config.get('webserver.host') + ':' + config.get('webserver.port') +const constants = require('../initializers/constants') + +const label = constants.CONFIG.WEBSERVER.HOST + ':' + constants.CONFIG.WEBSERVER.PORT // Create the directory if it does not exist -mkdirp.sync(logDir) +mkdirp.sync(constants.CONFIG.STORAGE.LOG_DIR) const logger = new winston.Logger({ transports: [ new winston.transports.File({ level: 'debug', - filename: path.join(logDir, 'all-logs.log'), + filename: path.join(constants.CONFIG.STORAGE.LOG_DIR, 'all-logs.log'), handleExceptions: true, json: true, maxsize: 5242880, diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 46dff8d03..ef130ea5c 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -1,15 +1,13 @@ 'use strict' -const config = require('config') const crypto = require('crypto') const fs = require('fs') const openssl = require('openssl-wrapper') -const path = require('path') const ursa = require('ursa') +const constants = require('../initializers/constants') const logger = require('./logger') -const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) const algorithm = 'aes-256-ctr' const peertubeCrypto = { @@ -17,7 +15,6 @@ const peertubeCrypto = { createCertsIfNotExist: createCertsIfNotExist, decrypt: decrypt, encrypt: encrypt, - getCertDir: getCertDir, sign: sign } @@ -40,7 +37,7 @@ function createCertsIfNotExist (callback) { } function decrypt (key, data, callback) { - fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { + fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) { if (err) return callback(err) const myPrivateKey = ursa.createPrivateKey(file) @@ -67,12 +64,8 @@ function encrypt (publicKey, data, callback) { }) } -function getCertDir () { - return certDir -} - function sign (data) { - const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) + const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')) const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') return signature @@ -85,7 +78,7 @@ module.exports = peertubeCrypto // --------------------------------------------------------------------------- function certsExist (callback) { - fs.exists(certDir + 'peertube.key.pem', function (exists) { + fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { return callback(exists) }) } @@ -99,15 +92,25 @@ function createCerts (callback) { } logger.info('Generating a RSA key...') - openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { + + let options = { + 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + '2048': false + } + openssl.exec('genrsa', options, function (err) { if (err) { logger.error('Cannot create private key on this pod.') return callback(err) } logger.info('RSA key generated.') + options = { + 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + 'pubout': true, + 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' + } logger.info('Manage public key...') - openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { + openssl.exec('rsa', options, function (err) { if (err) { logger.error('Cannot create public key on this pod.') return callback(err) diff --git a/server/helpers/requests.js b/server/helpers/requests.js index 547230adc..f76ff3473 100644 --- a/server/helpers/requests.js +++ b/server/helpers/requests.js @@ -1,16 +1,11 @@ 'use strict' -const config = require('config') const replay = require('request-replay') const request = require('request') const constants = require('../initializers/constants') const peertubeCrypto = require('./peertube-crypto') -const http = config.get('webserver.https') ? 'https' : 'http' -const host = config.get('webserver.host') -const port = config.get('webserver.port') - const requests = { makeRetryRequest: makeRetryRequest, makeSecureRequest: makeSecureRequest @@ -29,8 +24,6 @@ function makeRetryRequest (params, callback) { } function makeSecureRequest (params, callback) { - const myUrl = http + '://' + host + ':' + port - const requestParams = { url: params.toPod.url + params.path } @@ -42,8 +35,8 @@ function makeSecureRequest (params, callback) { // Add signature if it is specified in the params if (params.sign === true) { requestParams.json.signature = { - url: myUrl, - signature: peertubeCrypto.sign(myUrl) + url: constants.CONFIG.WEBSERVER.URL, + signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL) } } diff --git a/server/initializers/constants.js b/server/initializers/constants.js index cd2e0cfb9..ce9f8ad6c 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -1,8 +1,34 @@ 'use strict' +const config = require('config') +const path = require('path') + // API version of our pod const API_VERSION = 'v1' +const CONFIG = { + DATABASE: { + DBNAME: 'peertube' + config.get('database.suffix'), + HOST: config.get('database.host'), + PORT: config.get('database.port') + }, + ELECTRON: { + DEBUG: config.get('electron.debug') + }, + STORAGE: { + CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')), + LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')), + UPLOAD_DIR: path.join(__dirname, '..', '..', config.get('storage.uploads')), + THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails')) + }, + WEBSERVER: { + SCHEME: config.get('webserver.https') === true ? 'https' : 'http', + HOST: config.get('webserver.host'), + PORT: config.get('webserver.port') + } +} +CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOST + ':' + CONFIG.WEBSERVER.PORT + const CONSTRAINTS_FIELDS = { USERS: { USERNAME: { min: 3, max: 20 }, // Length @@ -89,6 +115,7 @@ if (isTestInstance() === true) { module.exports = { API_VERSION: API_VERSION, + CONFIG: CONFIG, CONSTRAINTS_FIELDS: CONSTRAINTS_FIELDS, FRIEND_SCORE: FRIEND_SCORE, INTERVAL: INTERVAL, diff --git a/server/initializers/database.js b/server/initializers/database.js index 8626895ee..20dcc056e 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js @@ -1,8 +1,8 @@ 'use strict' -const config = require('config') const mongoose = require('mongoose') +const constants = require('../initializers/constants') const logger = require('../helpers/logger') // Bootstrap models @@ -14,17 +14,13 @@ require('../models/video') // Request model needs Video model require('../models/request') -const dbname = 'peertube' + config.get('database.suffix') -const host = config.get('database.host') -const port = config.get('database.port') - const database = { connect: connect } function connect () { mongoose.Promise = global.Promise - mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname) + mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOST + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME) mongoose.connection.on('error', function () { throw new Error('Mongodb connection error.') }) diff --git a/server/lib/friends.js b/server/lib/friends.js index 6e1516b94..6a2c37fd7 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -11,12 +11,8 @@ const waterfall = require('async/waterfall') const constants = require('../initializers/constants') const logger = require('../helpers/logger') -const peertubeCrypto = require('../helpers/peertube-crypto') const requests = require('../helpers/requests') -const http = config.get('webserver.https') ? 'https' : 'http' -const host = config.get('webserver.host') -const port = config.get('webserver.port') const Pod = mongoose.model('Pod') const Request = mongoose.model('Request') const Video = mongoose.model('Video') @@ -45,7 +41,7 @@ function hasFriends (callback) { } function getMyCertificate (callback) { - fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback) + fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) } function makeFriends (callback) { @@ -220,7 +216,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { url: pod.url + '/api/' + constants.API_VERSION + '/pods/', method: 'POST', json: { - url: http + '://' + host + ':' + port, + url: constants.CONFIG.WEBSERVER.URL, publicKey: cert } } diff --git a/server/lib/webtorrent.js b/server/lib/webtorrent.js index bcd30139e..2090b792b 100644 --- a/server/lib/webtorrent.js +++ b/server/lib/webtorrent.js @@ -1,15 +1,14 @@ 'use strict' -const config = require('config') const ipc = require('node-ipc') const pathUtils = require('path') const spawn = require('electron-spawn') +const constants = require('../initializers/constants') const logger = require('../helpers/logger') -const electronDebug = config.get('electron.debug') -let host = config.get('webserver.host') -let port = config.get('webserver.port') +let host = constants.CONFIG.WEBSERVER.HOST +let port = constants.CONFIG.WEBSERVER.PORT let nodeKey = 'webtorrentnode' + port let processKey = 'webtorrentprocess' + port ipc.config.silent = true @@ -59,7 +58,7 @@ function create (options, callback) { const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true }) - if (electronDebug === true) { + if (constants.CONFIG.ELECTRON.DEBUG === true) { webtorrentProcess.stderr.on('data', function (data) { logger.debug('Webtorrent process stderr: ', data.toString()) }) diff --git a/server/models/video.js b/server/models/video.js index 63afc2efe..0f60b6cd4 100644 --- a/server/models/video.js +++ b/server/models/video.js @@ -1,6 +1,5 @@ 'use strict' -const config = require('config') const eachLimit = require('async/eachLimit') const ffmpeg = require('fluent-ffmpeg') const fs = require('fs') @@ -15,12 +14,6 @@ const modelUtils = require('./utils') const utils = require('../helpers/utils') const webtorrent = require('../lib/webtorrent') -const http = config.get('webserver.https') === true ? 'https' : 'http' -const host = config.get('webserver.host') -const port = config.get('webserver.port') -const uploadsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads')) -const thumbnailsDir = pathUtils.join(__dirname, '..', '..', config.get('storage.thumbnails')) - // --------------------------------------------------------------------------- // TODO: add indexes on searchable columns @@ -101,8 +94,8 @@ VideoSchema.pre('save', function (next) { const tasks = [] if (video.isOwned()) { - const videoPath = pathUtils.join(uploadsDir, video.filename) - this.podUrl = http + '://' + host + ':' + port + const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename) + this.podUrl = constants.CONFIG.WEBSERVER.URL tasks.push( function (callback) { @@ -162,7 +155,7 @@ function toRemoteJSON (callback) { const self = this // Convert thumbnail to base64 - fs.readFile(pathUtils.join(thumbnailsDir, this.thumbnail), function (err, thumbnailData) { + fs.readFile(pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, this.thumbnail), function (err, thumbnailData) { if (err) { logger.error('Cannot read the thumbnail of the video') return callback(err) @@ -242,7 +235,7 @@ function seedAllExisting (callback) { if (err) return callback(err) eachLimit(videos, constants.SEEDS_IN_PARALLEL, function (video, callbackEach) { - const videoPath = pathUtils.join(uploadsDir, video.filename) + const videoPath = pathUtils.join(constants.CONFIG.STORAGE.UPLOAD_DIR, video.filename) seed(videoPath, callbackEach) }, callback) }) @@ -251,11 +244,11 @@ function seedAllExisting (callback) { // --------------------------------------------------------------------------- function removeThumbnail (video, callback) { - fs.unlink(thumbnailsDir + video.thumbnail, callback) + fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.thumbnail, callback) } function removeFile (video, callback) { - fs.unlink(uploadsDir + video.filename, callback) + fs.unlink(constants.CONFIG.STORAGE.UPLOAD_DIR + video.filename, callback) } // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process @@ -277,7 +270,7 @@ function createThumbnail (videoPath, callback) { }) .thumbnail({ count: 1, - folder: thumbnailsDir, + folder: constants.CONFIG.STORAGE.THUMBNAILS_DIR, size: constants.THUMBNAILS_SIZE, filename: filename }) @@ -299,7 +292,7 @@ function generateThumbnailFromBase64 (data, callback) { if (err) return callback(err) const thumbnailName = randomString + '.jpg' - fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, function (err) { + fs.writeFile(constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName, data, { encoding: 'base64' }, function (err) { if (err) return callback(err) return callback(null, thumbnailName)