]>
Commit | Line | Data |
---|---|---|
1 | if ([ 'dev', 'test'].indexOf(process.env.NODE_ENV) !== -1) { | |
2 | require('source-map-support').install() | |
3 | } | |
4 | ||
5 | // ----------- Node modules ----------- | |
6 | import * as bodyParser from 'body-parser' | |
7 | import * as express from 'express' | |
8 | // FIXME: cannot import express-validator | |
9 | const expressValidator = require('express-validator') | |
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' | |
14 | import { Server as WebSocketServer } from 'ws' | |
15 | ||
16 | const TrackerServer = bittorrentTracker.Server | |
17 | ||
18 | process.title = 'peertube' | |
19 | ||
20 | // Create our main app | |
21 | const app = express() | |
22 | ||
23 | // ----------- Database ----------- | |
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' | |
27 | // Initialize database and models | |
28 | import { database as db } from './server/initializers/database' | |
29 | db.init(false, onDatabaseInitDone) | |
30 | ||
31 | // ----------- Checker ----------- | |
32 | import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker' | |
33 | ||
34 | const missed = checkMissedConfig() | |
35 | if (missed.length !== 0) { | |
36 | throw new Error('Miss some configurations keys : ' + missed) | |
37 | } | |
38 | checkFFmpeg(function (err) { | |
39 | if (err) { | |
40 | throw err | |
41 | } | |
42 | }) | |
43 | ||
44 | const errorMessage = checkConfig() | |
45 | if (errorMessage !== null) { | |
46 | throw new Error(errorMessage) | |
47 | } | |
48 | ||
49 | // ----------- PeerTube modules ----------- | |
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' | |
54 | ||
55 | // ----------- Command line ----------- | |
56 | ||
57 | // ----------- App ----------- | |
58 | ||
59 | // For the logger | |
60 | app.use(morgan('combined', { | |
61 | stream: { write: logger.info } | |
62 | })) | |
63 | // For body requests | |
64 | app.use(bodyParser.json({ limit: '500kb' })) | |
65 | app.use(bodyParser.urlencoded({ extended: false })) | |
66 | // Validate some params for the API | |
67 | app.use(expressValidator({ | |
68 | customValidators: customValidators | |
69 | })) | |
70 | ||
71 | // ----------- Views, routes and static files ----------- | |
72 | ||
73 | // API | |
74 | const apiRoute = '/api/' + API_VERSION | |
75 | app.use(apiRoute, apiRouter) | |
76 | ||
77 | // Client files | |
78 | app.use('/', clientsRouter) | |
79 | ||
80 | // Static files | |
81 | app.use('/', staticRouter) | |
82 | ||
83 | // Always serve index client page (the client is a single page application, let it handle routing) | |
84 | app.use('/*', function (req, res, next) { | |
85 | res.sendFile(path.join(__dirname, '../client/dist/index.html')) | |
86 | }) | |
87 | ||
88 | // ----------- Tracker ----------- | |
89 | ||
90 | const trackerServer = new TrackerServer({ | |
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 | ||
105 | const server = http.createServer(app) | |
106 | const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) | |
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) { | |
115 | const err = new Error('Not Found') | |
116 | err['status'] = 404 | |
117 | next(err) | |
118 | }) | |
119 | ||
120 | app.use(function (err, req, res, next) { | |
121 | logger.error(err) | |
122 | res.sendStatus(err.status || 500) | |
123 | }) | |
124 | ||
125 | // ----------- Run ----------- | |
126 | ||
127 | function onDatabaseInitDone () { | |
128 | const port = CONFIG.LISTEN.PORT | |
129 | // Run the migration scripts if needed | |
130 | migrate(function (err) { | |
131 | if (err) throw err | |
132 | ||
133 | installApplication(function (err) { | |
134 | if (err) throw err | |
135 | ||
136 | // ----------- Make the server listening ----------- | |
137 | server.listen(port, function () { | |
138 | // Activate the communication with friends | |
139 | activateSchedulers() | |
140 | ||
141 | // Activate job scheduler | |
142 | JobScheduler.Instance.activate() | |
143 | ||
144 | logger.info('Server listening on port %d', port) | |
145 | logger.info('Webserver: %s', CONFIG.WEBSERVER.URL) | |
146 | }) | |
147 | }) | |
148 | }) | |
149 | } |