1 // ----------- Node modules -----------
2 import bodyParser = require('body-parser')
3 import express = require('express')
4 const expressValidator = require('express-validator')
5 import http = require('http')
6 import morgan = require('morgan')
7 import path = require('path')
8 import bittorrentTracker = require('bittorrent-tracker')
9 import { Server as WebSocketServer } from 'ws'
11 const TrackerServer = bittorrentTracker.Server
13 process.title = 'peertube'
15 // Create our main app
18 // ----------- Database -----------
19 // Do not use barels because we don't want to load all modules here (we need to initialize database first)
20 import { logger } from './server/helpers/logger'
21 import { API_VERSION, CONFIG } from './server/initializers/constants'
22 // Initialize database and models
23 const db = require('./server/initializers/database')
24 db.init(onDatabaseInitDone)
26 // ----------- Checker -----------
27 import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker'
29 const missed = checkMissedConfig()
30 if (missed.length !== 0) {
31 throw new Error('Miss some configurations keys : ' + missed)
33 checkFFmpeg(function (err) {
39 const errorMessage = checkConfig()
40 if (errorMessage !== null) {
41 throw new Error(errorMessage)
44 // ----------- PeerTube modules -----------
45 import { migrate, installApplication } from './server/initializers'
46 import { JobScheduler, activateSchedulers } from './server/lib'
47 import * as customValidators from './server/helpers/custom-validators'
48 import { apiRouter, clientsRouter, staticRouter } from './server/controllers'
50 // ----------- Command line -----------
52 // ----------- App -----------
55 // app.use(morgan('combined', { stream: logger.stream }))
57 app.use(bodyParser.json({ limit: '500kb' }))
58 app.use(bodyParser.urlencoded({ extended: false }))
59 // Validate some params for the API
60 app.use(expressValidator({
61 customValidators: customValidators
64 // ----------- Views, routes and static files -----------
67 const apiRoute = '/api/' + API_VERSION
68 app.use(apiRoute, apiRouter)
71 app.use('/', clientsRouter)
74 app.use('/', staticRouter)
76 // Always serve index client page (the client is a single page application, let it handle routing)
77 app.use('/*', function (req, res, next) {
78 res.sendFile(path.join(__dirname, './client/dist/index.html'))
81 // ----------- Tracker -----------
83 const trackerServer = new TrackerServer({
90 trackerServer.on('error', function (err) {
94 trackerServer.on('warning', function (err) {
98 const server = http.createServer(app)
99 const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
100 wss.on('connection', function (ws) {
101 trackerServer.onWebSocketConnection(ws)
104 // ----------- Errors -----------
106 // Catch 404 and forward to error handler
107 app.use(function (req, res, next) {
108 const err = new Error('Not Found')
113 app.use(function (err, req, res, next) {
115 res.sendStatus(err.status || 500)
118 // ----------- Run -----------
120 function onDatabaseInitDone () {
121 const port = CONFIG.LISTEN.PORT
122 // Run the migration scripts if needed
123 migrate(function (err) {
126 installApplication(function (err) {
129 // ----------- Make the server listening -----------
130 server.listen(port, function () {
131 // Activate the communication with friends
134 // Activate job scheduler
135 JobScheduler.Instance.activate()
137 logger.info('Server listening on port %d', port)
138 logger.info('Webserver: %s', CONFIG.WEBSERVER.URL)