]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/config.ts
Add ability to delete old remote views
[github/Chocobozzz/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 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 TRUST_PROXY: config.get<string[]>('trust_proxy'),
67 LOG: {
68 LEVEL: config.get<string>('log.level')
69 },
70 SEARCH: {
71 REMOTE_URI: {
72 USERS: config.get<boolean>('search.remote_uri.users'),
73 ANONYMOUS: config.get<boolean>('search.remote_uri.anonymous')
74 }
75 },
76 TRENDING: {
77 VIDEOS: {
78 INTERVAL_DAYS: config.get<number>('trending.videos.interval_days')
79 }
80 },
81 REDUNDANCY: {
82 VIDEOS: {
83 CHECK_INTERVAL: parseDurationToMs(config.get<string>('redundancy.videos.check_interval')),
84 STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
85 }
86 },
87 CSP: {
88 ENABLED: config.get<boolean>('csp.enabled'),
89 REPORT_ONLY: config.get<boolean>('csp.report_only'),
90 REPORT_URI: config.get<boolean>('csp.report_uri')
91 },
92 TRACKER: {
93 ENABLED: config.get<boolean>('tracker.enabled'),
94 PRIVATE: config.get<boolean>('tracker.private'),
95 REJECT_TOO_MANY_ANNOUNCES: config.get<boolean>('tracker.reject_too_many_announces')
96 },
97 HISTORY: {
98 VIDEOS: {
99 MAX_AGE: parseDurationToMs(config.get('history.videos.max_age'))
100 }
101 },
102 VIEWS: {
103 VIDEOS: {
104 REMOTE: {
105 MAX_AGE: parseDurationToMs(config.get('views.videos.remote.max_age'))
106 }
107 }
108 },
109 ADMIN: {
110 get EMAIL () { return config.get<string>('admin.email') }
111 },
112 CONTACT_FORM: {
113 get ENABLED () { return config.get<boolean>('contact_form.enabled') }
114 },
115 SIGNUP: {
116 get ENABLED () { return config.get<boolean>('signup.enabled') },
117 get LIMIT () { return config.get<number>('signup.limit') },
118 get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
119 FILTERS: {
120 CIDR: {
121 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
122 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
123 }
124 }
125 },
126 USER: {
127 get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
128 get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
129 },
130 TRANSCODING: {
131 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
132 get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') },
133 get THREADS () { return config.get<number>('transcoding.threads') },
134 RESOLUTIONS: {
135 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
136 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
137 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
138 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
139 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
140 },
141 HLS: {
142 get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') }
143 }
144 },
145 IMPORT: {
146 VIDEOS: {
147 HTTP: {
148 get ENABLED () { return config.get<boolean>('import.videos.http.enabled') }
149 },
150 TORRENT: {
151 get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
152 }
153 }
154 },
155 AUTO_BLACKLIST: {
156 VIDEOS: {
157 OF_USERS: {
158 get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') }
159 }
160 }
161 },
162 CACHE: {
163 PREVIEWS: {
164 get SIZE () { return config.get<number>('cache.previews.size') }
165 },
166 VIDEO_CAPTIONS: {
167 get SIZE () { return config.get<number>('cache.captions.size') }
168 }
169 },
170 INSTANCE: {
171 get NAME () { return config.get<string>('instance.name') },
172 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
173 get DESCRIPTION () { return config.get<string>('instance.description') },
174 get TERMS () { return config.get<string>('instance.terms') },
175 get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') },
176 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
177 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
178 CUSTOMIZATIONS: {
179 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
180 get CSS () { return config.get<string>('instance.customizations.css') }
181 },
182 get ROBOTS () { return config.get<string>('instance.robots') },
183 get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
184 get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
185 },
186 SERVICES: {
187 TWITTER: {
188 get USERNAME () { return config.get<string>('services.twitter.username') },
189 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
190 }
191 },
192 FOLLOWERS: {
193 INSTANCE: {
194 get ENABLED () { return config.get<boolean>('followers.instance.enabled') },
195 get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') }
196 }
197 }
198 }
199
200 function registerConfigChangedHandler (fun: Function) {
201 configChangedHandlers.push(fun)
202 }
203
204 // ---------------------------------------------------------------------------
205
206 export {
207 CONFIG,
208 registerConfigChangedHandler
209 }
210
211 // ---------------------------------------------------------------------------
212
213 function getLocalConfigFilePath () {
214 const configSources = config.util.getConfigSources()
215 if (configSources.length === 0) throw new Error('Invalid config source.')
216
217 let filename = 'local'
218 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
219 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
220
221 return join(dirname(configSources[ 0 ].name), filename + '.json')
222 }
223
224 function buildVideosRedundancy (objs: any[]): VideosRedundancy[] {
225 if (!objs) return []
226
227 if (!Array.isArray(objs)) return objs
228
229 return objs.map(obj => {
230 return Object.assign({}, obj, {
231 minLifetime: parseDurationToMs(obj.min_lifetime),
232 size: bytes.parse(obj.size),
233 minViews: obj.min_views
234 })
235 })
236 }
237
238 export function reloadConfig () {
239
240 function directory () {
241 if (process.env.NODE_CONFIG_DIR) {
242 return process.env.NODE_CONFIG_DIR
243 }
244
245 return join(root(), 'config')
246 }
247
248 function purge () {
249 for (const fileName in require.cache) {
250 if (-1 === fileName.indexOf(directory())) {
251 continue
252 }
253
254 delete require.cache[fileName]
255 }
256
257 delete require.cache[require.resolve('config')]
258 }
259
260 purge()
261
262 config = require('config')
263
264 for (const configChangedHandler of configChangedHandlers) {
265 configChangedHandler()
266 }
267 }