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