]>
Commit | Line | Data |
---|---|---|
1 | import { isTestInstance } from './server/helpers/core-utils' | |
2 | ||
3 | if (isTestInstance()) { | |
4 | require('source-map-support').install() | |
5 | } | |
6 | ||
7 | // ----------- Node modules ----------- | |
8 | import * as bodyParser from 'body-parser' | |
9 | import * as express from 'express' | |
10 | // FIXME: cannot import express-validator | |
11 | const expressValidator = require('express-validator') | |
12 | import * as http from 'http' | |
13 | import * as morgan from 'morgan' | |
14 | import * as path from 'path' | |
15 | import * as bittorrentTracker from 'bittorrent-tracker' | |
16 | import * as cors from 'cors' | |
17 | import { Server as WebSocketServer } from 'ws' | |
18 | ||
19 | const TrackerServer = bittorrentTracker.Server | |
20 | ||
21 | process.title = 'peertube' | |
22 | ||
23 | // Create our main app | |
24 | const app = express() | |
25 | ||
26 | // ----------- Core checker ----------- | |
27 | import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker' | |
28 | ||
29 | const missed = checkMissedConfig() | |
30 | if (missed.length !== 0) { | |
31 | throw new Error('Your configuration files miss keys: ' + missed) | |
32 | } | |
33 | ||
34 | import { API_VERSION, CONFIG, STATIC_PATHS } from './server/initializers/constants' | |
35 | checkFFmpeg(CONFIG) | |
36 | ||
37 | const errorMessage = checkConfig() | |
38 | if (errorMessage !== null) { | |
39 | throw new Error(errorMessage) | |
40 | } | |
41 | ||
42 | // ----------- Database ----------- | |
43 | // Do not use barrels because we don't want to load all modules here (we need to initialize database first) | |
44 | import { logger } from './server/helpers/logger' | |
45 | // Initialize database and models | |
46 | import { database as db } from './server/initializers/database' | |
47 | db.init(false).then(() => onDatabaseInitDone()) | |
48 | ||
49 | // ----------- PeerTube modules ----------- | |
50 | import { migrate, installApplication } from './server/initializers' | |
51 | import { JobScheduler, activateSchedulers, VideosPreviewCache } 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 | // Enable CORS for develop | |
60 | if (isTestInstance()) { | |
61 | app.use((req, res, next) => { | |
62 | // These routes have already cors | |
63 | if ( | |
64 | req.path.indexOf(STATIC_PATHS.TORRENTS) === -1 && | |
65 | req.path.indexOf(STATIC_PATHS.WEBSEED) === -1 | |
66 | ) { | |
67 | return (cors({ | |
68 | origin: 'http://localhost:3000', | |
69 | credentials: true | |
70 | }))(req, res, next) | |
71 | } | |
72 | ||
73 | return next() | |
74 | }) | |
75 | } | |
76 | ||
77 | // For the logger | |
78 | app.use(morgan('combined', { | |
79 | stream: { write: logger.info } | |
80 | })) | |
81 | // For body requests | |
82 | app.use(bodyParser.json({ limit: '500kb' })) | |
83 | app.use(bodyParser.urlencoded({ extended: false })) | |
84 | // Validate some params for the API | |
85 | app.use(expressValidator({ | |
86 | customValidators: customValidators | |
87 | })) | |
88 | ||
89 | // ----------- Views, routes and static files ----------- | |
90 | ||
91 | // API | |
92 | const apiRoute = '/api/' + API_VERSION | |
93 | app.use(apiRoute, apiRouter) | |
94 | ||
95 | // Client files | |
96 | app.use('/', clientsRouter) | |
97 | ||
98 | // Static files | |
99 | app.use('/', staticRouter) | |
100 | ||
101 | // Always serve index client page (the client is a single page application, let it handle routing) | |
102 | app.use('/*', function (req, res, next) { | |
103 | res.sendFile(path.join(__dirname, '../client/dist/index.html')) | |
104 | }) | |
105 | ||
106 | // ----------- Tracker ----------- | |
107 | ||
108 | const trackerServer = new TrackerServer({ | |
109 | http: false, | |
110 | udp: false, | |
111 | ws: false, | |
112 | dht: false | |
113 | }) | |
114 | ||
115 | trackerServer.on('error', function (err) { | |
116 | logger.error(err) | |
117 | }) | |
118 | ||
119 | trackerServer.on('warning', function (err) { | |
120 | logger.error(err) | |
121 | }) | |
122 | ||
123 | const server = http.createServer(app) | |
124 | const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) | |
125 | wss.on('connection', function (ws) { | |
126 | trackerServer.onWebSocketConnection(ws) | |
127 | }) | |
128 | ||
129 | // ----------- Errors ----------- | |
130 | ||
131 | // Catch 404 and forward to error handler | |
132 | app.use(function (req, res, next) { | |
133 | const err = new Error('Not Found') | |
134 | err['status'] = 404 | |
135 | next(err) | |
136 | }) | |
137 | ||
138 | app.use(function (err, req, res, next) { | |
139 | logger.error(err) | |
140 | res.sendStatus(err.status || 500) | |
141 | }) | |
142 | ||
143 | // ----------- Run ----------- | |
144 | ||
145 | function onDatabaseInitDone () { | |
146 | const port = CONFIG.LISTEN.PORT | |
147 | // Run the migration scripts if needed | |
148 | migrate() | |
149 | .then(() => { | |
150 | return installApplication() | |
151 | }) | |
152 | .then(() => { | |
153 | // ----------- Make the server listening ----------- | |
154 | server.listen(port, function () { | |
155 | // Activate the communication with friends | |
156 | activateSchedulers() | |
157 | ||
158 | // Activate job scheduler | |
159 | JobScheduler.Instance.activate() | |
160 | ||
161 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE) | |
162 | ||
163 | logger.info('Server listening on port %d', port) | |
164 | logger.info('Web server: %s', CONFIG.WEBSERVER.URL) | |
165 | }) | |
166 | }) | |
167 | } |