Newer
Older
import { isTestInstance } from './server/helpers/core-utils'
if (isTestInstance()) {
import * as bodyParser from 'body-parser'
import * as express from 'express'
// FIXME: cannot import express-validator
const expressValidator = require('express-validator')
import * as http from 'http'
import * as morgan from 'morgan'
import * as path from 'path'
import * as bittorrentTracker from 'bittorrent-tracker'
import { Server as WebSocketServer } from 'ws'
const TrackerServer = bittorrentTracker.Server
// ----------- Database -----------
// Do not use barrels because we don't want to load all modules here (we need to initialize database first)
import { API_VERSION, CONFIG, STATIC_PATHS } from './server/initializers/constants'
// ----------- Checker -----------
import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker'
if (missed.length !== 0) {
throw new Error('Miss some configurations keys : ' + missed)
}
if (errorMessage !== null) {
throw new Error(errorMessage)
// ----------- PeerTube modules -----------
import { migrate, installApplication } from './server/initializers'
import { JobScheduler, activateSchedulers, VideosPreviewCache } from './server/lib'
import * as customValidators from './server/helpers/custom-validators'
import { apiRouter, clientsRouter, staticRouter } from './server/controllers'
// ----------- Command line -----------
// ----------- App -----------
app.use((req, res, next) => {
// These routes have already cors
if (
req.path.indexOf(STATIC_PATHS.TORRENTS) === -1 &&
req.path.indexOf(STATIC_PATHS.WEBSEED) === -1
) {
return (cors({
origin: 'http://localhost:3000',
credentials: true
}))(req, res, next)
}
return next()
})
app.use(morgan('combined', {
stream: { write: logger.info }
}))
app.use(bodyParser.json({ limit: '500kb' }))
app.use(bodyParser.urlencoded({ extended: false }))
// Validate some params for the API
app.use(expressValidator({
}))
// ----------- Views, routes and static files -----------
const apiRoute = '/api/' + API_VERSION
app.use(apiRoute, apiRouter)
// Always serve index client page (the client is a single page application, let it handle routing)
res.sendFile(path.join(__dirname, '../client/dist/index.html'))
http: false,
udp: false,
ws: false,
dht: false
})
trackerServer.on('error', function (err) {
logger.error(err)
})
trackerServer.on('warning', function (err) {
logger.error(err)
})
const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
wss.on('connection', function (ws) {
trackerServer.onWebSocketConnection(ws)
})
// ----------- Errors -----------
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
app.use(function (err, req, res, next) {
logger.error(err)
res.sendStatus(err.status || 500)
})
// ----------- Run -----------
migrate()
.then(() => {
return installApplication()
})
.then(() => {
// ----------- Make the server listening -----------
server.listen(port, function () {
// Activate the communication with friends
// Activate job scheduler
VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE)
logger.info('Server listening on port %d', port)
logger.info('Web server: %s', CONFIG.WEBSERVER.URL)