diff options
Diffstat (limited to 'initializers')
-rw-r--r-- | initializers/checker.js | 45 | ||||
-rw-r--r-- | initializers/constants.js | 37 | ||||
-rw-r--r-- | initializers/database.js | 61 |
3 files changed, 143 insertions, 0 deletions
diff --git a/initializers/checker.js b/initializers/checker.js new file mode 100644 index 000000000..7a3a53616 --- /dev/null +++ b/initializers/checker.js | |||
@@ -0,0 +1,45 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var config = require('config') | ||
5 | var mkdirp = require('mkdirp') | ||
6 | |||
7 | var checker = {} | ||
8 | |||
9 | // Check the config files | ||
10 | checker.checkConfig = function () { | ||
11 | var required = [ 'listen.port', | ||
12 | 'webserver.https', 'webserver.host', 'webserver.port', | ||
13 | 'database.host', 'database.port', 'database.suffix', | ||
14 | 'storage.certs', 'storage.uploads', 'storage.logs', | ||
15 | 'network.friends' ] | ||
16 | var miss = [] | ||
17 | |||
18 | for (var key of required) { | ||
19 | if (!config.has(key)) { | ||
20 | miss.push(key) | ||
21 | } | ||
22 | } | ||
23 | |||
24 | return miss | ||
25 | } | ||
26 | |||
27 | // Create directories for the storage if it doesn't exist | ||
28 | checker.createDirectoriesIfNotExist = function () { | ||
29 | var storages = config.get('storage') | ||
30 | |||
31 | for (var key of Object.keys(storages)) { | ||
32 | var path = storages[key] | ||
33 | try { | ||
34 | mkdirp.sync(__dirname + '/../' + path) | ||
35 | } catch (error) { | ||
36 | // Do not use logger | ||
37 | console.error('Cannot create ' + path + ':' + error) | ||
38 | process.exit(0) | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | // ----------- Export ----------- | ||
44 | module.exports = checker | ||
45 | })() | ||
diff --git a/initializers/constants.js b/initializers/constants.js new file mode 100644 index 000000000..00b713961 --- /dev/null +++ b/initializers/constants.js | |||
@@ -0,0 +1,37 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var constants = {} | ||
5 | |||
6 | function isTestInstance () { | ||
7 | return (process.env.NODE_ENV === 'test') | ||
8 | } | ||
9 | |||
10 | // API version of our pod | ||
11 | constants.API_VERSION = 'v1' | ||
12 | |||
13 | // Score a pod has when we create it as a friend | ||
14 | constants.FRIEND_BASE_SCORE = 100 | ||
15 | |||
16 | // Time to wait between requests to the friends | ||
17 | constants.INTERVAL = 60000 | ||
18 | |||
19 | // Number of points we add/remove from a friend after a successful/bad request | ||
20 | constants.PODS_SCORE = { | ||
21 | MALUS: -10, | ||
22 | BONUS: 10 | ||
23 | } | ||
24 | |||
25 | // Number of retries we make for the make retry requests (to friends...) | ||
26 | constants.REQUEST_RETRIES = 10 | ||
27 | |||
28 | // Special constants for a test instance | ||
29 | if (isTestInstance() === true) { | ||
30 | constants.FRIEND_BASE_SCORE = 20 | ||
31 | constants.INTERVAL = 10000 | ||
32 | constants.REQUEST_RETRIES = 2 | ||
33 | } | ||
34 | |||
35 | // ----------- Export ----------- | ||
36 | module.exports = constants | ||
37 | })() | ||
diff --git a/initializers/database.js b/initializers/database.js new file mode 100644 index 000000000..4570d3739 --- /dev/null +++ b/initializers/database.js | |||
@@ -0,0 +1,61 @@ | |||
1 | ;(function () { | ||
2 | 'use strict' | ||
3 | |||
4 | var config = require('config') | ||
5 | var mongoose = require('mongoose') | ||
6 | |||
7 | var constants = require('./constants') | ||
8 | var logger = require('../helpers/logger') | ||
9 | |||
10 | var dbname = 'peertube' + config.get('database.suffix') | ||
11 | var host = config.get('database.host') | ||
12 | var port = config.get('database.port') | ||
13 | |||
14 | // ----------- Videos ----------- | ||
15 | var videosSchema = mongoose.Schema({ | ||
16 | name: String, | ||
17 | namePath: String, | ||
18 | description: String, | ||
19 | magnetUri: String, | ||
20 | podUrl: String | ||
21 | }) | ||
22 | |||
23 | var VideosDB = mongoose.model('videos', videosSchema) | ||
24 | |||
25 | // ----------- Pods ----------- | ||
26 | var podsSchema = mongoose.Schema({ | ||
27 | url: String, | ||
28 | publicKey: String, | ||
29 | score: { type: Number, max: constants.FRIEND_BASE_SCORE } | ||
30 | }) | ||
31 | |||
32 | var PodsDB = mongoose.model('pods', podsSchema) | ||
33 | |||
34 | // ----------- PoolRequests ----------- | ||
35 | var poolRequestsSchema = mongoose.Schema({ | ||
36 | type: String, | ||
37 | id: String, // Special id to find duplicates (video created we want to remove...) | ||
38 | request: mongoose.Schema.Types.Mixed | ||
39 | }) | ||
40 | |||
41 | var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) | ||
42 | |||
43 | // ----------- Connection ----------- | ||
44 | |||
45 | mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname) | ||
46 | mongoose.connection.on('error', function () { | ||
47 | logger.error('Mongodb connection error.') | ||
48 | process.exit(0) | ||
49 | }) | ||
50 | |||
51 | mongoose.connection.on('open', function () { | ||
52 | logger.info('Connected to mongodb.') | ||
53 | }) | ||
54 | |||
55 | // ----------- Export ----------- | ||
56 | module.exports = { | ||
57 | VideosDB: VideosDB, | ||
58 | PodsDB: PodsDB, | ||
59 | PoolRequestsDB: PoolRequestsDB | ||
60 | } | ||
61 | })() | ||