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