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