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