Server: use constants to get port configuration
[oweals/peertube.git] / server / initializers / constants.js
1 'use strict'
2
3 const config = require('config')
4 const maxBy = require('lodash/maxBy')
5 const path = require('path')
6
7 // ---------------------------------------------------------------------------
8
9 // API version
10 const API_VERSION = 'v1'
11
12 // Number of results by default for the pagination
13 const PAGINATION_COUNT_DEFAULT = 15
14
15 // Sortable columns per schema
16 const SEARCHABLE_COLUMNS = {
17   VIDEOS: [ 'name', 'magnetUri', 'podUrl', 'author', 'tags' ]
18 }
19
20 // Sortable columns per schema
21 const SORTABLE_COLUMNS = {
22   USERS: [ 'username', '-username', 'createdDate', '-createdDate' ],
23   VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ]
24 }
25
26 const OAUTH_LIFETIME = {
27   ACCESS_TOKEN: 3600 * 4, // 4 hours
28   REFRESH_TOKEN: 1209600 // 2 weeks
29 }
30
31 // ---------------------------------------------------------------------------
32
33 const CONFIG = {
34   LISTEN: {
35     PORT: config.get('listen.port')
36   },
37   DATABASE: {
38     DBNAME: 'peertube' + config.get('database.suffix'),
39     HOSTNAME: config.get('database.hostname'),
40     PORT: config.get('database.port')
41   },
42   STORAGE: {
43     CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')),
44     LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')),
45     VIDEOS_DIR: path.join(__dirname, '..', '..', config.get('storage.videos')),
46     THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails')),
47     TORRENTS_DIR: path.join(__dirname, '..', '..', config.get('storage.torrents'))
48   },
49   WEBSERVER: {
50     SCHEME: config.get('webserver.https') === true ? 'https' : 'http',
51     WS: config.get('webserver.https') === true ? 'wss' : 'ws',
52     HOSTNAME: config.get('webserver.hostname'),
53     PORT: config.get('webserver.port')
54   }
55 }
56 CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
57
58 // ---------------------------------------------------------------------------
59
60 const CONSTRAINTS_FIELDS = {
61   USERS: {
62     USERNAME: { min: 3, max: 20 }, // Length
63     PASSWORD: { min: 6, max: 255 } // Length
64   },
65   VIDEOS: {
66     NAME: { min: 3, max: 50 }, // Length
67     DESCRIPTION: { min: 3, max: 250 }, // Length
68     MAGNET_URI: { min: 10 }, // Length
69     DURATION: { min: 1, max: 7200 }, // Number
70     TAGS: { min: 1, max: 3 }, // Number of total tags
71     TAG: { min: 2, max: 10 }, // Length
72     THUMBNAIL: { min: 2, max: 30 },
73     THUMBNAIL64: { min: 0, max: 20000 } // Bytes
74   }
75 }
76
77 // ---------------------------------------------------------------------------
78
79 // Score a pod has when we create it as a friend
80 const FRIEND_SCORE = {
81   BASE: 100,
82   MAX: 1000
83 }
84
85 // ---------------------------------------------------------------------------
86
87 const MONGO_MIGRATION_SCRIPTS = [
88   {
89     script: '0005-create-application',
90     version: 5
91   },
92   {
93     script: '0010-users-password',
94     version: 10
95   },
96   {
97     script: '0015-admin-role',
98     version: 15
99   },
100   {
101     script: '0020-requests-endpoint',
102     version: 20
103   }
104 ]
105 const LAST_MONGO_SCHEMA_VERSION = (maxBy(MONGO_MIGRATION_SCRIPTS, 'version'))['version']
106
107 // ---------------------------------------------------------------------------
108
109 // Number of points we add/remove from a friend after a successful/bad request
110 const PODS_SCORE = {
111   MALUS: -10,
112   BONUS: 10
113 }
114
115 // Time to wait between requests to the friends (10 min)
116 let REQUESTS_INTERVAL = 600000
117
118 // Number of requests in parallel we can make
119 const REQUESTS_IN_PARALLEL = 10
120
121 // How many requests we put in request
122 const REQUESTS_LIMIT = 10
123
124 // Number of requests to retry for replay requests module
125 const RETRY_REQUESTS = 5
126
127 const REQUEST_ENDPOINTS = {
128   VIDEOS: 'videos'
129 }
130
131 // ---------------------------------------------------------------------------
132
133 // Password encryption
134 const BCRYPT_SALT_SIZE = 10
135
136 // Express static paths (router)
137 const STATIC_PATHS = {
138   THUMBNAILS: '/static/thumbnails',
139   TORRENTS: '/static/torrents/',
140   WEBSEED: '/static/webseed/'
141 }
142
143 // Cache control
144 let STATIC_MAX_AGE = '30d'
145
146 // Videos thumbnail size
147 const THUMBNAILS_SIZE = '200x110'
148
149 const USER_ROLES = {
150   ADMIN: 'admin',
151   USER: 'user'
152 }
153
154 // ---------------------------------------------------------------------------
155
156 // Special constants for a test instance
157 if (isTestInstance() === true) {
158   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
159   FRIEND_SCORE.BASE = 20
160   REQUESTS_INTERVAL = 10000
161   STATIC_MAX_AGE = 0
162 }
163
164 // ---------------------------------------------------------------------------
165
166 module.exports = {
167   API_VERSION,
168   BCRYPT_SALT_SIZE,
169   CONFIG,
170   CONSTRAINTS_FIELDS,
171   FRIEND_SCORE,
172   LAST_MONGO_SCHEMA_VERSION,
173   MONGO_MIGRATION_SCRIPTS,
174   OAUTH_LIFETIME,
175   PAGINATION_COUNT_DEFAULT,
176   PODS_SCORE,
177   REQUEST_ENDPOINTS,
178   REQUESTS_IN_PARALLEL,
179   REQUESTS_INTERVAL,
180   REQUESTS_LIMIT,
181   RETRY_REQUESTS,
182   SEARCHABLE_COLUMNS,
183   SORTABLE_COLUMNS,
184   STATIC_MAX_AGE,
185   STATIC_PATHS,
186   THUMBNAILS_SIZE,
187   USER_ROLES
188 }
189
190 // ---------------------------------------------------------------------------
191
192 function isTestInstance () {
193   return (process.env.NODE_ENV === 'test')
194 }