]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server.ts
Move to promises
[github/Chocobozzz/PeerTube.git] / server.ts
index f5413b8e33fd7c5ce1ddf9e2f4cf4e776fc3decd..e7fa99c90d49f4965a1b315c3ba0438c156c1f58 100644 (file)
--- a/server.ts
+++ b/server.ts
@@ -1,15 +1,19 @@
-if ([ 'dev', 'test'].indexOf(process.env.NODE_ENV) !== -1) {
+import { isTestInstance } from './server/helpers/core-utils'
+
+if (isTestInstance()) {
   require('source-map-support').install()
 }
 
 // ----------- Node modules -----------
-import bodyParser = require('body-parser')
-import express = require('express')
+import * as bodyParser from 'body-parser'
+import * as express from 'express'
+// FIXME: cannot import express-validator
 const expressValidator = require('express-validator')
-import http = require('http')
-import morgan = require('morgan')
-import path = require('path')
-import bittorrentTracker = require('bittorrent-tracker')
+import * as http from 'http'
+import * as morgan from 'morgan'
+import * as path from 'path'
+import * as bittorrentTracker from 'bittorrent-tracker'
+import * as cors from 'cors'
 import { Server as WebSocketServer } from 'ws'
 
 const TrackerServer = bittorrentTracker.Server
@@ -25,7 +29,7 @@ import { logger } from './server/helpers/logger'
 import { API_VERSION, CONFIG } from './server/initializers/constants'
 // Initialize database and models
 import { database as db } from './server/initializers/database'
-db.init(false, onDatabaseInitDone)
+db.init(false).then(() => onDatabaseInitDone())
 
 // ----------- Checker -----------
 import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker'
@@ -34,11 +38,7 @@ const missed = checkMissedConfig()
 if (missed.length !== 0) {
   throw new Error('Miss some configurations keys : ' + missed)
 }
-checkFFmpeg(function (err) {
-  if (err) {
-    throw err
-  }
-})
+checkFFmpeg()
 
 const errorMessage = checkConfig()
 if (errorMessage !== null) {
@@ -55,6 +55,14 @@ import { apiRouter, clientsRouter, staticRouter } from './server/controllers'
 
 // ----------- App -----------
 
+// Enable cors for develop
+if (isTestInstance()) {
+  app.use(cors({
+    origin: 'http://localhost:3000',
+    credentials: true
+  }))
+}
+
 // For the logger
 app.use(morgan('combined', {
   stream: { write: logger.info }
@@ -126,12 +134,11 @@ app.use(function (err, req, res, next) {
 function onDatabaseInitDone () {
   const port = CONFIG.LISTEN.PORT
     // Run the migration scripts if needed
-  migrate(function (err) {
-    if (err) throw err
-
-    installApplication(function (err) {
-      if (err) throw err
-
+  migrate()
+    .then(() => {
+      return installApplication()
+    })
+    .then(() => {
       // ----------- Make the server listening -----------
       server.listen(port, function () {
         // Activate the communication with friends
@@ -144,5 +151,4 @@ function onDatabaseInitDone () {
         logger.info('Webserver: %s', CONFIG.WEBSERVER.URL)
       })
     })
-  })
 }