]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server.ts
Fix client compilation
[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
C
31import { database as db } from './server/initializers/database'
32db.init(false, 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}
65fcc311 41checkFFmpeg(function (err) {
4e284e97
C
42 if (err) {
43 throw err
44 }
45})
b65c27aa 46
65fcc311 47const errorMessage = checkConfig()
b65c27aa
C
48if (errorMessage !== null) {
49 throw new Error(errorMessage)
69b0a27c
C
50}
51
00057e85 52// ----------- PeerTube modules -----------
65fcc311
C
53import { migrate, installApplication } from './server/initializers'
54import { JobScheduler, activateSchedulers } from './server/lib'
55import * as customValidators from './server/helpers/custom-validators'
56import { apiRouter, clientsRouter, staticRouter } from './server/controllers'
a030a9b2 57
a030a9b2
C
58// ----------- Command line -----------
59
60// ----------- App -----------
61
1840c2f7
C
62// Enable cors for develop
63if (isTestInstance()) {
64 app.use(cors({
65 origin: 'http://localhost:3000',
66 credentials: true
67 }))
68}
69
a030a9b2 70// For the logger
e02643f3
C
71app.use(morgan('combined', {
72 stream: { write: logger.info }
73}))
a030a9b2 74// For body requests
def16d33 75app.use(bodyParser.json({ limit: '500kb' }))
a030a9b2
C
76app.use(bodyParser.urlencoded({ extended: false }))
77// Validate some params for the API
78app.use(expressValidator({
65fcc311 79 customValidators: customValidators
a030a9b2
C
80}))
81
82// ----------- Views, routes and static files -----------
83
79530164 84// API
65fcc311
C
85const apiRoute = '/api/' + API_VERSION
86app.use(apiRoute, apiRouter)
052937db 87
79530164 88// Client files
65fcc311 89app.use('/', clientsRouter)
cbe2f7c3 90
79530164 91// Static files
65fcc311 92app.use('/', staticRouter)
6a94a109 93
79530164 94// Always serve index client page (the client is a single page application, let it handle routing)
6f4e2522 95app.use('/*', function (req, res, next) {
e02643f3 96 res.sendFile(path.join(__dirname, '../client/dist/index.html'))
6f4e2522 97})
a030a9b2
C
98
99// ----------- Tracker -----------
100
13ce1d01 101const trackerServer = new TrackerServer({
a030a9b2
C
102 http: false,
103 udp: false,
104 ws: false,
105 dht: false
106})
107
108trackerServer.on('error', function (err) {
109 logger.error(err)
110})
111
112trackerServer.on('warning', function (err) {
113 logger.error(err)
114})
115
13ce1d01 116const server = http.createServer(app)
65fcc311 117const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
a030a9b2
C
118wss.on('connection', function (ws) {
119 trackerServer.onWebSocketConnection(ws)
120})
121
122// ----------- Errors -----------
123
124// Catch 404 and forward to error handler
125app.use(function (req, res, next) {
13ce1d01 126 const err = new Error('Not Found')
65fcc311 127 err['status'] = 404
a030a9b2
C
128 next(err)
129})
130
6f4e2522
C
131app.use(function (err, req, res, next) {
132 logger.error(err)
133 res.sendStatus(err.status || 500)
134})
a030a9b2 135
79530164
C
136// ----------- Run -----------
137
5804c0db 138function onDatabaseInitDone () {
65fcc311 139 const port = CONFIG.LISTEN.PORT
5804c0db 140 // Run the migration scripts if needed
65fcc311 141 migrate(function (err) {
00d6b0dd 142 if (err) throw err
a030a9b2 143
65fcc311 144 installApplication(function (err) {
5804c0db
C
145 if (err) throw err
146
147 // ----------- Make the server listening -----------
148 server.listen(port, function () {
149 // Activate the communication with friends
65fcc311 150 activateSchedulers()
052937db 151
227d02fe 152 // Activate job scheduler
65fcc311 153 JobScheduler.Instance.activate()
227d02fe 154
5804c0db 155 logger.info('Server listening on port %d', port)
65fcc311 156 logger.info('Webserver: %s', CONFIG.WEBSERVER.URL)
5804c0db 157 })
8c308c2b
C
158 })
159 })
5804c0db 160}