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