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