Server: put config in constants

pull/10/head
Chocobozzz 2016-08-19 21:34:51 +02:00
parent 5c39adb731
commit e861452fb2
10 changed files with 72 additions and 65 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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.')
})

View File

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

View File

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

View File

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