diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-11 11:33:44 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-11 13:45:39 +0200 |
commit | 6dd9de95dfa39bd5c1faed00d1dbd52cd112bae0 (patch) | |
tree | b47de7efb8c6c611c63a15a971c6125a278f547a /server/initializers/config.ts | |
parent | 2c3abc4fa796555eb7d25f416c4f41ab3e3ad8ca (diff) | |
download | PeerTube-6dd9de95dfa39bd5c1faed00d1dbd52cd112bae0.tar.gz PeerTube-6dd9de95dfa39bd5c1faed00d1dbd52cd112bae0.tar.zst PeerTube-6dd9de95dfa39bd5c1faed00d1dbd52cd112bae0.zip |
Move config in its own file
Diffstat (limited to 'server/initializers/config.ts')
-rw-r--r-- | server/initializers/config.ts | 255 |
1 files changed, 255 insertions, 0 deletions
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 @@ | |||
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, parseDuration, 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: parseDuration(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 | ADMIN: { | ||
98 | get EMAIL () { return config.get<string>('admin.email') } | ||
99 | }, | ||
100 | CONTACT_FORM: { | ||
101 | get ENABLED () { return config.get<boolean>('contact_form.enabled') } | ||
102 | }, | ||
103 | SIGNUP: { | ||
104 | get ENABLED () { return config.get<boolean>('signup.enabled') }, | ||
105 | get LIMIT () { return config.get<number>('signup.limit') }, | ||
106 | get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') }, | ||
107 | FILTERS: { | ||
108 | CIDR: { | ||
109 | get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') }, | ||
110 | get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') } | ||
111 | } | ||
112 | } | ||
113 | }, | ||
114 | USER: { | ||
115 | get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) }, | ||
116 | get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) } | ||
117 | }, | ||
118 | TRANSCODING: { | ||
119 | get ENABLED () { return config.get<boolean>('transcoding.enabled') }, | ||
120 | get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') }, | ||
121 | get THREADS () { return config.get<number>('transcoding.threads') }, | ||
122 | RESOLUTIONS: { | ||
123 | get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') }, | ||
124 | get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') }, | ||
125 | get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') }, | ||
126 | get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') }, | ||
127 | get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') } | ||
128 | }, | ||
129 | HLS: { | ||
130 | get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') } | ||
131 | } | ||
132 | }, | ||
133 | IMPORT: { | ||
134 | VIDEOS: { | ||
135 | HTTP: { | ||
136 | get ENABLED () { return config.get<boolean>('import.videos.http.enabled') } | ||
137 | }, | ||
138 | TORRENT: { | ||
139 | get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') } | ||
140 | } | ||
141 | } | ||
142 | }, | ||
143 | AUTO_BLACKLIST: { | ||
144 | VIDEOS: { | ||
145 | OF_USERS: { | ||
146 | get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') } | ||
147 | } | ||
148 | } | ||
149 | }, | ||
150 | CACHE: { | ||
151 | PREVIEWS: { | ||
152 | get SIZE () { return config.get<number>('cache.previews.size') } | ||
153 | }, | ||
154 | VIDEO_CAPTIONS: { | ||
155 | get SIZE () { return config.get<number>('cache.captions.size') } | ||
156 | } | ||
157 | }, | ||
158 | INSTANCE: { | ||
159 | get NAME () { return config.get<string>('instance.name') }, | ||
160 | get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') }, | ||
161 | get DESCRIPTION () { return config.get<string>('instance.description') }, | ||
162 | get TERMS () { return config.get<string>('instance.terms') }, | ||
163 | get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') }, | ||
164 | get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') }, | ||
165 | get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') }, | ||
166 | CUSTOMIZATIONS: { | ||
167 | get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') }, | ||
168 | get CSS () { return config.get<string>('instance.customizations.css') } | ||
169 | }, | ||
170 | get ROBOTS () { return config.get<string>('instance.robots') }, | ||
171 | get SECURITYTXT () { return config.get<string>('instance.securitytxt') }, | ||
172 | get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') } | ||
173 | }, | ||
174 | SERVICES: { | ||
175 | TWITTER: { | ||
176 | get USERNAME () { return config.get<string>('services.twitter.username') }, | ||
177 | get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') } | ||
178 | } | ||
179 | }, | ||
180 | FOLLOWERS: { | ||
181 | INSTANCE: { | ||
182 | get ENABLED () { return config.get<boolean>('followers.instance.enabled') }, | ||
183 | get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') } | ||
184 | } | ||
185 | } | ||
186 | } | ||
187 | |||
188 | function registerConfigChangedHandler (fun: Function) { | ||
189 | configChangedHandlers.push(fun) | ||
190 | } | ||
191 | |||
192 | // --------------------------------------------------------------------------- | ||
193 | |||
194 | export { | ||
195 | CONFIG, | ||
196 | registerConfigChangedHandler | ||
197 | } | ||
198 | |||
199 | // --------------------------------------------------------------------------- | ||
200 | |||
201 | function getLocalConfigFilePath () { | ||
202 | const configSources = config.util.getConfigSources() | ||
203 | if (configSources.length === 0) throw new Error('Invalid config source.') | ||
204 | |||
205 | let filename = 'local' | ||
206 | if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}` | ||
207 | if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}` | ||
208 | |||
209 | return join(dirname(configSources[ 0 ].name), filename + '.json') | ||
210 | } | ||
211 | |||
212 | function buildVideosRedundancy (objs: any[]): VideosRedundancy[] { | ||
213 | if (!objs) return [] | ||
214 | |||
215 | if (!Array.isArray(objs)) return objs | ||
216 | |||
217 | return objs.map(obj => { | ||
218 | return Object.assign({}, obj, { | ||
219 | minLifetime: parseDuration(obj.min_lifetime), | ||
220 | size: bytes.parse(obj.size), | ||
221 | minViews: obj.min_views | ||
222 | }) | ||
223 | }) | ||
224 | } | ||
225 | |||
226 | export function reloadConfig () { | ||
227 | |||
228 | function directory () { | ||
229 | if (process.env.NODE_CONFIG_DIR) { | ||
230 | return process.env.NODE_CONFIG_DIR | ||
231 | } | ||
232 | |||
233 | return join(root(), 'config') | ||
234 | } | ||
235 | |||
236 | function purge () { | ||
237 | for (const fileName in require.cache) { | ||
238 | if (-1 === fileName.indexOf(directory())) { | ||
239 | continue | ||
240 | } | ||
241 | |||
242 | delete require.cache[fileName] | ||
243 | } | ||
244 | |||
245 | delete require.cache[require.resolve('config')] | ||
246 | } | ||
247 | |||
248 | purge() | ||
249 | |||
250 | config = require('config') | ||
251 | |||
252 | for (const configChangedHandler of configChangedHandlers) { | ||
253 | configChangedHandler() | ||
254 | } | ||
255 | } | ||