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