]>
Commit | Line | Data |
---|---|---|
1 | // FIXME: https://github.com/nodejs/node/pull/16853 | |
2 | require('tls').DEFAULT_ECDH_CURVE = 'auto' | |
3 | ||
4 | import { isTestInstance } from './server/helpers/core-utils' | |
5 | ||
6 | if (isTestInstance()) { | |
7 | require('source-map-support').install() | |
8 | } | |
9 | ||
10 | // ----------- Node modules ----------- | |
11 | import * as bodyParser from 'body-parser' | |
12 | import * as express from 'express' | |
13 | import * as http from 'http' | |
14 | import * as morgan from 'morgan' | |
15 | import * as path from 'path' | |
16 | import * as bitTorrentTracker from 'bittorrent-tracker' | |
17 | import * as cors from 'cors' | |
18 | import { Server as WebSocketServer } from 'ws' | |
19 | ||
20 | const TrackerServer = bitTorrentTracker.Server | |
21 | ||
22 | process.title = 'peertube' | |
23 | ||
24 | // Create our main app | |
25 | const app = express() | |
26 | ||
27 | // ----------- Core checker ----------- | |
28 | import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker' | |
29 | ||
30 | // Do not use barrels because we don't want to load all modules here (we need to initialize database first) | |
31 | import { logger } from './server/helpers/logger' | |
32 | import { ACCEPT_HEADERS, API_VERSION, CONFIG, STATIC_PATHS } from './server/initializers/constants' | |
33 | ||
34 | const missed = checkMissedConfig() | |
35 | if (missed.length !== 0) { | |
36 | logger.error('Your configuration files miss keys: ' + missed) | |
37 | process.exit(-1) | |
38 | } | |
39 | ||
40 | checkFFmpeg(CONFIG) | |
41 | .catch(err => { | |
42 | logger.error('Error in ffmpeg check.', { err }) | |
43 | process.exit(-1) | |
44 | }) | |
45 | ||
46 | const errorMessage = checkConfig() | |
47 | if (errorMessage !== null) { | |
48 | throw new Error(errorMessage) | |
49 | } | |
50 | ||
51 | // ----------- Database ----------- | |
52 | ||
53 | // Initialize database and models | |
54 | import { initDatabaseModels } from './server/initializers/database' | |
55 | import { migrate } from './server/initializers/migrator' | |
56 | migrate() | |
57 | .then(() => initDatabaseModels(false)) | |
58 | .then(() => onDatabaseInitDone()) | |
59 | ||
60 | // ----------- PeerTube modules ----------- | |
61 | import { installApplication } from './server/initializers' | |
62 | import { Emailer } from './server/lib/emailer' | |
63 | import { JobQueue } from './server/lib/job-queue' | |
64 | import { VideosPreviewCache } from './server/lib/cache' | |
65 | import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers' | |
66 | import { Redis } from './server/lib/redis' | |
67 | import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler' | |
68 | import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' | |
69 | ||
70 | // ----------- Command line ----------- | |
71 | ||
72 | // ----------- App ----------- | |
73 | ||
74 | // Enable CORS for develop | |
75 | if (isTestInstance()) { | |
76 | app.use((req, res, next) => { | |
77 | // These routes have already cors | |
78 | if ( | |
79 | req.path.indexOf(STATIC_PATHS.TORRENTS) === -1 && | |
80 | req.path.indexOf(STATIC_PATHS.WEBSEED) === -1 | |
81 | ) { | |
82 | return (cors({ | |
83 | origin: 'http://localhost:3000', | |
84 | credentials: true | |
85 | }))(req, res, next) | |
86 | } | |
87 | ||
88 | return next() | |
89 | }) | |
90 | } | |
91 | ||
92 | // For the logger | |
93 | app.use(morgan('combined', { | |
94 | stream: { write: logger.info.bind(logger) } | |
95 | })) | |
96 | // For body requests | |
97 | app.use(bodyParser.urlencoded({ extended: false })) | |
98 | app.use(bodyParser.json({ | |
99 | type: [ 'application/json', 'application/*+json' ], | |
100 | limit: '500kb' | |
101 | })) | |
102 | ||
103 | // ----------- Tracker ----------- | |
104 | ||
105 | const trackerServer = new TrackerServer({ | |
106 | http: false, | |
107 | udp: false, | |
108 | ws: false, | |
109 | dht: false | |
110 | }) | |
111 | ||
112 | trackerServer.on('error', function (err) { | |
113 | logger.error('Error in websocket tracker.', err) | |
114 | }) | |
115 | ||
116 | trackerServer.on('warning', function (err) { | |
117 | logger.error('Warning in websocket tracker.', err) | |
118 | }) | |
119 | ||
120 | const server = http.createServer(app) | |
121 | const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) | |
122 | wss.on('connection', function (ws) { | |
123 | trackerServer.onWebSocketConnection(ws) | |
124 | }) | |
125 | ||
126 | const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer) | |
127 | app.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) | |
128 | app.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) | |
129 | ||
130 | // ----------- Views, routes and static files ----------- | |
131 | ||
132 | // API | |
133 | const apiRoute = '/api/' + API_VERSION | |
134 | app.use(apiRoute, apiRouter) | |
135 | ||
136 | // Services (oembed...) | |
137 | app.use('/services', servicesRouter) | |
138 | ||
139 | app.use('/', webfingerRouter) | |
140 | app.use('/', activityPubRouter) | |
141 | ||
142 | // Client files | |
143 | app.use('/', clientsRouter) | |
144 | ||
145 | // Static files | |
146 | app.use('/', staticRouter) | |
147 | ||
148 | // Always serve index client page (the client is a single page application, let it handle routing) | |
149 | app.use('/*', function (req, res) { | |
150 | if (req.accepts(ACCEPT_HEADERS) === 'html') { | |
151 | return res.sendFile(path.join(__dirname, '../client/dist/index.html')) | |
152 | } | |
153 | ||
154 | return res.status(404).end() | |
155 | }) | |
156 | ||
157 | // ----------- Errors ----------- | |
158 | ||
159 | // Catch 404 and forward to error handler | |
160 | app.use(function (req, res, next) { | |
161 | const err = new Error('Not Found') | |
162 | err['status'] = 404 | |
163 | next(err) | |
164 | }) | |
165 | ||
166 | app.use(function (err, req, res, next) { | |
167 | let error = 'Unknown error.' | |
168 | if (err) { | |
169 | error = err.stack || err.message || err | |
170 | } | |
171 | ||
172 | logger.error('Error in controller.', { error }) | |
173 | return res.status(err.status || 500).end() | |
174 | }) | |
175 | ||
176 | // ----------- Run ----------- | |
177 | ||
178 | function onDatabaseInitDone () { | |
179 | const port = CONFIG.LISTEN.PORT | |
180 | ||
181 | installApplication() | |
182 | .then(() => { | |
183 | // ----------- Make the server listening ----------- | |
184 | server.listen(port, () => { | |
185 | // Emailer initialization and then job queue initialization | |
186 | Emailer.Instance.init() | |
187 | Emailer.Instance.checkConnectionOrDie() | |
188 | .then(() => JobQueue.Instance.init()) | |
189 | ||
190 | // Caches initializations | |
191 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE) | |
192 | ||
193 | // Enable Schedulers | |
194 | BadActorFollowScheduler.Instance.enable() | |
195 | RemoveOldJobsScheduler.Instance.enable() | |
196 | ||
197 | // Redis initialization | |
198 | Redis.Instance.init() | |
199 | ||
200 | logger.info('Server listening on port %d', port) | |
201 | logger.info('Web server: %s', CONFIG.WEBSERVER.URL) | |
202 | }) | |
203 | }) | |
204 | } |