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