]>
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 | 13 | import * as morgan from 'morgan' |
1840c2f7 | 14 | import * as cors from 'cors' |
8afc19a6 | 15 | import * as cookieParser from 'cookie-parser' |
a030a9b2 | 16 | |
9f540774 C |
17 | process.title = 'peertube' |
18 | ||
a030a9b2 | 19 | // Create our main app |
13ce1d01 | 20 | const app = express() |
a030a9b2 | 21 | |
3482688c | 22 | // ----------- Core checker ----------- |
23687332 | 23 | import { checkMissedConfig, checkFFmpeg, checkConfig, checkActivityPubUrls } from './server/initializers/checker' |
69b0a27c | 24 | |
d5b7d911 C |
25 | // Do not use barrels because we don't want to load all modules here (we need to initialize database first) |
26 | import { logger } from './server/helpers/logger' | |
2baea0c7 | 27 | import { API_VERSION, CONFIG, STATIC_PATHS } from './server/initializers/constants' |
d5b7d911 | 28 | |
65fcc311 | 29 | const missed = checkMissedConfig() |
b65c27aa | 30 | if (missed.length !== 0) { |
d5b7d911 C |
31 | logger.error('Your configuration files miss keys: ' + missed) |
32 | process.exit(-1) | |
b65c27aa | 33 | } |
3482688c | 34 | |
3482688c | 35 | checkFFmpeg(CONFIG) |
d5b7d911 C |
36 | .catch(err => { |
37 | logger.error('Error in ffmpeg check.', { err }) | |
38 | process.exit(-1) | |
39 | }) | |
b65c27aa | 40 | |
65fcc311 | 41 | const errorMessage = checkConfig() |
b65c27aa C |
42 | if (errorMessage !== null) { |
43 | throw new Error(errorMessage) | |
69b0a27c C |
44 | } |
45 | ||
490b595a C |
46 | // Trust our proxy (IP forwarding...) |
47 | app.set('trust proxy', CONFIG.TRUST_PROXY) | |
48 | ||
3482688c | 49 | // ----------- Database ----------- |
91fea9fc | 50 | |
3482688c | 51 | // Initialize database and models |
91fea9fc C |
52 | import { initDatabaseModels } from './server/initializers/database' |
53 | import { migrate } from './server/initializers/migrator' | |
54 | migrate() | |
55 | .then(() => initDatabaseModels(false)) | |
3d3441d6 C |
56 | .then(() => startApplication()) |
57 | .catch(err => { | |
58 | logger.error('Cannot start application.', { err }) | |
59 | process.exit(-1) | |
60 | }) | |
3482688c | 61 | |
00057e85 | 62 | // ----------- PeerTube modules ----------- |
91fea9fc | 63 | import { installApplication } from './server/initializers' |
ecb4e35f | 64 | import { Emailer } from './server/lib/emailer' |
94a5ff8a | 65 | import { JobQueue } from './server/lib/job-queue' |
50d6de9c | 66 | import { VideosPreviewCache } from './server/lib/cache' |
244e76a5 RK |
67 | import { |
68 | activityPubRouter, | |
69 | apiRouter, | |
70 | clientsRouter, | |
71 | feedsRouter, | |
72 | staticRouter, | |
73 | servicesRouter, | |
9b67da3d C |
74 | webfingerRouter, |
75 | trackerRouter, | |
76 | createWebsocketServer | |
244e76a5 | 77 | } from './server/controllers' |
ecb4e35f | 78 | import { Redis } from './server/lib/redis' |
60650c77 | 79 | import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler' |
94a5ff8a | 80 | import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' |
2baea0c7 | 81 | import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler' |
a030a9b2 | 82 | |
a030a9b2 C |
83 | // ----------- Command line ----------- |
84 | ||
85 | // ----------- App ----------- | |
86 | ||
12daa837 WL |
87 | // Enable CORS for develop |
88 | if (isTestInstance()) { | |
89 | app.use((req, res, next) => { | |
90 | // These routes have already cors | |
91 | if ( | |
92 | req.path.indexOf(STATIC_PATHS.TORRENTS) === -1 && | |
93 | req.path.indexOf(STATIC_PATHS.WEBSEED) === -1 | |
94 | ) { | |
95 | return (cors({ | |
96 | origin: '*', | |
97 | exposedHeaders: 'Retry-After', | |
98 | credentials: true | |
99 | }))(req, res, next) | |
100 | } | |
101 | ||
102 | return next() | |
103 | }) | |
104 | } | |
1840c2f7 | 105 | |
a030a9b2 | 106 | // For the logger |
e02643f3 | 107 | app.use(morgan('combined', { |
23e27dd5 | 108 | stream: { write: logger.info.bind(logger) } |
e02643f3 | 109 | })) |
a030a9b2 | 110 | // For body requests |
bf9ae5ce | 111 | app.use(bodyParser.urlencoded({ extended: false })) |
165cdc75 | 112 | app.use(bodyParser.json({ |
86d13ec2 | 113 | type: [ 'application/json', 'application/*+json' ], |
165cdc75 C |
114 | limit: '500kb' |
115 | })) | |
8afc19a6 C |
116 | // Cookies |
117 | app.use(cookieParser()) | |
a030a9b2 | 118 | |
a96aed15 C |
119 | // ----------- Views, routes and static files ----------- |
120 | ||
121 | // API | |
122 | const apiRoute = '/api/' + API_VERSION | |
123 | app.use(apiRoute, apiRouter) | |
124 | ||
125 | // Services (oembed...) | |
126 | app.use('/services', servicesRouter) | |
127 | ||
350e31d6 | 128 | app.use('/', activityPubRouter) |
244e76a5 RK |
129 | app.use('/', feedsRouter) |
130 | app.use('/', webfingerRouter) | |
9b67da3d | 131 | app.use('/', trackerRouter) |
350e31d6 | 132 | |
a96aed15 C |
133 | // Static files |
134 | app.use('/', staticRouter) | |
135 | ||
989e526a C |
136 | // Client files, last valid routes! |
137 | app.use('/', clientsRouter) | |
a96aed15 | 138 | |
a030a9b2 C |
139 | // ----------- Errors ----------- |
140 | ||
141 | // Catch 404 and forward to error handler | |
142 | app.use(function (req, res, next) { | |
13ce1d01 | 143 | const err = new Error('Not Found') |
65fcc311 | 144 | err['status'] = 404 |
a030a9b2 C |
145 | next(err) |
146 | }) | |
147 | ||
6f4e2522 | 148 | app.use(function (err, req, res, next) { |
e3a682a8 C |
149 | let error = 'Unknown error.' |
150 | if (err) { | |
151 | error = err.stack || err.message || err | |
152 | } | |
153 | ||
154 | logger.error('Error in controller.', { error }) | |
155 | return res.status(err.status || 500).end() | |
6f4e2522 | 156 | }) |
a030a9b2 | 157 | |
9b67da3d C |
158 | const server = createWebsocketServer(app) |
159 | ||
79530164 C |
160 | // ----------- Run ----------- |
161 | ||
3d3441d6 | 162 | async function startApplication () { |
65fcc311 | 163 | const port = CONFIG.LISTEN.PORT |
cff8b272 | 164 | const hostname = CONFIG.LISTEN.HOSTNAME |
91fea9fc | 165 | |
3d3441d6 C |
166 | await installApplication() |
167 | ||
23687332 C |
168 | // Check activity pub urls are valid |
169 | checkActivityPubUrls() | |
170 | .catch(err => { | |
171 | logger.error('Error in ActivityPub URLs checker.', { err }) | |
172 | process.exit(-1) | |
173 | }) | |
174 | ||
3d3441d6 C |
175 | // Email initialization |
176 | Emailer.Instance.init() | |
177 | await Emailer.Instance.checkConnectionOrDie() | |
178 | ||
179 | await JobQueue.Instance.init() | |
180 | ||
181 | // Caches initializations | |
182 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE) | |
183 | ||
184 | // Enable Schedulers | |
185 | BadActorFollowScheduler.Instance.enable() | |
186 | RemoveOldJobsScheduler.Instance.enable() | |
2baea0c7 | 187 | UpdateVideosScheduler.Instance.enable() |
3d3441d6 C |
188 | |
189 | // Redis initialization | |
190 | Redis.Instance.init() | |
191 | ||
192 | // Make server listening | |
f55e5a7b C |
193 | server.listen(port, hostname, () => { |
194 | logger.info('Server listening on %s:%d', hostname, port) | |
195 | logger.info('Web server: %s', CONFIG.WEBSERVER.URL) | |
196 | }) | |
5804c0db | 197 | } |