]>
Commit | Line | Data |
---|---|---|
1 | import { IConfig } from 'config' | |
2 | import { dirname, join } from 'path' | |
3 | import { VideosRedundancyStrategy } from '../../shared/models' | |
4 | // Do not use barrels, remain constants as independent as possible | |
5 | import { buildPath, parseBytes, parseDurationToMs, root } from '../helpers/core-utils' | |
6 | import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type' | |
7 | import * as bytes from 'bytes' | |
8 | import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type' | |
9 | ||
10 | // Use a variable to reload the configuration if we need | |
11 | let config: IConfig = require('config') | |
12 | ||
13 | const configChangedHandlers: Function[] = [] | |
14 | ||
15 | const CONFIG = { | |
16 | CUSTOM_FILE: getLocalConfigFilePath(), | |
17 | LISTEN: { | |
18 | PORT: config.get<number>('listen.port'), | |
19 | HOSTNAME: config.get<string>('listen.hostname') | |
20 | }, | |
21 | DATABASE: { | |
22 | DBNAME: 'peertube' + config.get<string>('database.suffix'), | |
23 | HOSTNAME: config.get<string>('database.hostname'), | |
24 | PORT: config.get<number>('database.port'), | |
25 | USERNAME: config.get<string>('database.username'), | |
26 | PASSWORD: config.get<string>('database.password'), | |
27 | POOL: { | |
28 | MAX: config.get<number>('database.pool.max') | |
29 | } | |
30 | }, | |
31 | REDIS: { | |
32 | HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null, | |
33 | PORT: config.has('redis.port') ? config.get<number>('redis.port') : null, | |
34 | SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null, | |
35 | AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null, | |
36 | DB: config.has('redis.db') ? config.get<number>('redis.db') : null | |
37 | }, | |
38 | SMTP: { | |
39 | TRANSPORT: config.has('smtp.transport') ? config.get<string>('smtp.transport') : 'smtp', | |
40 | SENDMAIL: config.has('smtp.sendmail') ? config.get<string>('smtp.sendmail') : null, | |
41 | HOSTNAME: config.get<string>('smtp.hostname'), | |
42 | PORT: config.get<number>('smtp.port'), | |
43 | USERNAME: config.get<string>('smtp.username'), | |
44 | PASSWORD: config.get<string>('smtp.password'), | |
45 | TLS: config.get<boolean>('smtp.tls'), | |
46 | DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'), | |
47 | CA_FILE: config.get<string>('smtp.ca_file'), | |
48 | FROM_ADDRESS: config.get<string>('smtp.from_address') | |
49 | }, | |
50 | EMAIL: { | |
51 | BODY: { | |
52 | SIGNATURE: config.get<string>('email.body.signature') | |
53 | }, | |
54 | SUBJECT: { | |
55 | PREFIX: config.get<string>('email.subject.prefix') + ' ' | |
56 | } | |
57 | }, | |
58 | STORAGE: { | |
59 | TMP_DIR: buildPath(config.get<string>('storage.tmp')), | |
60 | AVATARS_DIR: buildPath(config.get<string>('storage.avatars')), | |
61 | LOG_DIR: buildPath(config.get<string>('storage.logs')), | |
62 | VIDEOS_DIR: buildPath(config.get<string>('storage.videos')), | |
63 | STREAMING_PLAYLISTS_DIR: buildPath(config.get<string>('storage.streaming_playlists')), | |
64 | REDUNDANCY_DIR: buildPath(config.get<string>('storage.redundancy')), | |
65 | THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')), | |
66 | PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')), | |
67 | CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')), | |
68 | TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')), | |
69 | CACHE_DIR: buildPath(config.get<string>('storage.cache')), | |
70 | PLUGINS_DIR: buildPath(config.get<string>('storage.plugins')) | |
71 | }, | |
72 | WEBSERVER: { | |
73 | SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http', | |
74 | WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws', | |
75 | HOSTNAME: config.get<string>('webserver.hostname'), | |
76 | PORT: config.get<number>('webserver.port') | |
77 | }, | |
78 | RATES_LIMIT: { | |
79 | API: { | |
80 | WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.api.window')), | |
81 | MAX: config.get<number>('rates_limit.api.max') | |
82 | }, | |
83 | SIGNUP: { | |
84 | WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.signup.window')), | |
85 | MAX: config.get<number>('rates_limit.signup.max') | |
86 | }, | |
87 | LOGIN: { | |
88 | WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.login.window')), | |
89 | MAX: config.get<number>('rates_limit.login.max') | |
90 | }, | |
91 | ASK_SEND_EMAIL: { | |
92 | WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.ask_send_email.window')), | |
93 | MAX: config.get<number>('rates_limit.ask_send_email.max') | |
94 | } | |
95 | }, | |
96 | TRUST_PROXY: config.get<string[]>('trust_proxy'), | |
97 | LOG: { | |
98 | LEVEL: config.get<string>('log.level'), | |
99 | ROTATION: { | |
100 | ENABLED: config.get<boolean>('log.rotation.enabled'), | |
101 | MAX_FILE_SIZE: bytes.parse(config.get<string>('log.rotation.maxFileSize')), | |
102 | MAX_FILES: config.get<number>('log.rotation.maxFiles') | |
103 | }, | |
104 | ANONYMIZE_IP: config.get<boolean>('log.anonymizeIP') | |
105 | }, | |
106 | SEARCH: { | |
107 | REMOTE_URI: { | |
108 | USERS: config.get<boolean>('search.remote_uri.users'), | |
109 | ANONYMOUS: config.get<boolean>('search.remote_uri.anonymous') | |
110 | } | |
111 | }, | |
112 | TRENDING: { | |
113 | VIDEOS: { | |
114 | INTERVAL_DAYS: config.get<number>('trending.videos.interval_days') | |
115 | } | |
116 | }, | |
117 | REDUNDANCY: { | |
118 | VIDEOS: { | |
119 | CHECK_INTERVAL: parseDurationToMs(config.get<string>('redundancy.videos.check_interval')), | |
120 | STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies')) | |
121 | } | |
122 | }, | |
123 | REMOTE_REDUNDANCY: { | |
124 | VIDEOS: { | |
125 | ACCEPT_FROM: config.get<VideoRedundancyConfigFilter>('remote_redundancy.videos.accept_from') | |
126 | } | |
127 | }, | |
128 | CSP: { | |
129 | ENABLED: config.get<boolean>('csp.enabled'), | |
130 | REPORT_ONLY: config.get<boolean>('csp.report_only'), | |
131 | REPORT_URI: config.get<boolean>('csp.report_uri') | |
132 | }, | |
133 | TRACKER: { | |
134 | ENABLED: config.get<boolean>('tracker.enabled'), | |
135 | PRIVATE: config.get<boolean>('tracker.private'), | |
136 | REJECT_TOO_MANY_ANNOUNCES: config.get<boolean>('tracker.reject_too_many_announces') | |
137 | }, | |
138 | HISTORY: { | |
139 | VIDEOS: { | |
140 | MAX_AGE: parseDurationToMs(config.get('history.videos.max_age')) | |
141 | } | |
142 | }, | |
143 | VIEWS: { | |
144 | VIDEOS: { | |
145 | REMOTE: { | |
146 | MAX_AGE: parseDurationToMs(config.get('views.videos.remote.max_age')) | |
147 | } | |
148 | } | |
149 | }, | |
150 | PLUGINS: { | |
151 | INDEX: { | |
152 | ENABLED: config.get<boolean>('plugins.index.enabled'), | |
153 | CHECK_LATEST_VERSIONS_INTERVAL: parseDurationToMs(config.get<string>('plugins.index.check_latest_versions_interval')), | |
154 | URL: config.get<string>('plugins.index.url') | |
155 | } | |
156 | }, | |
157 | ADMIN: { | |
158 | get EMAIL () { return config.get<string>('admin.email') } | |
159 | }, | |
160 | CONTACT_FORM: { | |
161 | get ENABLED () { return config.get<boolean>('contact_form.enabled') } | |
162 | }, | |
163 | SIGNUP: { | |
164 | get ENABLED () { return config.get<boolean>('signup.enabled') }, | |
165 | get LIMIT () { return config.get<number>('signup.limit') }, | |
166 | get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') }, | |
167 | FILTERS: { | |
168 | CIDR: { | |
169 | get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') }, | |
170 | get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') } | |
171 | } | |
172 | } | |
173 | }, | |
174 | USER: { | |
175 | get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) }, | |
176 | get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) } | |
177 | }, | |
178 | TRANSCODING: { | |
179 | get ENABLED () { return config.get<boolean>('transcoding.enabled') }, | |
180 | get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') }, | |
181 | get ALLOW_AUDIO_FILES () { return config.get<boolean>('transcoding.allow_audio_files') }, | |
182 | get THREADS () { return config.get<number>('transcoding.threads') }, | |
183 | RESOLUTIONS: { | |
184 | get '0p' () { return config.get<boolean>('transcoding.resolutions.0p') }, | |
185 | get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') }, | |
186 | get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') }, | |
187 | get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') }, | |
188 | get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') }, | |
189 | get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }, | |
190 | get '2160p' () { return config.get<boolean>('transcoding.resolutions.2160p') } | |
191 | }, | |
192 | HLS: { | |
193 | get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') } | |
194 | }, | |
195 | WEBTORRENT: { | |
196 | get ENABLED () { return config.get<boolean>('transcoding.webtorrent.enabled') } | |
197 | } | |
198 | }, | |
199 | IMPORT: { | |
200 | VIDEOS: { | |
201 | HTTP: { | |
202 | get ENABLED () { return config.get<boolean>('import.videos.http.enabled') }, | |
203 | PROXY: { | |
204 | get ENABLED () { return config.get<boolean>('import.videos.http.proxy.enabled') }, | |
205 | get URL () { return config.get<string>('import.videos.http.proxy.url') } | |
206 | } | |
207 | }, | |
208 | TORRENT: { | |
209 | get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') } | |
210 | } | |
211 | } | |
212 | }, | |
213 | AUTO_BLACKLIST: { | |
214 | VIDEOS: { | |
215 | OF_USERS: { | |
216 | get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') } | |
217 | } | |
218 | } | |
219 | }, | |
220 | CACHE: { | |
221 | PREVIEWS: { | |
222 | get SIZE () { return config.get<number>('cache.previews.size') } | |
223 | }, | |
224 | VIDEO_CAPTIONS: { | |
225 | get SIZE () { return config.get<number>('cache.captions.size') } | |
226 | } | |
227 | }, | |
228 | INSTANCE: { | |
229 | get NAME () { return config.get<string>('instance.name') }, | |
230 | get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') }, | |
231 | get DESCRIPTION () { return config.get<string>('instance.description') }, | |
232 | get TERMS () { return config.get<string>('instance.terms') }, | |
233 | get CODE_OF_CONDUCT () { return config.get<string>('instance.code_of_conduct') }, | |
234 | ||
235 | get CREATION_REASON () { return config.get<string>('instance.creation_reason') }, | |
236 | ||
237 | get MODERATION_INFORMATION () { return config.get<string>('instance.moderation_information') }, | |
238 | get ADMINISTRATOR () { return config.get<string>('instance.administrator') }, | |
239 | get MAINTENANCE_LIFETIME () { return config.get<string>('instance.maintenance_lifetime') }, | |
240 | get BUSINESS_MODEL () { return config.get<string>('instance.business_model') }, | |
241 | get HARDWARE_INFORMATION () { return config.get<string>('instance.hardware_information') }, | |
242 | ||
243 | get LANGUAGES () { return config.get<string[]>('instance.languages') || [] }, | |
244 | get CATEGORIES () { return config.get<number[]>('instance.categories') || [] }, | |
245 | ||
246 | get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') }, | |
247 | get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') }, | |
248 | get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') }, | |
249 | CUSTOMIZATIONS: { | |
250 | get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') }, | |
251 | get CSS () { return config.get<string>('instance.customizations.css') } | |
252 | }, | |
253 | get ROBOTS () { return config.get<string>('instance.robots') }, | |
254 | get SECURITYTXT () { return config.get<string>('instance.securitytxt') }, | |
255 | get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') } | |
256 | }, | |
257 | SERVICES: { | |
258 | TWITTER: { | |
259 | get USERNAME () { return config.get<string>('services.twitter.username') }, | |
260 | get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') } | |
261 | } | |
262 | }, | |
263 | FOLLOWERS: { | |
264 | INSTANCE: { | |
265 | get ENABLED () { return config.get<boolean>('followers.instance.enabled') }, | |
266 | get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') } | |
267 | } | |
268 | }, | |
269 | FOLLOWINGS: { | |
270 | INSTANCE: { | |
271 | AUTO_FOLLOW_BACK: { | |
272 | get ENABLED () { | |
273 | return config.get<boolean>('followings.instance.auto_follow_back.enabled') | |
274 | } | |
275 | }, | |
276 | AUTO_FOLLOW_INDEX: { | |
277 | get ENABLED () { | |
278 | return config.get<boolean>('followings.instance.auto_follow_index.enabled') | |
279 | }, | |
280 | get INDEX_URL () { | |
281 | return config.get<string>('followings.instance.auto_follow_index.index_url') | |
282 | } | |
283 | } | |
284 | } | |
285 | }, | |
286 | THEME: { | |
287 | get DEFAULT () { return config.get<string>('theme.default') } | |
288 | } | |
289 | } | |
290 | ||
291 | function registerConfigChangedHandler (fun: Function) { | |
292 | configChangedHandlers.push(fun) | |
293 | } | |
294 | ||
295 | function isEmailEnabled () { | |
296 | return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT | |
297 | } | |
298 | ||
299 | // --------------------------------------------------------------------------- | |
300 | ||
301 | export { | |
302 | CONFIG, | |
303 | registerConfigChangedHandler, | |
304 | isEmailEnabled | |
305 | } | |
306 | ||
307 | // --------------------------------------------------------------------------- | |
308 | ||
309 | function getLocalConfigFilePath () { | |
310 | const configSources = config.util.getConfigSources() | |
311 | if (configSources.length === 0) throw new Error('Invalid config source.') | |
312 | ||
313 | let filename = 'local' | |
314 | if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}` | |
315 | if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}` | |
316 | ||
317 | return join(dirname(configSources[0].name), filename + '.json') | |
318 | } | |
319 | ||
320 | function buildVideosRedundancy (objs: any[]): VideosRedundancyStrategy[] { | |
321 | if (!objs) return [] | |
322 | ||
323 | if (!Array.isArray(objs)) return objs | |
324 | ||
325 | return objs.map(obj => { | |
326 | return Object.assign({}, obj, { | |
327 | minLifetime: parseDurationToMs(obj.min_lifetime), | |
328 | size: bytes.parse(obj.size), | |
329 | minViews: obj.min_views | |
330 | }) | |
331 | }) | |
332 | } | |
333 | ||
334 | export function reloadConfig () { | |
335 | ||
336 | function directory () { | |
337 | if (process.env.NODE_CONFIG_DIR) { | |
338 | return process.env.NODE_CONFIG_DIR | |
339 | } | |
340 | ||
341 | return join(root(), 'config') | |
342 | } | |
343 | ||
344 | function purge () { | |
345 | for (const fileName in require.cache) { | |
346 | if (fileName.includes(directory()) === false) { | |
347 | continue | |
348 | } | |
349 | ||
350 | delete require.cache[fileName] | |
351 | } | |
352 | ||
353 | delete require.cache[require.resolve('config')] | |
354 | } | |
355 | ||
356 | purge() | |
357 | ||
358 | config = require('config') | |
359 | ||
360 | for (const configChangedHandler of configChangedHandlers) { | |
361 | configChangedHandler() | |
362 | } | |
363 | } |