From 6dd9de95dfa39bd5c1faed00d1dbd52cd112bae0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 11 Apr 2019 11:33:44 +0200 Subject: Move config in its own file --- server/initializers/checker-after-init.ts | 5 +- server/initializers/config.ts | 255 ++++++++++++++++ server/initializers/constants.ts | 321 ++++----------------- server/initializers/database.ts | 2 +- server/initializers/installer.ts | 5 +- .../migrations/0075-video-resolutions.ts | 2 +- server/initializers/migrations/0140-actor-url.ts | 6 +- .../migrations/0235-delete-some-video-indexes.ts | 4 - .../migrations/0240-drop-old-indexes.ts | 4 - .../migrations/0345-video-playlists.ts | 4 +- 10 files changed, 319 insertions(+), 289 deletions(-) create mode 100644 server/initializers/config.ts (limited to 'server/initializers') diff --git a/server/initializers/checker-after-init.ts b/server/initializers/checker-after-init.ts index 85f752284..db3115085 100644 --- a/server/initializers/checker-after-init.ts +++ b/server/initializers/checker-after-init.ts @@ -4,19 +4,20 @@ import { UserModel } from '../models/account/user' import { ApplicationModel } from '../models/application/application' import { OAuthClientModel } from '../models/oauth/oauth-client' import { parse } from 'url' -import { CONFIG } from './constants' +import { CONFIG } from './config' import { logger } from '../helpers/logger' import { getServerActor } from '../helpers/utils' import { RecentlyAddedStrategy } from '../../shared/models/redundancy' import { isArray } from '../helpers/custom-validators/misc' import { uniq } from 'lodash' import { Emailer } from '../lib/emailer' +import { WEBSERVER } from './constants' async function checkActivityPubUrls () { const actor = await getServerActor() const parsed = parse(actor.url) - if (CONFIG.WEBSERVER.HOST !== parsed.host) { + if (WEBSERVER.HOST !== parsed.host) { const NODE_ENV = config.util.getEnv('NODE_ENV') const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR') diff --git a/server/initializers/config.ts b/server/initializers/config.ts new file mode 100644 index 000000000..8dd62cba8 --- /dev/null +++ b/server/initializers/config.ts @@ -0,0 +1,255 @@ +import { IConfig } from 'config' +import { dirname, join } from 'path' +import { VideosRedundancy } from '../../shared/models' +// Do not use barrels, remain constants as independent as possible +import { buildPath, parseBytes, parseDuration, root } from '../helpers/core-utils' +import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type' +import * as bytes from 'bytes' + +// Use a variable to reload the configuration if we need +let config: IConfig = require('config') + +const configChangedHandlers: Function[] = [] + +const CONFIG = { + CUSTOM_FILE: getLocalConfigFilePath(), + LISTEN: { + PORT: config.get('listen.port'), + HOSTNAME: config.get('listen.hostname') + }, + DATABASE: { + DBNAME: 'peertube' + config.get('database.suffix'), + HOSTNAME: config.get('database.hostname'), + PORT: config.get('database.port'), + USERNAME: config.get('database.username'), + PASSWORD: config.get('database.password'), + POOL: { + MAX: config.get('database.pool.max') + } + }, + REDIS: { + HOSTNAME: config.has('redis.hostname') ? config.get('redis.hostname') : null, + PORT: config.has('redis.port') ? config.get('redis.port') : null, + SOCKET: config.has('redis.socket') ? config.get('redis.socket') : null, + AUTH: config.has('redis.auth') ? config.get('redis.auth') : null, + DB: config.has('redis.db') ? config.get('redis.db') : null + }, + SMTP: { + HOSTNAME: config.get('smtp.hostname'), + PORT: config.get('smtp.port'), + USERNAME: config.get('smtp.username'), + PASSWORD: config.get('smtp.password'), + TLS: config.get('smtp.tls'), + DISABLE_STARTTLS: config.get('smtp.disable_starttls'), + CA_FILE: config.get('smtp.ca_file'), + FROM_ADDRESS: config.get('smtp.from_address') + }, + STORAGE: { + TMP_DIR: buildPath(config.get('storage.tmp')), + AVATARS_DIR: buildPath(config.get('storage.avatars')), + LOG_DIR: buildPath(config.get('storage.logs')), + VIDEOS_DIR: buildPath(config.get('storage.videos')), + STREAMING_PLAYLISTS_DIR: buildPath(config.get('storage.streaming_playlists')), + REDUNDANCY_DIR: buildPath(config.get('storage.redundancy')), + THUMBNAILS_DIR: buildPath(config.get('storage.thumbnails')), + PREVIEWS_DIR: buildPath(config.get('storage.previews')), + CAPTIONS_DIR: buildPath(config.get('storage.captions')), + TORRENTS_DIR: buildPath(config.get('storage.torrents')), + CACHE_DIR: buildPath(config.get('storage.cache')) + }, + WEBSERVER: { + SCHEME: config.get('webserver.https') === true ? 'https' : 'http', + WS: config.get('webserver.https') === true ? 'wss' : 'ws', + HOSTNAME: config.get('webserver.hostname'), + PORT: config.get('webserver.port') + }, + TRUST_PROXY: config.get('trust_proxy'), + LOG: { + LEVEL: config.get('log.level') + }, + SEARCH: { + REMOTE_URI: { + USERS: config.get('search.remote_uri.users'), + ANONYMOUS: config.get('search.remote_uri.anonymous') + } + }, + TRENDING: { + VIDEOS: { + INTERVAL_DAYS: config.get('trending.videos.interval_days') + } + }, + REDUNDANCY: { + VIDEOS: { + CHECK_INTERVAL: parseDuration(config.get('redundancy.videos.check_interval')), + STRATEGIES: buildVideosRedundancy(config.get('redundancy.videos.strategies')) + } + }, + CSP: { + ENABLED: config.get('csp.enabled'), + REPORT_ONLY: config.get('csp.report_only'), + REPORT_URI: config.get('csp.report_uri') + }, + TRACKER: { + ENABLED: config.get('tracker.enabled'), + PRIVATE: config.get('tracker.private'), + REJECT_TOO_MANY_ANNOUNCES: config.get('tracker.reject_too_many_announces') + }, + ADMIN: { + get EMAIL () { return config.get('admin.email') } + }, + CONTACT_FORM: { + get ENABLED () { return config.get('contact_form.enabled') } + }, + SIGNUP: { + get ENABLED () { return config.get('signup.enabled') }, + get LIMIT () { return config.get('signup.limit') }, + get REQUIRES_EMAIL_VERIFICATION () { return config.get('signup.requires_email_verification') }, + FILTERS: { + CIDR: { + get WHITELIST () { return config.get('signup.filters.cidr.whitelist') }, + get BLACKLIST () { return config.get('signup.filters.cidr.blacklist') } + } + } + }, + USER: { + get VIDEO_QUOTA () { return parseBytes(config.get('user.video_quota')) }, + get VIDEO_QUOTA_DAILY () { return parseBytes(config.get('user.video_quota_daily')) } + }, + TRANSCODING: { + get ENABLED () { return config.get('transcoding.enabled') }, + get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get('transcoding.allow_additional_extensions') }, + get THREADS () { return config.get('transcoding.threads') }, + RESOLUTIONS: { + get '240p' () { return config.get('transcoding.resolutions.240p') }, + get '360p' () { return config.get('transcoding.resolutions.360p') }, + get '480p' () { return config.get('transcoding.resolutions.480p') }, + get '720p' () { return config.get('transcoding.resolutions.720p') }, + get '1080p' () { return config.get('transcoding.resolutions.1080p') } + }, + HLS: { + get ENABLED () { return config.get('transcoding.hls.enabled') } + } + }, + IMPORT: { + VIDEOS: { + HTTP: { + get ENABLED () { return config.get('import.videos.http.enabled') } + }, + TORRENT: { + get ENABLED () { return config.get('import.videos.torrent.enabled') } + } + } + }, + AUTO_BLACKLIST: { + VIDEOS: { + OF_USERS: { + get ENABLED () { return config.get('auto_blacklist.videos.of_users.enabled') } + } + } + }, + CACHE: { + PREVIEWS: { + get SIZE () { return config.get('cache.previews.size') } + }, + VIDEO_CAPTIONS: { + get SIZE () { return config.get('cache.captions.size') } + } + }, + INSTANCE: { + get NAME () { return config.get('instance.name') }, + get SHORT_DESCRIPTION () { return config.get('instance.short_description') }, + get DESCRIPTION () { return config.get('instance.description') }, + get TERMS () { return config.get('instance.terms') }, + get IS_NSFW () { return config.get('instance.is_nsfw') }, + get DEFAULT_CLIENT_ROUTE () { return config.get('instance.default_client_route') }, + get DEFAULT_NSFW_POLICY () { return config.get('instance.default_nsfw_policy') }, + CUSTOMIZATIONS: { + get JAVASCRIPT () { return config.get('instance.customizations.javascript') }, + get CSS () { return config.get('instance.customizations.css') } + }, + get ROBOTS () { return config.get('instance.robots') }, + get SECURITYTXT () { return config.get('instance.securitytxt') }, + get SECURITYTXT_CONTACT () { return config.get('admin.email') } + }, + SERVICES: { + TWITTER: { + get USERNAME () { return config.get('services.twitter.username') }, + get WHITELISTED () { return config.get('services.twitter.whitelisted') } + } + }, + FOLLOWERS: { + INSTANCE: { + get ENABLED () { return config.get('followers.instance.enabled') }, + get MANUAL_APPROVAL () { return config.get('followers.instance.manual_approval') } + } + } +} + +function registerConfigChangedHandler (fun: Function) { + configChangedHandlers.push(fun) +} + +// --------------------------------------------------------------------------- + +export { + CONFIG, + registerConfigChangedHandler +} + +// --------------------------------------------------------------------------- + +function getLocalConfigFilePath () { + const configSources = config.util.getConfigSources() + if (configSources.length === 0) throw new Error('Invalid config source.') + + let filename = 'local' + if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}` + if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}` + + return join(dirname(configSources[ 0 ].name), filename + '.json') +} + +function buildVideosRedundancy (objs: any[]): VideosRedundancy[] { + if (!objs) return [] + + if (!Array.isArray(objs)) return objs + + return objs.map(obj => { + return Object.assign({}, obj, { + minLifetime: parseDuration(obj.min_lifetime), + size: bytes.parse(obj.size), + minViews: obj.min_views + }) + }) +} + +export function reloadConfig () { + + function directory () { + if (process.env.NODE_CONFIG_DIR) { + return process.env.NODE_CONFIG_DIR + } + + return join(root(), 'config') + } + + function purge () { + for (const fileName in require.cache) { + if (-1 === fileName.indexOf(directory())) { + continue + } + + delete require.cache[fileName] + } + + delete require.cache[require.resolve('config')] + } + + purge() + + config = require('config') + + for (const configChangedHandler of configChangedHandlers) { + configChangedHandler() + } +} diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 739ea5502..3922d8515 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -1,20 +1,16 @@ -import { IConfig } from 'config' -import { dirname, join } from 'path' -import { JobType, VideoRateType, VideoState, VideosRedundancy } from '../../shared/models' +import { join } from 'path' +import { JobType, VideoRateType, VideoState } from '../../shared/models' import { ActivityPubActorType } from '../../shared/models/activitypub' import { FollowState } from '../../shared/models/actors' import { VideoAbuseState, VideoImportState, VideoPrivacy, VideoTranscodingFPS } from '../../shared/models/videos' // Do not use barrels, remain constants as independent as possible -import { buildPath, isTestInstance, parseDuration, parseBytes, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' +import { isTestInstance, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type' import { invert } from 'lodash' import { CronRepeatOptions, EveryRepeatOptions } from 'bull' -import * as bytes from 'bytes' import { VideoPlaylistPrivacy } from '../../shared/models/videos/playlist/video-playlist-privacy.model' import { VideoPlaylistType } from '../../shared/models/videos/playlist/video-playlist-type.model' - -// Use a variable to reload the configuration if we need -let config: IConfig = require('config') +import { CONFIG, registerConfigChangedHandler } from './config' // --------------------------------------------------------------------------- @@ -32,6 +28,15 @@ const PAGINATION = { } } +const WEBSERVER = { + URL: '', + HOST: '', + SCHEME: '', + WS: '', + HOSTNAME: '', + PORT: 0 +} + // Sortable columns per schema const SORTABLE_COLUMNS = { USERS: [ 'id', 'username', 'createdAt' ], @@ -102,7 +107,7 @@ const REMOTE_SCHEME = { } // TODO: remove 'video-file' -const JOB_ATTEMPTS: { [ id in (JobType | 'video-file') ]: number } = { +const JOB_ATTEMPTS: { [id in (JobType | 'video-file')]: number } = { 'activitypub-http-broadcast': 5, 'activitypub-http-unicast': 5, 'activitypub-http-fetcher': 5, @@ -115,7 +120,7 @@ const JOB_ATTEMPTS: { [ id in (JobType | 'video-file') ]: number } = { 'videos-views': 1, 'activitypub-refresher': 1 } -const JOB_CONCURRENCY: { [ id in (JobType | 'video-file') ]: number } = { +const JOB_CONCURRENCY: { [id in (JobType | 'video-file')]: number } = { 'activitypub-http-broadcast': 1, 'activitypub-http-unicast': 5, 'activitypub-http-fetcher': 1, @@ -128,7 +133,7 @@ const JOB_CONCURRENCY: { [ id in (JobType | 'video-file') ]: number } = { 'videos-views': 1, 'activitypub-refresher': 1 } -const JOB_TTL: { [ id in (JobType | 'video-file') ]: number } = { +const JOB_TTL: { [id in (JobType | 'video-file')]: number } = { 'activitypub-http-broadcast': 60000 * 10, // 10 minutes 'activitypub-http-unicast': 60000 * 10, // 10 minutes 'activitypub-http-fetcher': 60000 * 10, // 10 minutes @@ -163,184 +168,6 @@ let SCHEDULER_INTERVALS_MS = { // --------------------------------------------------------------------------- -const CONFIG = { - CUSTOM_FILE: getLocalConfigFilePath(), - LISTEN: { - PORT: config.get('listen.port'), - HOSTNAME: config.get('listen.hostname') - }, - DATABASE: { - DBNAME: 'peertube' + config.get('database.suffix'), - HOSTNAME: config.get('database.hostname'), - PORT: config.get('database.port'), - USERNAME: config.get('database.username'), - PASSWORD: config.get('database.password'), - POOL: { - MAX: config.get('database.pool.max') - } - }, - REDIS: { - HOSTNAME: config.has('redis.hostname') ? config.get('redis.hostname') : null, - PORT: config.has('redis.port') ? config.get('redis.port') : null, - SOCKET: config.has('redis.socket') ? config.get('redis.socket') : null, - AUTH: config.has('redis.auth') ? config.get('redis.auth') : null, - DB: config.has('redis.db') ? config.get('redis.db') : null - }, - SMTP: { - HOSTNAME: config.get('smtp.hostname'), - PORT: config.get('smtp.port'), - USERNAME: config.get('smtp.username'), - PASSWORD: config.get('smtp.password'), - TLS: config.get('smtp.tls'), - DISABLE_STARTTLS: config.get('smtp.disable_starttls'), - CA_FILE: config.get('smtp.ca_file'), - FROM_ADDRESS: config.get('smtp.from_address') - }, - STORAGE: { - TMP_DIR: buildPath(config.get('storage.tmp')), - AVATARS_DIR: buildPath(config.get('storage.avatars')), - LOG_DIR: buildPath(config.get('storage.logs')), - VIDEOS_DIR: buildPath(config.get('storage.videos')), - STREAMING_PLAYLISTS_DIR: buildPath(config.get('storage.streaming_playlists')), - REDUNDANCY_DIR: buildPath(config.get('storage.redundancy')), - THUMBNAILS_DIR: buildPath(config.get('storage.thumbnails')), - PREVIEWS_DIR: buildPath(config.get('storage.previews')), - CAPTIONS_DIR: buildPath(config.get('storage.captions')), - TORRENTS_DIR: buildPath(config.get('storage.torrents')), - CACHE_DIR: buildPath(config.get('storage.cache')) - }, - WEBSERVER: { - SCHEME: config.get('webserver.https') === true ? 'https' : 'http', - WS: config.get('webserver.https') === true ? 'wss' : 'ws', - HOSTNAME: config.get('webserver.hostname'), - PORT: config.get('webserver.port'), - URL: '', - HOST: '' - }, - TRUST_PROXY: config.get('trust_proxy'), - LOG: { - LEVEL: config.get('log.level') - }, - SEARCH: { - REMOTE_URI: { - USERS: config.get('search.remote_uri.users'), - ANONYMOUS: config.get('search.remote_uri.anonymous') - } - }, - TRENDING: { - VIDEOS: { - INTERVAL_DAYS: config.get('trending.videos.interval_days') - } - }, - REDUNDANCY: { - VIDEOS: { - CHECK_INTERVAL: parseDuration(config.get('redundancy.videos.check_interval')), - STRATEGIES: buildVideosRedundancy(config.get('redundancy.videos.strategies')) - } - }, - CSP: { - ENABLED: config.get('csp.enabled'), - REPORT_ONLY: config.get('csp.report_only'), - REPORT_URI: config.get('csp.report_uri') - }, - TRACKER: { - ENABLED: config.get('tracker.enabled'), - PRIVATE: config.get('tracker.private'), - REJECT_TOO_MANY_ANNOUNCES: config.get('tracker.reject_too_many_announces') - }, - ADMIN: { - get EMAIL () { return config.get('admin.email') } - }, - CONTACT_FORM: { - get ENABLED () { return config.get('contact_form.enabled') } - }, - SIGNUP: { - get ENABLED () { return config.get('signup.enabled') }, - get LIMIT () { return config.get('signup.limit') }, - get REQUIRES_EMAIL_VERIFICATION () { return config.get('signup.requires_email_verification') }, - FILTERS: { - CIDR: { - get WHITELIST () { return config.get('signup.filters.cidr.whitelist') }, - get BLACKLIST () { return config.get('signup.filters.cidr.blacklist') } - } - } - }, - USER: { - get VIDEO_QUOTA () { return parseBytes(config.get('user.video_quota')) }, - get VIDEO_QUOTA_DAILY () { return parseBytes(config.get('user.video_quota_daily')) } - }, - TRANSCODING: { - get ENABLED () { return config.get('transcoding.enabled') }, - get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get('transcoding.allow_additional_extensions') }, - get THREADS () { return config.get('transcoding.threads') }, - RESOLUTIONS: { - get '240p' () { return config.get('transcoding.resolutions.240p') }, - get '360p' () { return config.get('transcoding.resolutions.360p') }, - get '480p' () { return config.get('transcoding.resolutions.480p') }, - get '720p' () { return config.get('transcoding.resolutions.720p') }, - get '1080p' () { return config.get('transcoding.resolutions.1080p') } - }, - HLS: { - get ENABLED () { return config.get('transcoding.hls.enabled') } - } - }, - IMPORT: { - VIDEOS: { - HTTP: { - get ENABLED () { return config.get('import.videos.http.enabled') } - }, - TORRENT: { - get ENABLED () { return config.get('import.videos.torrent.enabled') } - } - } - }, - AUTO_BLACKLIST: { - VIDEOS: { - OF_USERS: { - get ENABLED () { return config.get('auto_blacklist.videos.of_users.enabled') } - } - } - }, - CACHE: { - PREVIEWS: { - get SIZE () { return config.get('cache.previews.size') } - }, - VIDEO_CAPTIONS: { - get SIZE () { return config.get('cache.captions.size') } - } - }, - INSTANCE: { - get NAME () { return config.get('instance.name') }, - get SHORT_DESCRIPTION () { return config.get('instance.short_description') }, - get DESCRIPTION () { return config.get('instance.description') }, - get TERMS () { return config.get('instance.terms') }, - get IS_NSFW () { return config.get('instance.is_nsfw') }, - get DEFAULT_CLIENT_ROUTE () { return config.get('instance.default_client_route') }, - get DEFAULT_NSFW_POLICY () { return config.get('instance.default_nsfw_policy') }, - CUSTOMIZATIONS: { - get JAVASCRIPT () { return config.get('instance.customizations.javascript') }, - get CSS () { return config.get('instance.customizations.css') } - }, - get ROBOTS () { return config.get('instance.robots') }, - get SECURITYTXT () { return config.get('instance.securitytxt') }, - get SECURITYTXT_CONTACT () { return config.get('admin.email') } - }, - SERVICES: { - TWITTER: { - get USERNAME () { return config.get('services.twitter.username') }, - get WHITELISTED () { return config.get('services.twitter.whitelisted') } - } - }, - FOLLOWERS: { - INSTANCE: { - get ENABLED () { return config.get('followers.instance.enabled') }, - get MANUAL_APPROVAL () { return config.get('followers.instance.manual_approval') } - } - } -} - -// --------------------------------------------------------------------------- - let CONSTRAINTS_FIELDS = { USERS: { NAME: { min: 1, max: 120 }, // Length @@ -517,38 +344,38 @@ const VIDEO_LICENCES = { const VIDEO_LANGUAGES = buildLanguages() const VIDEO_PRIVACIES = { - [VideoPrivacy.PUBLIC]: 'Public', - [VideoPrivacy.UNLISTED]: 'Unlisted', - [VideoPrivacy.PRIVATE]: 'Private' + [ VideoPrivacy.PUBLIC ]: 'Public', + [ VideoPrivacy.UNLISTED ]: 'Unlisted', + [ VideoPrivacy.PRIVATE ]: 'Private' } const VIDEO_STATES = { - [VideoState.PUBLISHED]: 'Published', - [VideoState.TO_TRANSCODE]: 'To transcode', - [VideoState.TO_IMPORT]: 'To import' + [ VideoState.PUBLISHED ]: 'Published', + [ VideoState.TO_TRANSCODE ]: 'To transcode', + [ VideoState.TO_IMPORT ]: 'To import' } const VIDEO_IMPORT_STATES = { - [VideoImportState.FAILED]: 'Failed', - [VideoImportState.PENDING]: 'Pending', - [VideoImportState.SUCCESS]: 'Success' + [ VideoImportState.FAILED ]: 'Failed', + [ VideoImportState.PENDING ]: 'Pending', + [ VideoImportState.SUCCESS ]: 'Success' } const VIDEO_ABUSE_STATES = { - [VideoAbuseState.PENDING]: 'Pending', - [VideoAbuseState.REJECTED]: 'Rejected', - [VideoAbuseState.ACCEPTED]: 'Accepted' + [ VideoAbuseState.PENDING ]: 'Pending', + [ VideoAbuseState.REJECTED ]: 'Rejected', + [ VideoAbuseState.ACCEPTED ]: 'Accepted' } const VIDEO_PLAYLIST_PRIVACIES = { - [VideoPlaylistPrivacy.PUBLIC]: 'Public', - [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted', - [VideoPlaylistPrivacy.PRIVATE]: 'Private' + [ VideoPlaylistPrivacy.PUBLIC ]: 'Public', + [ VideoPlaylistPrivacy.UNLISTED ]: 'Unlisted', + [ VideoPlaylistPrivacy.PRIVATE ]: 'Private' } const VIDEO_PLAYLIST_TYPES = { - [VideoPlaylistType.REGULAR]: 'Regular', - [VideoPlaylistType.WATCH_LATER]: 'Watch later' + [ VideoPlaylistType.REGULAR ]: 'Regular', + [ VideoPlaylistType.WATCH_LATER ]: 'Watch later' } const MIMETYPES = { @@ -634,7 +461,7 @@ const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes -const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = { +const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = { DO_NOT_LIST: 'do_not_list', BLUR: 'blur', DISPLAY: 'display' @@ -765,14 +592,14 @@ if (isTestInstance() === true) { SCHEDULER_INTERVALS_MS.actorFollowScores = 1000 SCHEDULER_INTERVALS_MS.removeOldJobs = 10000 SCHEDULER_INTERVALS_MS.updateVideos = 5000 - REPEAT_JOBS['videos-views'] = { every: 5000 } + REPEAT_JOBS[ 'videos-views' ] = { every: 5000 } REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1 VIDEO_VIEW_LIFETIME = 1000 // 1 second CONTACT_FORM_LIFETIME = 1000 // 1 second - JOB_ATTEMPTS['email'] = 1 + JOB_ATTEMPTS[ 'email' ] = 1 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 1 @@ -783,9 +610,15 @@ if (isTestInstance() === true) { updateWebserverUrls() +registerConfigChangedHandler(() => { + updateWebserverUrls() + updateWebserverConfig() +}) + // --------------------------------------------------------------------------- export { + WEBSERVER, API_VERSION, HLS_REDUNDANCY_DIRECTORY, P2P_MEDIA_LOADER_PEER_VERSION, @@ -794,7 +627,6 @@ export { BCRYPT_SALT_SIZE, TRACKER_RATE_LIMITS, FILES_CACHE, - CONFIG, CONSTRAINTS_FIELDS, EMBED_SIZE, REDUNDANCY, @@ -857,17 +689,6 @@ export { // --------------------------------------------------------------------------- -function getLocalConfigFilePath () { - const configSources = config.util.getConfigSources() - if (configSources.length === 0) throw new Error('Invalid config source.') - - let filename = 'local' - if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}` - if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}` - - return join(dirname(configSources[ 0 ].name), filename + '.json') -} - function buildVideoMimetypeExt () { const data = { 'video/webm': '.webm', @@ -890,8 +711,12 @@ function buildVideoMimetypeExt () { } function updateWebserverUrls () { - CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT) - CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP) + WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT) + WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP) + WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME + WEBSERVER.WS = CONFIG.WEBSERVER.WS + WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME + WEBSERVER.PORT = CONFIG.WEBSERVER.PORT } function updateWebserverConfig () { @@ -907,20 +732,6 @@ function buildVideosExtname () { : [ '.mp4', '.ogv', '.webm' ] } -function buildVideosRedundancy (objs: any[]): VideosRedundancy[] { - if (!objs) return [] - - if (!Array.isArray(objs)) return objs - - return objs.map(obj => { - return Object.assign({}, obj, { - minLifetime: parseDuration(obj.min_lifetime), - size: bytes.parse(obj.size), - minViews: obj.min_views - }) - }) -} - function buildLanguages () { const iso639 = require('iso-639-3') @@ -953,42 +764,12 @@ function buildLanguages () { iso639 .filter(l => { return (l.iso6391 !== null && l.type === 'living') || - additionalLanguages[l.iso6393] === true + additionalLanguages[ l.iso6393 ] === true }) - .forEach(l => languages[l.iso6391 || l.iso6393] = l.name) + .forEach(l => languages[ l.iso6391 || l.iso6393 ] = l.name) // Override Occitan label - languages['oc'] = 'Occitan' + languages[ 'oc' ] = 'Occitan' return languages } - -export function reloadConfig () { - - function directory () { - if (process.env.NODE_CONFIG_DIR) { - return process.env.NODE_CONFIG_DIR - } - - return join(root(), 'config') - } - - function purge () { - for (const fileName in require.cache) { - if (-1 === fileName.indexOf(directory())) { - continue - } - - delete require.cache[fileName] - } - - delete require.cache[require.resolve('config')] - } - - purge() - - config = require('config') - - updateWebserverConfig() - updateWebserverUrls() -} diff --git a/server/initializers/database.ts b/server/initializers/database.ts index 541ebbecf..872a56220 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts @@ -21,7 +21,7 @@ import { VideoCommentModel } from '../models/video/video-comment' import { VideoFileModel } from '../models/video/video-file' import { VideoShareModel } from '../models/video/video-share' import { VideoTagModel } from '../models/video/video-tag' -import { CONFIG } from './constants' +import { CONFIG } from './config' import { ScheduleVideoUpdateModel } from '../models/video/schedule-video-update' import { VideoCaptionModel } from '../models/video/video-caption' import { VideoImportModel } from '../models/video/video-import' diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index 07af96b68..127449577 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -6,9 +6,10 @@ import { UserModel } from '../models/account/user' import { ApplicationModel } from '../models/application/application' import { OAuthClientModel } from '../models/oauth/oauth-client' import { applicationExist, clientsExist, usersExist } from './checker-after-init' -import { FILES_CACHE, CONFIG, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants' +import { FILES_CACHE, HLS_STREAMING_PLAYLIST_DIRECTORY, LAST_MIGRATION_VERSION } from './constants' import { sequelizeTypescript } from './database' -import { remove, ensureDir } from 'fs-extra' +import { ensureDir, remove } from 'fs-extra' +import { CONFIG } from './config' async function installApplication () { try { diff --git a/server/initializers/migrations/0075-video-resolutions.ts b/server/initializers/migrations/0075-video-resolutions.ts index 26a188e5e..e4f26cb77 100644 --- a/server/initializers/migrations/0075-video-resolutions.ts +++ b/server/initializers/migrations/0075-video-resolutions.ts @@ -1,6 +1,6 @@ import * as Sequelize from 'sequelize' import { join } from 'path' -import { CONFIG } from '../../initializers/constants' +import { CONFIG } from '../../initializers/config' import { getVideoFileResolution } from '../../helpers/ffmpeg-utils' import { readdir, rename } from 'fs-extra' diff --git a/server/initializers/migrations/0140-actor-url.ts b/server/initializers/migrations/0140-actor-url.ts index e64ee3487..020499391 100644 --- a/server/initializers/migrations/0140-actor-url.ts +++ b/server/initializers/migrations/0140-actor-url.ts @@ -1,13 +1,13 @@ import * as Sequelize from 'sequelize' -import { CONFIG } from '../constants' +import { WEBSERVER } from '../constants' async function up (utils: { transaction: Sequelize.Transaction, queryInterface: Sequelize.QueryInterface, sequelize: Sequelize.Sequelize }): Promise { - const toReplace = CONFIG.WEBSERVER.HOSTNAME + ':443' - const by = CONFIG.WEBSERVER.HOST + const toReplace = WEBSERVER.HOSTNAME + ':443' + const by = WEBSERVER.HOST const replacer = column => `replace("${column}", '${toReplace}', '${by}')` { diff --git a/server/initializers/migrations/0235-delete-some-video-indexes.ts b/server/initializers/migrations/0235-delete-some-video-indexes.ts index e362f240c..5964b0dc5 100644 --- a/server/initializers/migrations/0235-delete-some-video-indexes.ts +++ b/server/initializers/migrations/0235-delete-some-video-indexes.ts @@ -1,8 +1,4 @@ import * as Sequelize from 'sequelize' -import { createClient } from 'redis' -import { CONFIG } from '../constants' -import { JobQueue } from '../../lib/job-queue' -import { initDatabaseModels } from '../database' async function up (utils: { transaction: Sequelize.Transaction diff --git a/server/initializers/migrations/0240-drop-old-indexes.ts b/server/initializers/migrations/0240-drop-old-indexes.ts index ba961e3f9..39868fa2d 100644 --- a/server/initializers/migrations/0240-drop-old-indexes.ts +++ b/server/initializers/migrations/0240-drop-old-indexes.ts @@ -1,8 +1,4 @@ import * as Sequelize from 'sequelize' -import { createClient } from 'redis' -import { CONFIG } from '../constants' -import { JobQueue } from '../../lib/job-queue' -import { initDatabaseModels } from '../database' async function up (utils: { transaction: Sequelize.Transaction diff --git a/server/initializers/migrations/0345-video-playlists.ts b/server/initializers/migrations/0345-video-playlists.ts index 11670b11d..6953f5553 100644 --- a/server/initializers/migrations/0345-video-playlists.ts +++ b/server/initializers/migrations/0345-video-playlists.ts @@ -1,7 +1,7 @@ import * as Sequelize from 'sequelize' -import { CONFIG } from '../constants' import { VideoPlaylistPrivacy, VideoPlaylistType } from '../../../shared/models/videos' import * as uuidv4 from 'uuid/v4' +import { WEBSERVER } from '../constants' async function up (utils: { transaction: Sequelize.Transaction, @@ -57,7 +57,7 @@ CREATE TABLE IF NOT EXISTS "videoPlaylistElement" for (const username of usernames) { const uuid = uuidv4() - const baseUrl = CONFIG.WEBSERVER.URL + '/video-playlists/' + uuid + const baseUrl = WEBSERVER.URL + '/video-playlists/' + uuid const query = ` INSERT INTO "videoPlaylist" ("url", "uuid", "name", "privacy", "type", "ownerAccountId", "createdAt", "updatedAt") SELECT '${baseUrl}' AS "url", -- cgit v1.2.3