aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers
diff options
context:
space:
mode:
Diffstat (limited to 'server/initializers')
-rw-r--r--server/initializers/checker.js8
-rw-r--r--server/initializers/constants.js19
-rw-r--r--server/initializers/database.js58
-rw-r--r--server/initializers/installer.js29
-rw-r--r--server/initializers/migrator.js12
5 files changed, 67 insertions, 59 deletions
diff --git a/server/initializers/checker.js b/server/initializers/checker.js
index aea013fa9..7b402de82 100644
--- a/server/initializers/checker.js
+++ b/server/initializers/checker.js
@@ -1,10 +1,8 @@
1'use strict' 1'use strict'
2 2
3const config = require('config') 3const config = require('config')
4const mongoose = require('mongoose')
5 4
6const Client = mongoose.model('OAuthClient') 5const db = require('./database')
7const User = mongoose.model('User')
8 6
9const checker = { 7const checker = {
10 checkConfig, 8 checkConfig,
@@ -44,7 +42,7 @@ function checkMissedConfig () {
44} 42}
45 43
46function clientsExist (callback) { 44function clientsExist (callback) {
47 Client.list(function (err, clients) { 45 db.OAuthClient.list(function (err, clients) {
48 if (err) return callback(err) 46 if (err) return callback(err)
49 47
50 return callback(null, clients.length !== 0) 48 return callback(null, clients.length !== 0)
@@ -52,7 +50,7 @@ function clientsExist (callback) {
52} 50}
53 51
54function usersExist (callback) { 52function usersExist (callback) {
55 User.countTotal(function (err, totalUsers) { 53 db.User.countTotal(function (err, totalUsers) {
56 if (err) return callback(err) 54 if (err) return callback(err)
57 55
58 return callback(null, totalUsers !== 0) 56 return callback(null, totalUsers !== 0)
diff --git a/server/initializers/constants.js b/server/initializers/constants.js
index 3ddf87454..1ad0c82a0 100644
--- a/server/initializers/constants.js
+++ b/server/initializers/constants.js
@@ -14,13 +14,13 @@ const PAGINATION_COUNT_DEFAULT = 15
14 14
15// Sortable columns per schema 15// Sortable columns per schema
16const SEARCHABLE_COLUMNS = { 16const SEARCHABLE_COLUMNS = {
17 VIDEOS: [ 'name', 'magnetUri', 'podHost', 'author', 'tags' ] 17 VIDEOS: [ 'name', 'magnetUri', 'host', 'author', 'tags' ]
18} 18}
19 19
20// Sortable columns per schema 20// Sortable columns per schema
21const SORTABLE_COLUMNS = { 21const SORTABLE_COLUMNS = {
22 USERS: [ 'username', '-username', 'createdDate', '-createdDate' ], 22 USERS: [ 'username', '-username', 'createdAt', '-createdAt' ],
23 VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdDate', '-createdDate' ] 23 VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdAt', '-createdAt' ]
24} 24}
25 25
26const OAUTH_LIFETIME = { 26const OAUTH_LIFETIME = {
@@ -67,9 +67,8 @@ const CONSTRAINTS_FIELDS = {
67 VIDEOS: { 67 VIDEOS: {
68 NAME: { min: 3, max: 50 }, // Length 68 NAME: { min: 3, max: 50 }, // Length
69 DESCRIPTION: { min: 3, max: 250 }, // Length 69 DESCRIPTION: { min: 3, max: 250 }, // Length
70 MAGNET: { 70 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
71 INFO_HASH: { min: 10, max: 50 } // Length 71 INFO_HASH: { min: 10, max: 50 }, // Length
72 },
73 DURATION: { min: 1, max: 7200 }, // Number 72 DURATION: { min: 1, max: 7200 }, // Number
74 TAGS: { min: 1, max: 3 }, // Number of total tags 73 TAGS: { min: 1, max: 3 }, // Number of total tags
75 TAG: { min: 2, max: 10 }, // Length 74 TAG: { min: 2, max: 10 }, // Length
@@ -88,7 +87,7 @@ const FRIEND_SCORE = {
88 87
89// --------------------------------------------------------------------------- 88// ---------------------------------------------------------------------------
90 89
91const MONGO_MIGRATION_SCRIPTS = [ 90const MIGRATION_SCRIPTS = [
92 { 91 {
93 script: '0005-create-application', 92 script: '0005-create-application',
94 version: 5 93 version: 5
@@ -122,7 +121,7 @@ const MONGO_MIGRATION_SCRIPTS = [
122 version: 40 121 version: 40
123 } 122 }
124] 123]
125const LAST_MONGO_SCHEMA_VERSION = (maxBy(MONGO_MIGRATION_SCRIPTS, 'version'))['version'] 124const LAST_SQL_SCHEMA_VERSION = (maxBy(MIGRATION_SCRIPTS, 'version'))['version']
126 125
127// --------------------------------------------------------------------------- 126// ---------------------------------------------------------------------------
128 127
@@ -198,8 +197,8 @@ module.exports = {
198 CONFIG, 197 CONFIG,
199 CONSTRAINTS_FIELDS, 198 CONSTRAINTS_FIELDS,
200 FRIEND_SCORE, 199 FRIEND_SCORE,
201 LAST_MONGO_SCHEMA_VERSION, 200 LAST_SQL_SCHEMA_VERSION,
202 MONGO_MIGRATION_SCRIPTS, 201 MIGRATION_SCRIPTS,
203 OAUTH_LIFETIME, 202 OAUTH_LIFETIME,
204 PAGINATION_COUNT_DEFAULT, 203 PAGINATION_COUNT_DEFAULT,
205 PODS_SCORE, 204 PODS_SCORE,
diff --git a/server/initializers/database.js b/server/initializers/database.js
index 0564e4e77..cc6f59b63 100644
--- a/server/initializers/database.js
+++ b/server/initializers/database.js
@@ -1,36 +1,46 @@
1'use strict' 1'use strict'
2 2
3const mongoose = require('mongoose') 3const fs = require('fs')
4const path = require('path')
5const Sequelize = require('sequelize')
4 6
5const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
6const logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
7 9
8// Bootstrap models 10const database = {}
9require('../models/application') 11
10require('../models/oauth-token') 12const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'peertube', {
11require('../models/user') 13 dialect: 'postgres',
12require('../models/oauth-client') 14 host: constants.CONFIG.DATABASE.HOSTNAME,
13require('../models/video') 15 port: constants.CONFIG.DATABASE.PORT
14// Request model needs Video model 16})
15require('../models/pods') 17
16// Request model needs Pod model 18const modelDirectory = path.join(__dirname, '..', 'models')
17require('../models/request') 19fs.readdir(modelDirectory, function (err, files) {
18 20 if (err) throw err
19const database = { 21
20 connect: connect 22 files.filter(function (file) {
21} 23 if (file === 'utils.js') return false
22 24
23function connect () { 25 return true
24 mongoose.Promise = global.Promise
25 mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOSTNAME + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME)
26 mongoose.connection.on('error', function () {
27 throw new Error('Mongodb connection error.')
28 }) 26 })
27 .forEach(function (file) {
28 const model = sequelize.import(path.join(modelDirectory, file))
29 29
30 mongoose.connection.on('open', function () { 30 database[model.name] = model
31 logger.info('Connected to mongodb.')
32 }) 31 })
33} 32
33 Object.keys(database).forEach(function (modelName) {
34 if ('associate' in database[modelName]) {
35 database[modelName].associate(database)
36 }
37 })
38
39 logger.info('Database is ready.')
40})
41
42database.sequelize = sequelize
43database.Sequelize = Sequelize
34 44
35// --------------------------------------------------------------------------- 45// ---------------------------------------------------------------------------
36 46
diff --git a/server/initializers/installer.js b/server/initializers/installer.js
index 1df300ba8..4823bc8c8 100644
--- a/server/initializers/installer.js
+++ b/server/initializers/installer.js
@@ -3,26 +3,27 @@
3const config = require('config') 3const config = require('config')
4const each = require('async/each') 4const each = require('async/each')
5const mkdirp = require('mkdirp') 5const mkdirp = require('mkdirp')
6const mongoose = require('mongoose')
7const passwordGenerator = require('password-generator') 6const passwordGenerator = require('password-generator')
8const path = require('path') 7const path = require('path')
9const series = require('async/series') 8const series = require('async/series')
10 9
11const checker = require('./checker') 10const checker = require('./checker')
12const constants = require('./constants') 11const constants = require('./constants')
12const db = require('./database')
13const logger = require('../helpers/logger') 13const logger = require('../helpers/logger')
14const peertubeCrypto = require('../helpers/peertube-crypto') 14const peertubeCrypto = require('../helpers/peertube-crypto')
15 15
16const Application = mongoose.model('Application')
17const Client = mongoose.model('OAuthClient')
18const User = mongoose.model('User')
19
20const installer = { 16const installer = {
21 installApplication 17 installApplication
22} 18}
23 19
24function installApplication (callback) { 20function installApplication (callback) {
25 series([ 21 series([
22 function createDatabase (callbackAsync) {
23 db.sequelize.sync().asCallback(callbackAsync)
24 // db.sequelize.sync({ force: true }).asCallback(callbackAsync)
25 },
26
26 function createDirectories (callbackAsync) { 27 function createDirectories (callbackAsync) {
27 createDirectoriesIfNotExist(callbackAsync) 28 createDirectoriesIfNotExist(callbackAsync)
28 }, 29 },
@@ -65,16 +66,18 @@ function createOAuthClientIfNotExist (callback) {
65 66
66 logger.info('Creating a default OAuth Client.') 67 logger.info('Creating a default OAuth Client.')
67 68
68 const secret = passwordGenerator(32, false) 69 const id = passwordGenerator(32, false, /[a-z0-9]/)
69 const client = new Client({ 70 const secret = passwordGenerator(32, false, /[a-zA-Z0-9]/)
71 const client = db.OAuthClient.build({
72 clientId: id,
70 clientSecret: secret, 73 clientSecret: secret,
71 grants: [ 'password', 'refresh_token' ] 74 grants: [ 'password', 'refresh_token' ]
72 }) 75 })
73 76
74 client.save(function (err, createdClient) { 77 client.save().asCallback(function (err, createdClient) {
75 if (err) return callback(err) 78 if (err) return callback(err)
76 79
77 logger.info('Client id: ' + createdClient._id) 80 logger.info('Client id: ' + createdClient.clientId)
78 logger.info('Client secret: ' + createdClient.clientSecret) 81 logger.info('Client secret: ' + createdClient.clientSecret)
79 82
80 return callback(null) 83 return callback(null)
@@ -106,21 +109,21 @@ function createOAuthAdminIfNotExist (callback) {
106 password = passwordGenerator(8, true) 109 password = passwordGenerator(8, true)
107 } 110 }
108 111
109 const user = new User({ 112 const user = db.User.build({
110 username, 113 username,
111 password, 114 password,
112 role 115 role
113 }) 116 })
114 117
115 user.save(function (err, createdUser) { 118 user.save().asCallback(function (err, createdUser) {
116 if (err) return callback(err) 119 if (err) return callback(err)
117 120
118 logger.info('Username: ' + username) 121 logger.info('Username: ' + username)
119 logger.info('User password: ' + password) 122 logger.info('User password: ' + password)
120 123
121 logger.info('Creating Application collection.') 124 logger.info('Creating Application collection.')
122 const application = new Application({ mongoSchemaVersion: constants.LAST_MONGO_SCHEMA_VERSION }) 125 const application = db.Application.build({ sqlSchemaVersion: constants.LAST_SQL_SCHEMA_VERSION })
123 application.save(callback) 126 application.save().asCallback(callback)
124 }) 127 })
125 }) 128 })
126} 129}
diff --git a/server/initializers/migrator.js b/server/initializers/migrator.js
index 6b31d994f..9e5350e60 100644
--- a/server/initializers/migrator.js
+++ b/server/initializers/migrator.js
@@ -1,24 +1,22 @@
1'use strict' 1'use strict'
2 2
3const eachSeries = require('async/eachSeries') 3const eachSeries = require('async/eachSeries')
4const mongoose = require('mongoose')
5const path = require('path') 4const path = require('path')
6 5
7const constants = require('./constants') 6const constants = require('./constants')
7const db = require('./database')
8const logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
9 9
10const Application = mongoose.model('Application')
11
12const migrator = { 10const migrator = {
13 migrate: migrate 11 migrate: migrate
14} 12}
15 13
16function migrate (callback) { 14function migrate (callback) {
17 Application.loadMongoSchemaVersion(function (err, actualVersion) { 15 db.Application.loadSqlSchemaVersion(function (err, actualVersion) {
18 if (err) return callback(err) 16 if (err) return callback(err)
19 17
20 // If there are a new mongo schemas 18 // If there are a new mongo schemas
21 if (!actualVersion || actualVersion < constants.LAST_MONGO_SCHEMA_VERSION) { 19 if (!actualVersion || actualVersion < constants.LAST_SQL_SCHEMA_VERSION) {
22 logger.info('Begin migrations.') 20 logger.info('Begin migrations.')
23 21
24 eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) { 22 eachSeries(constants.MONGO_MIGRATION_SCRIPTS, function (entity, callbackEach) {
@@ -36,12 +34,12 @@ function migrate (callback) {
36 if (err) return callbackEach(err) 34 if (err) return callbackEach(err)
37 35
38 // Update the new mongo version schema 36 // Update the new mongo version schema
39 Application.updateMongoSchemaVersion(versionScript, callbackEach) 37 db.Application.updateSqlSchemaVersion(versionScript, callbackEach)
40 }) 38 })
41 }, function (err) { 39 }, function (err) {
42 if (err) return callback(err) 40 if (err) return callback(err)
43 41
44 logger.info('Migrations finished. New mongo version schema: %s', constants.LAST_MONGO_SCHEMA_VERSION) 42 logger.info('Migrations finished. New SQL version schema: %s', constants.LAST_SQL_SCHEMA_VERSION)
45 return callback(null) 43 return callback(null)
46 }) 44 })
47 } else { 45 } else {