]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server.ts
Type models
[github/Chocobozzz/PeerTube.git] / server.ts
CommitLineData
e02643f3
C
1if ([ 'dev', 'test'].indexOf(process.env.NODE_ENV) !== -1) {
2 require('source-map-support').install()
3}
4
a030a9b2 5// ----------- Node modules -----------
65fcc311
C
6import bodyParser = require('body-parser')
7import express = require('express')
13ce1d01 8const expressValidator = require('express-validator')
65fcc311
C
9import http = require('http')
10import morgan = require('morgan')
11import path = require('path')
12import bittorrentTracker = require('bittorrent-tracker')
13import { Server as WebSocketServer } from 'ws'
14
15const TrackerServer = bittorrentTracker.Server
a030a9b2 16
9f540774
C
17process.title = 'peertube'
18
a030a9b2 19// Create our main app
13ce1d01 20const app = express()
a030a9b2 21
00057e85 22// ----------- Database -----------
65fcc311
C
23// Do not use barels because we don't want to load all modules here (we need to initialize database first)
24import { logger } from './server/helpers/logger'
25import { API_VERSION, CONFIG } from './server/initializers/constants'
feb4bdfd 26// Initialize database and models
e02643f3
C
27import { database as db } from './server/initializers/database'
28db.init(false, onDatabaseInitDone)
00057e85 29
69b0a27c 30// ----------- Checker -----------
65fcc311 31import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker'
69b0a27c 32
65fcc311 33const missed = checkMissedConfig()
b65c27aa
C
34if (missed.length !== 0) {
35 throw new Error('Miss some configurations keys : ' + missed)
36}
65fcc311 37checkFFmpeg(function (err) {
4e284e97
C
38 if (err) {
39 throw err
40 }
41})
b65c27aa 42
65fcc311 43const errorMessage = checkConfig()
b65c27aa
C
44if (errorMessage !== null) {
45 throw new Error(errorMessage)
69b0a27c
C
46}
47
00057e85 48// ----------- PeerTube modules -----------
65fcc311
C
49import { migrate, installApplication } from './server/initializers'
50import { JobScheduler, activateSchedulers } from './server/lib'
51import * as customValidators from './server/helpers/custom-validators'
52import { apiRouter, clientsRouter, staticRouter } from './server/controllers'
a030a9b2 53
a030a9b2
C
54// ----------- Command line -----------
55
56// ----------- App -----------
57
58// For the logger
e02643f3
C
59app.use(morgan('combined', {
60 stream: { write: logger.info }
61}))
a030a9b2 62// For body requests
def16d33 63app.use(bodyParser.json({ limit: '500kb' }))
a030a9b2
C
64app.use(bodyParser.urlencoded({ extended: false }))
65// Validate some params for the API
66app.use(expressValidator({
65fcc311 67 customValidators: customValidators
a030a9b2
C
68}))
69
70// ----------- Views, routes and static files -----------
71
79530164 72// API
65fcc311
C
73const apiRoute = '/api/' + API_VERSION
74app.use(apiRoute, apiRouter)
052937db 75
79530164 76// Client files
65fcc311 77app.use('/', clientsRouter)
cbe2f7c3 78
79530164 79// Static files
65fcc311 80app.use('/', staticRouter)
6a94a109 81
79530164 82// Always serve index client page (the client is a single page application, let it handle routing)
6f4e2522 83app.use('/*', function (req, res, next) {
e02643f3 84 res.sendFile(path.join(__dirname, '../client/dist/index.html'))
6f4e2522 85})
a030a9b2
C
86
87// ----------- Tracker -----------
88
13ce1d01 89const trackerServer = new TrackerServer({
a030a9b2
C
90 http: false,
91 udp: false,
92 ws: false,
93 dht: false
94})
95
96trackerServer.on('error', function (err) {
97 logger.error(err)
98})
99
100trackerServer.on('warning', function (err) {
101 logger.error(err)
102})
103
13ce1d01 104const server = http.createServer(app)
65fcc311 105const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
a030a9b2
C
106wss.on('connection', function (ws) {
107 trackerServer.onWebSocketConnection(ws)
108})
109
110// ----------- Errors -----------
111
112// Catch 404 and forward to error handler
113app.use(function (req, res, next) {
13ce1d01 114 const err = new Error('Not Found')
65fcc311 115 err['status'] = 404
a030a9b2
C
116 next(err)
117})
118
6f4e2522
C
119app.use(function (err, req, res, next) {
120 logger.error(err)
121 res.sendStatus(err.status || 500)
122})
a030a9b2 123
79530164
C
124// ----------- Run -----------
125
5804c0db 126function onDatabaseInitDone () {
65fcc311 127 const port = CONFIG.LISTEN.PORT
5804c0db 128 // Run the migration scripts if needed
65fcc311 129 migrate(function (err) {
00d6b0dd 130 if (err) throw err
a030a9b2 131
65fcc311 132 installApplication(function (err) {
5804c0db
C
133 if (err) throw err
134
135 // ----------- Make the server listening -----------
136 server.listen(port, function () {
137 // Activate the communication with friends
65fcc311 138 activateSchedulers()
052937db 139
227d02fe 140 // Activate job scheduler
65fcc311 141 JobScheduler.Instance.activate()
227d02fe 142
5804c0db 143 logger.info('Server listening on port %d', port)
65fcc311 144 logger.info('Webserver: %s', CONFIG.WEBSERVER.URL)
5804c0db 145 })
8c308c2b
C
146 })
147 })
5804c0db 148}