diff options
author | Chocobozzz <me@florianbigard.com> | 2018-01-17 10:32:03 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-01-17 10:41:27 +0100 |
commit | fd206f0b2d7e5c8e00e2817266d90ec54f79e1da (patch) | |
tree | 86b096cf2abd7eb49b892de1c9be855f45a41a9c /server/initializers | |
parent | 9581cabc596acb18c0ad86bcf3a07c2b45e8e47e (diff) | |
download | PeerTube-fd206f0b2d7e5c8e00e2817266d90ec54f79e1da.tar.gz PeerTube-fd206f0b2d7e5c8e00e2817266d90ec54f79e1da.tar.zst PeerTube-fd206f0b2d7e5c8e00e2817266d90ec54f79e1da.zip |
Add ability to update some configuration keys
Diffstat (limited to 'server/initializers')
-rw-r--r-- | server/initializers/constants.ts | 84 |
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 @@ | |||
1 | import * as config from 'config' | 1 | import { IConfig } from 'config' |
2 | import { join } from 'path' | 2 | import { dirname, join } from 'path' |
3 | import { JobCategory, JobState, VideoRateType } from '../../shared/models' | 3 | import { JobCategory, JobState, VideoRateType } from '../../shared/models' |
4 | import { ActivityPubActorType } from '../../shared/models/activitypub' | 4 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
5 | import { FollowState } from '../../shared/models/actors' | 5 | import { FollowState } from '../../shared/models/actors' |
6 | import { VideoPrivacy } from '../../shared/models/videos' | 6 | import { 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 |
8 | import { buildPath, isTestInstance, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' | 8 | import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' |
9 | |||
10 | // Use a variable to reload the configuration if we need | ||
11 | let config: IConfig = require('config') | ||
9 | 12 | ||
10 | // --------------------------------------------------------------------------- | 13 | // --------------------------------------------------------------------------- |
11 | 14 | ||
@@ -82,6 +85,7 @@ let SCHEDULER_INTERVAL = 60000 * 60 | |||
82 | // --------------------------------------------------------------------------- | 85 | // --------------------------------------------------------------------------- |
83 | 86 | ||
84 | const CONFIG = { | 87 | const 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 | ||
364 | CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT) | 368 | updateWebserverConfig() |
365 | CONFIG.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 | |||
413 | function 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 | |||
424 | function 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 | |||
429 | export 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 | } | ||