diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-22 20:58:25 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-25 17:32:16 +0200 |
commit | e02643f32e4c97ca307f8fc5b69be79c40d70a3b (patch) | |
tree | b7f6269913cd5a0e4f26a9461a043deb0c168be0 /server/initializers | |
parent | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (diff) | |
download | PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.gz PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.tar.zst PeerTube-e02643f32e4c97ca307f8fc5b69be79c40d70a3b.zip |
Type models
Diffstat (limited to 'server/initializers')
-rw-r--r-- | server/initializers/checker.ts | 2 | ||||
-rw-r--r-- | server/initializers/constants.ts | 22 | ||||
-rw-r--r-- | server/initializers/database.ts | 59 | ||||
-rw-r--r-- | server/initializers/installer.ts | 9 | ||||
-rw-r--r-- | server/initializers/migrator.ts | 8 |
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 @@ | |||
1 | import config = require('config') | 1 | import config = require('config') |
2 | 2 | ||
3 | const db = require('./database') | 3 | import { database as db } from './database' |
4 | import { CONFIG } from './constants' | 4 | import { 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 @@ | |||
1 | import config = require('config') | 1 | import config = require('config') |
2 | import { join } from 'path' | 2 | import { join } from 'path' |
3 | 3 | ||
4 | // Do not use barrels, remain constants as independent as possible | ||
5 | import { root, isTestInstance } from '../helpers/utils' | ||
6 | |||
4 | // --------------------------------------------------------------------------- | 7 | // --------------------------------------------------------------------------- |
5 | 8 | ||
6 | const LAST_MIGRATION_VERSION = 50 | 9 | const 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 | ||
341 | function 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 |
7 | import { logger } from '../helpers/logger' | 7 | import { logger } from '../helpers/logger' |
8 | import { isTestInstance } from '../helpers/utils' | 8 | import { isTestInstance } from '../helpers/utils' |
9 | import { | ||
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 | ||
10 | const dbname = CONFIG.DATABASE.DBNAME | 29 | const dbname = CONFIG.DATABASE.DBNAME |
11 | const username = CONFIG.DATABASE.USERNAME | 30 | const username = CONFIG.DATABASE.USERNAME |
12 | const password = CONFIG.DATABASE.PASSWORD | 31 | const password = CONFIG.DATABASE.PASSWORD |
13 | 32 | ||
14 | const database: any = {} | 33 | const 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 | ||
16 | const sequelize = new Sequelize(dbname, username, password, { | 56 | const sequelize = new Sequelize(dbname, username, password, { |
17 | dialect: 'postgres', | 57 | dialect: 'postgres', |
@@ -32,12 +72,6 @@ const sequelize = new Sequelize(dbname, username, password, { | |||
32 | database.sequelize = sequelize | 72 | database.sequelize = sequelize |
33 | 73 | ||
34 | database.init = function (silent, callback) { | 74 | database.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 | ||
72 | module.exports = database | 111 | export { |
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' | |||
4 | import mkdirp = require('mkdirp') | 4 | import mkdirp = require('mkdirp') |
5 | import passwordGenerator = require('password-generator') | 5 | import passwordGenerator = require('password-generator') |
6 | 6 | ||
7 | const db = require('./database') | 7 | import { database as db } from './database' |
8 | import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants' | 8 | import { USER_ROLES, CONFIG, LAST_MIGRATION_VERSION } from './constants' |
9 | import { clientsExist, usersExist } from './checker' | 9 | import { clientsExist, usersExist } from './checker' |
10 | import { logger, createCertsIfNotExist } from '../helpers' | 10 | import { logger, createCertsIfNotExist, root } from '../helpers' |
11 | 11 | ||
12 | function installApplication (callback) { | 12 | function 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 @@ | |||
1 | import { waterfall, eachSeries } from 'async' | 1 | import { waterfall, eachSeries } from 'async' |
2 | import fs = require('fs') | 2 | import fs = require('fs') |
3 | import path = require('path') | 3 | import path = require('path') |
4 | import * as Sequelize from 'sequelize' | ||
4 | 5 | ||
5 | const db = require('./database') | 6 | import { database as db } from './database' |
6 | import { LAST_MIGRATION_VERSION } from './constants' | 7 | import { LAST_MIGRATION_VERSION } from './constants' |
7 | import { logger } from '../helpers' | 8 | import { logger } from '../helpers' |
9 | import { ApplicationInstance } from '../models' | ||
8 | 10 | ||
9 | function migrate (finalCallback) { | 11 | function migrate (finalCallback) { |
10 | waterfall([ | 12 | waterfall([ |
@@ -94,7 +96,7 @@ function getMigrationScripts (callback) { | |||
94 | } | 96 | } |
95 | 97 | ||
96 | function executeMigration (actualVersion, entity, callback) { | 98 | function 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) { |