]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server.ts
Adapt scripts to typescript
[github/Chocobozzz/PeerTube.git] / server.ts
... / ...
CommitLineData
1if ([ 'dev', 'test'].indexOf(process.env.NODE_ENV) !== -1) {
2 require('source-map-support').install()
3}
4
5// ----------- Node modules -----------
6import * as bodyParser from 'body-parser'
7import * as express from 'express'
8// FIXME: cannot import express-validator
9const expressValidator = require('express-validator')
10import * as http from 'http'
11import * as morgan from 'morgan'
12import * as path from 'path'
13import * as bittorrentTracker from 'bittorrent-tracker'
14import { Server as WebSocketServer } from 'ws'
15
16const TrackerServer = bittorrentTracker.Server
17
18process.title = 'peertube'
19
20// Create our main app
21const app = express()
22
23// ----------- Database -----------
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'
27// Initialize database and models
28import { database as db } from './server/initializers/database'
29db.init(false, onDatabaseInitDone)
30
31// ----------- Checker -----------
32import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker'
33
34const missed = checkMissedConfig()
35if (missed.length !== 0) {
36 throw new Error('Miss some configurations keys : ' + missed)
37}
38checkFFmpeg(function (err) {
39 if (err) {
40 throw err
41 }
42})
43
44const errorMessage = checkConfig()
45if (errorMessage !== null) {
46 throw new Error(errorMessage)
47}
48
49// ----------- PeerTube modules -----------
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'
54
55// ----------- Command line -----------
56
57// ----------- App -----------
58
59// For the logger
60app.use(morgan('combined', {
61 stream: { write: logger.info }
62}))
63// For body requests
64app.use(bodyParser.json({ limit: '500kb' }))
65app.use(bodyParser.urlencoded({ extended: false }))
66// Validate some params for the API
67app.use(expressValidator({
68 customValidators: customValidators
69}))
70
71// ----------- Views, routes and static files -----------
72
73// API
74const apiRoute = '/api/' + API_VERSION
75app.use(apiRoute, apiRouter)
76
77// Client files
78app.use('/', clientsRouter)
79
80// Static files
81app.use('/', staticRouter)
82
83// Always serve index client page (the client is a single page application, let it handle routing)
84app.use('/*', function (req, res, next) {
85 res.sendFile(path.join(__dirname, '../client/dist/index.html'))
86})
87
88// ----------- Tracker -----------
89
90const trackerServer = new TrackerServer({
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
105const server = http.createServer(app)
106const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
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) {
115 const err = new Error('Not Found')
116 err['status'] = 404
117 next(err)
118})
119
120app.use(function (err, req, res, next) {
121 logger.error(err)
122 res.sendStatus(err.status || 500)
123})
124
125// ----------- Run -----------
126
127function onDatabaseInitDone () {
128 const port = CONFIG.LISTEN.PORT
129 // Run the migration scripts if needed
130 migrate(function (err) {
131 if (err) throw err
132
133 installApplication(function (err) {
134 if (err) throw err
135
136 // ----------- Make the server listening -----------
137 server.listen(port, function () {
138 // Activate the communication with friends
139 activateSchedulers()
140
141 // Activate job scheduler
142 JobScheduler.Instance.activate()
143
144 logger.info('Server listening on port %d', port)
145 logger.info('Webserver: %s', CONFIG.WEBSERVER.URL)
146 })
147 })
148 })
149}