aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/constants.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-17 10:32:03 +0100
committerChocobozzz <me@florianbigard.com>2018-01-17 10:41:27 +0100
commitfd206f0b2d7e5c8e00e2817266d90ec54f79e1da (patch)
tree86b096cf2abd7eb49b892de1c9be855f45a41a9c /server/initializers/constants.ts
parent9581cabc596acb18c0ad86bcf3a07c2b45e8e47e (diff)
downloadPeerTube-fd206f0b2d7e5c8e00e2817266d90ec54f79e1da.tar.gz
PeerTube-fd206f0b2d7e5c8e00e2817266d90ec54f79e1da.tar.zst
PeerTube-fd206f0b2d7e5c8e00e2817266d90ec54f79e1da.zip
Add ability to update some configuration keys
Diffstat (limited to 'server/initializers/constants.ts')
-rw-r--r--server/initializers/constants.ts84
1 files changed, 67 insertions, 17 deletions
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 759880201..7b63a9ccd 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -1,11 +1,14 @@
1import * as config from 'config' 1import { IConfig } from 'config'
2import { join } from 'path' 2import { dirname, join } from 'path'
3import { JobCategory, JobState, VideoRateType } from '../../shared/models' 3import { JobCategory, JobState, VideoRateType } from '../../shared/models'
4import { ActivityPubActorType } from '../../shared/models/activitypub' 4import { ActivityPubActorType } from '../../shared/models/activitypub'
5import { FollowState } from '../../shared/models/actors' 5import { FollowState } from '../../shared/models/actors'
6import { VideoPrivacy } from '../../shared/models/videos' 6import { VideoPrivacy } from '../../shared/models/videos'
7// Do not use barrels, remain constants as independent as possible 7// Do not use barrels, remain constants as independent as possible
8import { buildPath, isTestInstance, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' 8import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9
10// Use a variable to reload the configuration if we need
11let config: IConfig = require('config')
9 12
10// --------------------------------------------------------------------------- 13// ---------------------------------------------------------------------------
11 14
@@ -82,6 +85,7 @@ let SCHEDULER_INTERVAL = 60000 * 60
82// --------------------------------------------------------------------------- 85// ---------------------------------------------------------------------------
83 86
84const CONFIG = { 87const CONFIG = {
88 CUSTOM_FILE: getLocalConfigFilePath(),
85 LISTEN: { 89 LISTEN: {
86 PORT: config.get<number>('listen.port') 90 PORT: config.get<number>('listen.port')
87 }, 91 },
@@ -110,29 +114,29 @@ const CONFIG = {
110 HOST: '' 114 HOST: ''
111 }, 115 },
112 ADMIN: { 116 ADMIN: {
113 EMAIL: config.get<string>('admin.email') 117 get EMAIL () { return config.get<string>('admin.email') }
114 }, 118 },
115 SIGNUP: { 119 SIGNUP: {
116 ENABLED: config.get<boolean>('signup.enabled'), 120 get ENABLED () { return config.get<boolean>('signup.enabled') },
117 LIMIT: config.get<number>('signup.limit') 121 get LIMIT () { return config.get<number>('signup.limit') }
118 }, 122 },
119 USER: { 123 USER: {
120 VIDEO_QUOTA: config.get<number>('user.video_quota') 124 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
121 }, 125 },
122 TRANSCODING: { 126 TRANSCODING: {
123 ENABLED: config.get<boolean>('transcoding.enabled'), 127 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
124 THREADS: config.get<number>('transcoding.threads'), 128 get THREADS () { return config.get<number>('transcoding.threads') },
125 RESOLUTIONS: { 129 RESOLUTIONS: {
126 '240' : config.get<boolean>('transcoding.resolutions.240p'), 130 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
127 '360': config.get<boolean>('transcoding.resolutions.360p'), 131 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
128 '480': config.get<boolean>('transcoding.resolutions.480p'), 132 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
129 '720': config.get<boolean>('transcoding.resolutions.720p'), 133 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
130 '1080': config.get<boolean>('transcoding.resolutions.1080p') 134 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
131 } 135 }
132 }, 136 },
133 CACHE: { 137 CACHE: {
134 PREVIEWS: { 138 PREVIEWS: {
135 SIZE: config.get<number>('cache.previews.size') 139 get SIZE () { return config.get<number>('cache.previews.size') }
136 } 140 }
137 } 141 }
138} 142}
@@ -361,8 +365,7 @@ if (isTestInstance() === true) {
361 SCHEDULER_INTERVAL = 10000 365 SCHEDULER_INTERVAL = 10000
362} 366}
363 367
364CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT) 368updateWebserverConfig()
365CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
366 369
367// --------------------------------------------------------------------------- 370// ---------------------------------------------------------------------------
368 371
@@ -404,3 +407,50 @@ export {
404 AVATAR_MIMETYPE_EXT, 407 AVATAR_MIMETYPE_EXT,
405 SCHEDULER_INTERVAL 408 SCHEDULER_INTERVAL
406} 409}
410
411// ---------------------------------------------------------------------------
412
413function getLocalConfigFilePath () {
414 const configSources = config.util.getConfigSources()
415 if (configSources.length === 0) throw new Error('Invalid config source.')
416
417 let filename = 'local'
418 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
419 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
420
421 return join(dirname(configSources[ 0 ].name), filename + '.json')
422}
423
424function updateWebserverConfig () {
425 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
426 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
427}
428
429export function reloadConfig () {
430
431 function directory () {
432 if (process.env.NODE_CONFIG_DIR) {
433 return process.env.NODE_CONFIG_DIR
434 }
435
436 return join(root(), 'config')
437 }
438
439 function purge () {
440 for (const fileName in require.cache) {
441 if (-1 === fileName.indexOf(directory())) {
442 continue
443 }
444
445 delete require.cache[fileName]
446 }
447
448 delete require.cache[require.resolve('config')]
449 }
450
451 purge()
452
453 config = require('config')
454
455 updateWebserverConfig()
456}