]>
Commit | Line | Data |
---|---|---|
e02643f3 C |
1 | if ([ 'dev', 'test'].indexOf(process.env.NODE_ENV) !== -1) { |
2 | require('source-map-support').install() | |
3 | } | |
4 | ||
a030a9b2 | 5 | // ----------- Node modules ----------- |
4d4e5cd4 C |
6 | import * as bodyParser from 'body-parser' |
7 | import * as express from 'express' | |
8 | // FIXME: cannot import express-validator | |
13ce1d01 | 9 | const expressValidator = require('express-validator') |
4d4e5cd4 C |
10 | import * as http from 'http' |
11 | import * as morgan from 'morgan' | |
12 | import * as path from 'path' | |
13 | import * as bittorrentTracker from 'bittorrent-tracker' | |
65fcc311 C |
14 | import { Server as WebSocketServer } from 'ws' |
15 | ||
16 | const TrackerServer = bittorrentTracker.Server | |
a030a9b2 | 17 | |
9f540774 C |
18 | process.title = 'peertube' |
19 | ||
a030a9b2 | 20 | // Create our main app |
13ce1d01 | 21 | const 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) |
25 | import { logger } from './server/helpers/logger' | |
26 | import { API_VERSION, CONFIG } from './server/initializers/constants' | |
feb4bdfd | 27 | // Initialize database and models |
e02643f3 C |
28 | import { database as db } from './server/initializers/database' |
29 | db.init(false, onDatabaseInitDone) | |
00057e85 | 30 | |
69b0a27c | 31 | // ----------- Checker ----------- |
65fcc311 | 32 | import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker' |
69b0a27c | 33 | |
65fcc311 | 34 | const missed = checkMissedConfig() |
b65c27aa C |
35 | if (missed.length !== 0) { |
36 | throw new Error('Miss some configurations keys : ' + missed) | |
37 | } | |
65fcc311 | 38 | checkFFmpeg(function (err) { |
4e284e97 C |
39 | if (err) { |
40 | throw err | |
41 | } | |
42 | }) | |
b65c27aa | 43 | |
65fcc311 | 44 | const errorMessage = checkConfig() |
b65c27aa C |
45 | if (errorMessage !== null) { |
46 | throw new Error(errorMessage) | |
69b0a27c C |
47 | } |
48 | ||
00057e85 | 49 | // ----------- PeerTube modules ----------- |
65fcc311 C |
50 | import { migrate, installApplication } from './server/initializers' |
51 | import { JobScheduler, activateSchedulers } from './server/lib' | |
52 | import * as customValidators from './server/helpers/custom-validators' | |
53 | import { apiRouter, clientsRouter, staticRouter } from './server/controllers' | |
a030a9b2 | 54 | |
a030a9b2 C |
55 | // ----------- Command line ----------- |
56 | ||
57 | // ----------- App ----------- | |
58 | ||
59 | // For the logger | |
e02643f3 C |
60 | app.use(morgan('combined', { |
61 | stream: { write: logger.info } | |
62 | })) | |
a030a9b2 | 63 | // For body requests |
def16d33 | 64 | app.use(bodyParser.json({ limit: '500kb' })) |
a030a9b2 C |
65 | app.use(bodyParser.urlencoded({ extended: false })) |
66 | // Validate some params for the API | |
67 | app.use(expressValidator({ | |
65fcc311 | 68 | customValidators: customValidators |
a030a9b2 C |
69 | })) |
70 | ||
71 | // ----------- Views, routes and static files ----------- | |
72 | ||
79530164 | 73 | // API |
65fcc311 C |
74 | const apiRoute = '/api/' + API_VERSION |
75 | app.use(apiRoute, apiRouter) | |
052937db | 76 | |
79530164 | 77 | // Client files |
65fcc311 | 78 | app.use('/', clientsRouter) |
cbe2f7c3 | 79 | |
79530164 | 80 | // Static files |
65fcc311 | 81 | app.use('/', staticRouter) |
6a94a109 | 82 | |
79530164 | 83 | // Always serve index client page (the client is a single page application, let it handle routing) |
6f4e2522 | 84 | app.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 | 90 | const trackerServer = new TrackerServer({ |
a030a9b2 C |
91 | http: false, |
92 | udp: false, | |
93 | ws: false, | |
94 | dht: false | |
95 | }) | |
96 | ||
97 | trackerServer.on('error', function (err) { | |
98 | logger.error(err) | |
99 | }) | |
100 | ||
101 | trackerServer.on('warning', function (err) { | |
102 | logger.error(err) | |
103 | }) | |
104 | ||
13ce1d01 | 105 | const server = http.createServer(app) |
65fcc311 | 106 | const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) |
a030a9b2 C |
107 | wss.on('connection', function (ws) { |
108 | trackerServer.onWebSocketConnection(ws) | |
109 | }) | |
110 | ||
111 | // ----------- Errors ----------- | |
112 | ||
113 | // Catch 404 and forward to error handler | |
114 | app.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 |
120 | app.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 | 127 | function 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 | } |