]>
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 | |
65fcc311 | 30 | const missed = checkMissedConfig() |
b65c27aa | 31 | if (missed.length !== 0) { |
3482688c | 32 | throw new Error('Your configuration files miss keys: ' + missed) |
b65c27aa | 33 | } |
3482688c | 34 | |
4f491371 | 35 | import { ACCEPT_HEADERS, API_VERSION, CONFIG, STATIC_PATHS } from './server/initializers/constants' |
3482688c | 36 | checkFFmpeg(CONFIG) |
b65c27aa | 37 | |
65fcc311 | 38 | const errorMessage = checkConfig() |
b65c27aa C |
39 | if (errorMessage !== null) { |
40 | throw new Error(errorMessage) | |
69b0a27c C |
41 | } |
42 | ||
3482688c C |
43 | // ----------- Database ----------- |
44 | // Do not use barrels because we don't want to load all modules here (we need to initialize database first) | |
45 | import { logger } from './server/helpers/logger' | |
91fea9fc | 46 | |
3482688c | 47 | // Initialize database and models |
91fea9fc C |
48 | import { initDatabaseModels } from './server/initializers/database' |
49 | import { migrate } from './server/initializers/migrator' | |
50 | migrate() | |
51 | .then(() => initDatabaseModels(false)) | |
52 | .then(() => onDatabaseInitDone()) | |
3482688c | 53 | |
00057e85 | 54 | // ----------- PeerTube modules ----------- |
91fea9fc | 55 | import { installApplication } from './server/initializers' |
94a5ff8a | 56 | import { JobQueue } from './server/lib/job-queue' |
50d6de9c | 57 | import { VideosPreviewCache } from './server/lib/cache' |
350e31d6 | 58 | import { apiRouter, clientsRouter, staticRouter, servicesRouter, webfingerRouter, activityPubRouter } from './server/controllers' |
60650c77 | 59 | import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler' |
94a5ff8a | 60 | import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' |
a030a9b2 | 61 | |
a030a9b2 C |
62 | // ----------- Command line ----------- |
63 | ||
64 | // ----------- App ----------- | |
65 | ||
407c4473 | 66 | // Enable CORS for develop |
1840c2f7 | 67 | if (isTestInstance()) { |
93e1258c C |
68 | app.use((req, res, next) => { |
69 | // These routes have already cors | |
70 | if ( | |
71 | req.path.indexOf(STATIC_PATHS.TORRENTS) === -1 && | |
72 | req.path.indexOf(STATIC_PATHS.WEBSEED) === -1 | |
73 | ) { | |
74 | return (cors({ | |
75 | origin: 'http://localhost:3000', | |
76 | credentials: true | |
77 | }))(req, res, next) | |
78 | } | |
79 | ||
80 | return next() | |
81 | }) | |
1840c2f7 C |
82 | } |
83 | ||
a030a9b2 | 84 | // For the logger |
e02643f3 | 85 | app.use(morgan('combined', { |
23e27dd5 | 86 | stream: { write: logger.info.bind(logger) } |
e02643f3 | 87 | })) |
a030a9b2 | 88 | // For body requests |
165cdc75 | 89 | app.use(bodyParser.json({ |
86d13ec2 | 90 | type: [ 'application/json', 'application/*+json' ], |
165cdc75 C |
91 | limit: '500kb' |
92 | })) | |
a030a9b2 | 93 | app.use(bodyParser.urlencoded({ extended: false })) |
a030a9b2 | 94 | |
a030a9b2 C |
95 | // ----------- Tracker ----------- |
96 | ||
13ce1d01 | 97 | const trackerServer = new TrackerServer({ |
a030a9b2 C |
98 | http: false, |
99 | udp: false, | |
100 | ws: false, | |
101 | dht: false | |
102 | }) | |
103 | ||
104 | trackerServer.on('error', function (err) { | |
105 | logger.error(err) | |
106 | }) | |
107 | ||
108 | trackerServer.on('warning', function (err) { | |
109 | logger.error(err) | |
110 | }) | |
111 | ||
13ce1d01 | 112 | const server = http.createServer(app) |
65fcc311 | 113 | const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) |
a030a9b2 C |
114 | wss.on('connection', function (ws) { |
115 | trackerServer.onWebSocketConnection(ws) | |
116 | }) | |
117 | ||
a96aed15 C |
118 | const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer) |
119 | app.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) | |
120 | app.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) | |
121 | ||
122 | // ----------- Views, routes and static files ----------- | |
123 | ||
124 | // API | |
125 | const apiRoute = '/api/' + API_VERSION | |
126 | app.use(apiRoute, apiRouter) | |
127 | ||
128 | // Services (oembed...) | |
129 | app.use('/services', servicesRouter) | |
130 | ||
350e31d6 C |
131 | app.use('/', webfingerRouter) |
132 | app.use('/', activityPubRouter) | |
133 | ||
a96aed15 C |
134 | // Client files |
135 | app.use('/', clientsRouter) | |
136 | ||
137 | // Static files | |
138 | app.use('/', staticRouter) | |
139 | ||
140 | // Always serve index client page (the client is a single page application, let it handle routing) | |
141 | app.use('/*', function (req, res) { | |
4f491371 | 142 | if (req.accepts(ACCEPT_HEADERS) === 'html') { |
98ec8b8e C |
143 | return res.sendFile(path.join(__dirname, '../client/dist/index.html')) |
144 | } | |
145 | ||
146 | return res.status(404).end() | |
a96aed15 C |
147 | }) |
148 | ||
a030a9b2 C |
149 | // ----------- Errors ----------- |
150 | ||
151 | // Catch 404 and forward to error handler | |
152 | app.use(function (req, res, next) { | |
13ce1d01 | 153 | const err = new Error('Not Found') |
65fcc311 | 154 | err['status'] = 404 |
a030a9b2 C |
155 | next(err) |
156 | }) | |
157 | ||
6f4e2522 | 158 | app.use(function (err, req, res, next) { |
4635f59d | 159 | logger.error(err, err) |
6f4e2522 C |
160 | res.sendStatus(err.status || 500) |
161 | }) | |
a030a9b2 | 162 | |
79530164 C |
163 | // ----------- Run ----------- |
164 | ||
5804c0db | 165 | function onDatabaseInitDone () { |
65fcc311 | 166 | const port = CONFIG.LISTEN.PORT |
91fea9fc C |
167 | |
168 | installApplication() | |
6fcd19ba | 169 | .then(() => { |
5804c0db | 170 | // ----------- Make the server listening ----------- |
571389d4 | 171 | server.listen(port, () => { |
e8e12200 | 172 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE) |
60650c77 | 173 | BadActorFollowScheduler.Instance.enable() |
94a5ff8a C |
174 | RemoveOldJobsScheduler.Instance.enable() |
175 | JobQueue.Instance.init() | |
f981dae8 | 176 | |
5804c0db | 177 | logger.info('Server listening on port %d', port) |
556ddc31 | 178 | logger.info('Web server: %s', CONFIG.WEBSERVER.URL) |
5804c0db | 179 | }) |
8c308c2b | 180 | }) |
5804c0db | 181 | } |