aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers
diff options
context:
space:
mode:
Diffstat (limited to 'server/initializers')
-rw-r--r--server/initializers/checker.ts2
-rw-r--r--server/initializers/constants.ts22
-rw-r--r--server/initializers/database.ts59
-rw-r--r--server/initializers/installer.ts9
-rw-r--r--server/initializers/migrator.ts8
5 files changed, 70 insertions, 30 deletions
diff --git a/server/initializers/checker.ts b/server/initializers/checker.ts
index 370dff2d4..0ee01b0e3 100644
--- a/server/initializers/checker.ts
+++ b/server/initializers/checker.ts
@@ -1,6 +1,6 @@
1import config = require('config') 1import config = require('config')
2 2
3const db = require('./database') 3import { database as db } from './database'
4import { CONFIG } from './constants' 4import { CONFIG } from './constants'
5 5
6// Some checks on configuration files 6// Some checks on configuration files
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 6bdc261ad..1072d0de0 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -1,6 +1,9 @@
1import config = require('config') 1import config = require('config')
2import { join } from 'path' 2import { join } from 'path'
3 3
4// Do not use barrels, remain constants as independent as possible
5import { root, isTestInstance } from '../helpers/utils'
6
4// --------------------------------------------------------------------------- 7// ---------------------------------------------------------------------------
5 8
6const LAST_MIGRATION_VERSION = 50 9const LAST_MIGRATION_VERSION = 50
@@ -44,12 +47,12 @@ const CONFIG = {
44 PASSWORD: config.get<string>('database.password') 47 PASSWORD: config.get<string>('database.password')
45 }, 48 },
46 STORAGE: { 49 STORAGE: {
47 CERT_DIR: join(__dirname, '..', '..', config.get<string>('storage.certs')), 50 CERT_DIR: join(root(), config.get<string>('storage.certs')),
48 LOG_DIR: join(__dirname, '..', '..', config.get<string>('storage.logs')), 51 LOG_DIR: join(root(), config.get<string>('storage.logs')),
49 VIDEOS_DIR: join(__dirname, '..', '..', config.get<string>('storage.videos')), 52 VIDEOS_DIR: join(root(), config.get<string>('storage.videos')),
50 THUMBNAILS_DIR: join(__dirname, '..', '..', config.get<string>('storage.thumbnails')), 53 THUMBNAILS_DIR: join(root(), config.get<string>('storage.thumbnails')),
51 PREVIEWS_DIR: join(__dirname, '..', '..', config.get<string>('storage.previews')), 54 PREVIEWS_DIR: join(root(), config.get<string>('storage.previews')),
52 TORRENTS_DIR: join(__dirname, '..', '..', config.get<string>('storage.torrents')) 55 TORRENTS_DIR: join(root(), config.get<string>('storage.torrents'))
53 }, 56 },
54 WEBSERVER: { 57 WEBSERVER: {
55 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http', 58 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
@@ -334,10 +337,3 @@ export {
334 VIDEO_LICENCES, 337 VIDEO_LICENCES,
335 VIDEO_RATE_TYPES 338 VIDEO_RATE_TYPES
336} 339}
337
338// ---------------------------------------------------------------------------
339
340// This method exists in utils module but we want to let the constants module independent
341function isTestInstance () {
342 return (process.env.NODE_ENV === 'test')
343}
diff --git a/server/initializers/database.ts b/server/initializers/database.ts
index 753a06669..c89a8b23c 100644
--- a/server/initializers/database.ts
+++ b/server/initializers/database.ts
@@ -6,12 +6,52 @@ import { CONFIG } from './constants'
6// Do not use barrel, we need to load database first 6// Do not use barrel, we need to load database first
7import { logger } from '../helpers/logger' 7import { logger } from '../helpers/logger'
8import { isTestInstance } from '../helpers/utils' 8import { isTestInstance } from '../helpers/utils'
9import {
10 ApplicationModel,
11 AuthorModel,
12 JobModel,
13 OAuthClientModel,
14 OAuthTokenModel,
15 PodModel,
16 RequestModel,
17 RequestToPodModel,
18 RequestVideoEventModel,
19 RequestVideoQaduModel,
20 TagModel,
21 UserModel,
22 UserVideoRateModel,
23 VideoAbuseModel,
24 BlacklistedVideoModel,
25 VideoTagModel,
26 VideoModel
27} from '../models'
9 28
10const dbname = CONFIG.DATABASE.DBNAME 29const dbname = CONFIG.DATABASE.DBNAME
11const username = CONFIG.DATABASE.USERNAME 30const username = CONFIG.DATABASE.USERNAME
12const password = CONFIG.DATABASE.PASSWORD 31const password = CONFIG.DATABASE.PASSWORD
13 32
14const database: any = {} 33const database: {
34 sequelize?: Sequelize.Sequelize,
35 init?: (silent: any, callback: any) => void,
36
37 Application?: ApplicationModel,
38 Author?: AuthorModel,
39 Job?: JobModel,
40 OAuthClient?: OAuthClientModel,
41 OAuthToken?: OAuthTokenModel,
42 Pod?: PodModel,
43 RequestToPod?: RequestToPodModel,
44 RequestVideoEvent?: RequestVideoEventModel,
45 RequestVideoQadu?: RequestVideoQaduModel,
46 Request?: RequestModel,
47 Tag?: TagModel,
48 UserVideoRate?: UserVideoRateModel,
49 User?: UserModel,
50 VideoAbuse?: VideoAbuseModel,
51 BlacklistedVideo?: BlacklistedVideoModel,
52 VideoTag?: VideoTagModel,
53 Video?: VideoModel
54} = {}
15 55
16const sequelize = new Sequelize(dbname, username, password, { 56const sequelize = new Sequelize(dbname, username, password, {
17 dialect: 'postgres', 57 dialect: 'postgres',
@@ -32,12 +72,6 @@ const sequelize = new Sequelize(dbname, username, password, {
32database.sequelize = sequelize 72database.sequelize = sequelize
33 73
34database.init = function (silent, callback) { 74database.init = function (silent, callback) {
35 if (!callback) {
36 callback = silent
37 silent = false
38 }
39
40 if (!callback) callback = function () { /* empty */ }
41 75
42 const modelDirectory = join(__dirname, '..', 'models') 76 const modelDirectory = join(__dirname, '..', 'models')
43 fs.readdir(modelDirectory, function (err, files) { 77 fs.readdir(modelDirectory, function (err, files) {
@@ -45,7 +79,12 @@ database.init = function (silent, callback) {
45 79
46 files.filter(function (file) { 80 files.filter(function (file) {
47 // For all models but not utils.js 81 // For all models but not utils.js
48 if (file === 'utils.js') return false 82 if (
83 file === 'index.js' ||
84 file === 'utils.js' ||
85 file.endsWith('-interface.js') ||
86 file.endsWith('.js.map')
87 ) return false
49 88
50 return true 89 return true
51 }) 90 })
@@ -69,4 +108,6 @@ database.init = function (silent, callback) {
69 108
70// --------------------------------------------------------------------------- 109// ---------------------------------------------------------------------------
71 110
72module.exports = database 111export {
112 database
113}
diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts
index cd1404d48..467164107 100644
--- a/server/initializers/installer.ts
+++ b/server/initializers/installer.ts
@@ -4,10 +4,10 @@ import { each, series } from 'async'
4import mkdirp = require('mkdirp') 4import mkdirp = require('mkdirp')
5import passwordGenerator = require('password-generator') 5import passwordGenerator = require('password-generator')
6 6
7const db = require('./database') 7import { database as db } from './database'
8import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants' 8import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants'
9import { clientsExist, usersExist } from './checker' 9import { clientsExist, usersExist } from './checker'
10import { logger, createCertsIfNotExist } from '../helpers' 10import { logger, createCertsIfNotExist, root } from '../helpers'
11 11
12function installApplication (callback) { 12function installApplication (callback) {
13 series([ 13 series([
@@ -47,7 +47,7 @@ function createDirectoriesIfNotExist (callback) {
47 47
48 each(Object.keys(storages), function (key, callbackEach) { 48 each(Object.keys(storages), function (key, callbackEach) {
49 const dir = storages[key] 49 const dir = storages[key]
50 mkdirp(join(__dirname, '..', '..', dir), callbackEach) 50 mkdirp(join(root(), dir), callbackEach)
51 }, callback) 51 }, callback)
52} 52}
53 53
@@ -65,7 +65,8 @@ function createOAuthClientIfNotExist (callback) {
65 const client = db.OAuthClient.build({ 65 const client = db.OAuthClient.build({
66 clientId: id, 66 clientId: id,
67 clientSecret: secret, 67 clientSecret: secret,
68 grants: [ 'password', 'refresh_token' ] 68 grants: [ 'password', 'refresh_token' ],
69 redirectUris: null
69 }) 70 })
70 71
71 client.save().asCallback(function (err, createdClient) { 72 client.save().asCallback(function (err, createdClient) {
diff --git a/server/initializers/migrator.ts b/server/initializers/migrator.ts
index cfa3220e0..d42cb3ccc 100644
--- a/server/initializers/migrator.ts
+++ b/server/initializers/migrator.ts
@@ -1,10 +1,12 @@
1import { waterfall, eachSeries } from 'async' 1import { waterfall, eachSeries } from 'async'
2import fs = require('fs') 2import fs = require('fs')
3import path = require('path') 3import path = require('path')
4import * as Sequelize from 'sequelize'
4 5
5const db = require('./database') 6import { database as db } from './database'
6import { LAST_MIGRATION_VERSION } from './constants' 7import { LAST_MIGRATION_VERSION } from './constants'
7import { logger } from '../helpers' 8import { logger } from '../helpers'
9import { ApplicationInstance } from '../models'
8 10
9function migrate (finalCallback) { 11function migrate (finalCallback) {
10 waterfall([ 12 waterfall([
@@ -94,7 +96,7 @@ function getMigrationScripts (callback) {
94} 96}
95 97
96function executeMigration (actualVersion, entity, callback) { 98function executeMigration (actualVersion, entity, callback) {
97 const versionScript = parseInt(entity.version) 99 const versionScript = parseInt(entity.version, 10)
98 100
99 // Do not execute old migration scripts 101 // Do not execute old migration scripts
100 if (versionScript <= actualVersion) return callback(null) 102 if (versionScript <= actualVersion) return callback(null)
@@ -112,7 +114,7 @@ function executeMigration (actualVersion, entity, callback) {
112 transaction: t, 114 transaction: t,
113 queryInterface: db.sequelize.getQueryInterface(), 115 queryInterface: db.sequelize.getQueryInterface(),
114 sequelize: db.sequelize, 116 sequelize: db.sequelize,
115 Sequelize: db.Sequelize 117 Sequelize: Sequelize
116 } 118 }
117 migrationScript.up(options, function (err) { 119 migrationScript.up(options, function (err) {
118 if (err) { 120 if (err) {