aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/config.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/initializers/config.ts')
-rw-r--r--server/initializers/config.ts277
1 files changed, 277 insertions, 0 deletions
diff --git a/server/initializers/config.ts b/server/initializers/config.ts
new file mode 100644
index 000000000..4f77e144d
--- /dev/null
+++ b/server/initializers/config.ts
@@ -0,0 +1,277 @@
1import { IConfig } from 'config'
2import { dirname, join } from 'path'
3import { VideosRedundancy } from '../../shared/models'
4// Do not use barrels, remain constants as independent as possible
5import { buildPath, parseBytes, parseDurationToMs, root } from '../helpers/core-utils'
6import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
7import * as bytes from 'bytes'
8
9// Use a variable to reload the configuration if we need
10let config: IConfig = require('config')
11
12const configChangedHandlers: Function[] = []
13
14const 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 STORAGE: {
48 TMP_DIR: buildPath(config.get<string>('storage.tmp')),
49 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
50 LOG_DIR: buildPath(config.get<string>('storage.logs')),
51 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
52 STREAMING_PLAYLISTS_DIR: buildPath(config.get<string>('storage.streaming_playlists')),
53 REDUNDANCY_DIR: buildPath(config.get<string>('storage.redundancy')),
54 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
55 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
56 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
57 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
58 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
59 },
60 WEBSERVER: {
61 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
62 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
63 HOSTNAME: config.get<string>('webserver.hostname'),
64 PORT: config.get<number>('webserver.port')
65 },
66 RATES_LIMIT: {
67 LOGIN: {
68 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.login.window')),
69 MAX: config.get<number>('rates_limit.login.max')
70 },
71 ASK_SEND_EMAIL: {
72 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.ask_send_email.window')),
73 MAX: config.get<number>('rates_limit.ask_send_email.max')
74 }
75 },
76 TRUST_PROXY: config.get<string[]>('trust_proxy'),
77 LOG: {
78 LEVEL: config.get<string>('log.level')
79 },
80 SEARCH: {
81 REMOTE_URI: {
82 USERS: config.get<boolean>('search.remote_uri.users'),
83 ANONYMOUS: config.get<boolean>('search.remote_uri.anonymous')
84 }
85 },
86 TRENDING: {
87 VIDEOS: {
88 INTERVAL_DAYS: config.get<number>('trending.videos.interval_days')
89 }
90 },
91 REDUNDANCY: {
92 VIDEOS: {
93 CHECK_INTERVAL: parseDurationToMs(config.get<string>('redundancy.videos.check_interval')),
94 STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
95 }
96 },
97 CSP: {
98 ENABLED: config.get<boolean>('csp.enabled'),
99 REPORT_ONLY: config.get<boolean>('csp.report_only'),
100 REPORT_URI: config.get<boolean>('csp.report_uri')
101 },
102 TRACKER: {
103 ENABLED: config.get<boolean>('tracker.enabled'),
104 PRIVATE: config.get<boolean>('tracker.private'),
105 REJECT_TOO_MANY_ANNOUNCES: config.get<boolean>('tracker.reject_too_many_announces')
106 },
107 HISTORY: {
108 VIDEOS: {
109 MAX_AGE: parseDurationToMs(config.get('history.videos.max_age'))
110 }
111 },
112 VIEWS: {
113 VIDEOS: {
114 REMOTE: {
115 MAX_AGE: parseDurationToMs(config.get('views.videos.remote.max_age'))
116 }
117 }
118 },
119 ADMIN: {
120 get EMAIL () { return config.get<string>('admin.email') }
121 },
122 CONTACT_FORM: {
123 get ENABLED () { return config.get<boolean>('contact_form.enabled') }
124 },
125 SIGNUP: {
126 get ENABLED () { return config.get<boolean>('signup.enabled') },
127 get LIMIT () { return config.get<number>('signup.limit') },
128 get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
129 FILTERS: {
130 CIDR: {
131 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
132 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
133 }
134 }
135 },
136 USER: {
137 get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
138 get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
139 },
140 TRANSCODING: {
141 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
142 get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') },
143 get THREADS () { return config.get<number>('transcoding.threads') },
144 RESOLUTIONS: {
145 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
146 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
147 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
148 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
149 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
150 },
151 HLS: {
152 get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') }
153 }
154 },
155 IMPORT: {
156 VIDEOS: {
157 HTTP: {
158 get ENABLED () { return config.get<boolean>('import.videos.http.enabled') }
159 },
160 TORRENT: {
161 get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
162 }
163 }
164 },
165 AUTO_BLACKLIST: {
166 VIDEOS: {
167 OF_USERS: {
168 get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') }
169 }
170 }
171 },
172 CACHE: {
173 PREVIEWS: {
174 get SIZE () { return config.get<number>('cache.previews.size') }
175 },
176 VIDEO_CAPTIONS: {
177 get SIZE () { return config.get<number>('cache.captions.size') }
178 }
179 },
180 INSTANCE: {
181 get NAME () { return config.get<string>('instance.name') },
182 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
183 get DESCRIPTION () { return config.get<string>('instance.description') },
184 get TERMS () { return config.get<string>('instance.terms') },
185 get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') },
186 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
187 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
188 CUSTOMIZATIONS: {
189 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
190 get CSS () { return config.get<string>('instance.customizations.css') }
191 },
192 get ROBOTS () { return config.get<string>('instance.robots') },
193 get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
194 get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
195 },
196 SERVICES: {
197 TWITTER: {
198 get USERNAME () { return config.get<string>('services.twitter.username') },
199 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
200 }
201 },
202 FOLLOWERS: {
203 INSTANCE: {
204 get ENABLED () { return config.get<boolean>('followers.instance.enabled') },
205 get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') }
206 }
207 }
208}
209
210function registerConfigChangedHandler (fun: Function) {
211 configChangedHandlers.push(fun)
212}
213
214// ---------------------------------------------------------------------------
215
216export {
217 CONFIG,
218 registerConfigChangedHandler
219}
220
221// ---------------------------------------------------------------------------
222
223function getLocalConfigFilePath () {
224 const configSources = config.util.getConfigSources()
225 if (configSources.length === 0) throw new Error('Invalid config source.')
226
227 let filename = 'local'
228 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
229 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
230
231 return join(dirname(configSources[ 0 ].name), filename + '.json')
232}
233
234function buildVideosRedundancy (objs: any[]): VideosRedundancy[] {
235 if (!objs) return []
236
237 if (!Array.isArray(objs)) return objs
238
239 return objs.map(obj => {
240 return Object.assign({}, obj, {
241 minLifetime: parseDurationToMs(obj.min_lifetime),
242 size: bytes.parse(obj.size),
243 minViews: obj.min_views
244 })
245 })
246}
247
248export function reloadConfig () {
249
250 function directory () {
251 if (process.env.NODE_CONFIG_DIR) {
252 return process.env.NODE_CONFIG_DIR
253 }
254
255 return join(root(), 'config')
256 }
257
258 function purge () {
259 for (const fileName in require.cache) {
260 if (-1 === fileName.indexOf(directory())) {
261 continue
262 }
263
264 delete require.cache[fileName]
265 }
266
267 delete require.cache[require.resolve('config')]
268 }
269
270 purge()
271
272 config = require('config')
273
274 for (const configChangedHandler of configChangedHandlers) {
275 configChangedHandler()
276 }
277}