feat(config): set config default value on render index

This commit is contained in:
OldHawk
2017-12-29 13:56:08 +08:00
parent 71fa4692c2
commit 11635470bc
3 changed files with 28 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ module.exports = {
*/
app: {
name: 'CHD.im',
domain: 'http://chd.im:3000',
domain: 'http://chd:3000',
showDemoWarningPopup: true,
cronTimeZone: 'Asia/Shanghai',
showDebugLog: true

View File

@@ -1,8 +1,6 @@
'use strict';
var validator = require('validator'),
path = require('path'),
moment = require('moment'),
var path = require('path'),
config = require(path.resolve('./config/config'));
/**
@@ -15,6 +13,8 @@ exports.renderIndex = function (req, res) {
req.user.addSignedIp(ip);
}
setMeanTorrentConfigDefaultValue(req, res, config.meanTorrentConfig);
res.render('modules/core/server/views/index', {
user: JSON.stringify(safeUserObject),
sharedConfig: JSON.stringify(config.shared),
@@ -28,6 +28,8 @@ exports.renderIndex = function (req, res) {
exports.renderServerError = function (req, res) {
var safeUserObject = req.user || null;
setMeanTorrentConfigDefaultValue(req, res, config.meanTorrentConfig);
res.status(500).render('modules/core/server/views/500', {
user: JSON.stringify(safeUserObject),
sharedConfig: JSON.stringify(config.shared),
@@ -43,6 +45,8 @@ exports.renderServerError = function (req, res) {
exports.renderNotFound = function (req, res) {
var safeUserObject = req.user || null;
setMeanTorrentConfigDefaultValue(req, res, config.meanTorrentConfig);
res.status(404).format({
'text/html': function () {
res.render('modules/core/server/views/404', {
@@ -92,3 +96,20 @@ function getSafeMeanTorrentConfig(cfg) {
return cfg;
}
/**
* setMeanTorrentConfigDefaultValue
* @param cfg
*/
function setMeanTorrentConfigDefaultValue(req, res, cfg) {
var httpTransport = 'http://';
if (config.secure && config.secure.ssl === true) {
httpTransport = 'https://';
}
var baseUrl = req.app.get('domain') || httpTransport + req.headers.host;
cfg.app.domain = baseUrl;
if (!cfg.announce.url.startsWith('http')) {
cfg.announce.url = baseUrl + cfg.announce.url;
}
}

View File

@@ -204,7 +204,7 @@ exports.upload = function (req, res) {
} else {
if (config.meanTorrentConfig.announce.privateTorrentCmsMode) {
//force change announce url to config value
var announce = appConfig.domain + config.meanTorrentConfig.announce.url;
var announce = config.meanTorrentConfig.announce.url;
torrent.metadata.announce = announce;
var cws = fs.createWriteStream(newfile);
@@ -477,7 +477,7 @@ exports.announceEdit = function (req, res) {
message = 'Read torrent file faild';
reject(message);
} else {
var announce = appConfig.domain + config.meanTorrentConfig.announce.url;
var announce = config.meanTorrentConfig.announce.url;
torrent.metadata.announce = announce;
torrent.metadata.comment = req.query.comment;
torrent_data = torrent.metadata;
@@ -551,7 +551,7 @@ exports.download = function (req, res) {
reject(message);
} else {
if (config.meanTorrentConfig.announce.privateTorrentCmsMode) {
var announce = appConfig.domain + config.meanTorrentConfig.announce.url + '/' + user.passkey;
var announce = config.meanTorrentConfig.announce.url + '/' + user.passkey;
torrent.metadata.announce = announce;
}
torrent_data = torrent.metadata;