]>
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 C |
31 | |
32 | import { API_VERSION, CONFIG, STATIC_PATHS } from './server/initializers/constants' | |
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' | |
43 | // Initialize database and models | |
44 | import { database as db } from './server/initializers/database' | |
45 | db.init(false).then(() => onDatabaseInitDone()) | |
46 | ||
00057e85 | 47 | // ----------- PeerTube modules ----------- |
65fcc311 | 48 | import { migrate, installApplication } from './server/initializers' |
f981dae8 | 49 | import { JobScheduler, activateSchedulers, VideosPreviewCache } from './server/lib' |
65fcc311 | 50 | import { apiRouter, clientsRouter, staticRouter } from './server/controllers' |
a030a9b2 | 51 | |
a030a9b2 C |
52 | // ----------- Command line ----------- |
53 | ||
54 | // ----------- App ----------- | |
55 | ||
407c4473 | 56 | // Enable CORS for develop |
1840c2f7 | 57 | if (isTestInstance()) { |
93e1258c C |
58 | app.use((req, res, next) => { |
59 | // These routes have already cors | |
60 | if ( | |
61 | req.path.indexOf(STATIC_PATHS.TORRENTS) === -1 && | |
62 | req.path.indexOf(STATIC_PATHS.WEBSEED) === -1 | |
63 | ) { | |
64 | return (cors({ | |
65 | origin: 'http://localhost:3000', | |
66 | credentials: true | |
67 | }))(req, res, next) | |
68 | } | |
69 | ||
70 | return next() | |
71 | }) | |
1840c2f7 C |
72 | } |
73 | ||
a030a9b2 | 74 | // For the logger |
e02643f3 C |
75 | app.use(morgan('combined', { |
76 | stream: { write: logger.info } | |
77 | })) | |
a030a9b2 | 78 | // For body requests |
def16d33 | 79 | app.use(bodyParser.json({ limit: '500kb' })) |
a030a9b2 | 80 | app.use(bodyParser.urlencoded({ extended: false })) |
a030a9b2 C |
81 | |
82 | // ----------- Views, routes and static files ----------- | |
83 | ||
79530164 | 84 | // API |
65fcc311 C |
85 | const apiRoute = '/api/' + API_VERSION |
86 | app.use(apiRoute, apiRouter) | |
052937db | 87 | |
79530164 | 88 | // Client files |
65fcc311 | 89 | app.use('/', clientsRouter) |
cbe2f7c3 | 90 | |
79530164 | 91 | // Static files |
65fcc311 | 92 | app.use('/', staticRouter) |
6a94a109 | 93 | |
79530164 | 94 | // Always serve index client page (the client is a single page application, let it handle routing) |
6f4e2522 | 95 | app.use('/*', function (req, res, next) { |
e02643f3 | 96 | res.sendFile(path.join(__dirname, '../client/dist/index.html')) |
6f4e2522 | 97 | }) |
a030a9b2 C |
98 | |
99 | // ----------- Tracker ----------- | |
100 | ||
13ce1d01 | 101 | const trackerServer = new TrackerServer({ |
a030a9b2 C |
102 | http: false, |
103 | udp: false, | |
104 | ws: false, | |
105 | dht: false | |
106 | }) | |
107 | ||
108 | trackerServer.on('error', function (err) { | |
109 | logger.error(err) | |
110 | }) | |
111 | ||
112 | trackerServer.on('warning', function (err) { | |
113 | logger.error(err) | |
114 | }) | |
115 | ||
13ce1d01 | 116 | const server = http.createServer(app) |
65fcc311 | 117 | const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) |
a030a9b2 C |
118 | wss.on('connection', function (ws) { |
119 | trackerServer.onWebSocketConnection(ws) | |
120 | }) | |
121 | ||
122 | // ----------- Errors ----------- | |
123 | ||
124 | // Catch 404 and forward to error handler | |
125 | app.use(function (req, res, next) { | |
13ce1d01 | 126 | const err = new Error('Not Found') |
65fcc311 | 127 | err['status'] = 404 |
a030a9b2 C |
128 | next(err) |
129 | }) | |
130 | ||
6f4e2522 C |
131 | app.use(function (err, req, res, next) { |
132 | logger.error(err) | |
133 | res.sendStatus(err.status || 500) | |
134 | }) | |
a030a9b2 | 135 | |
79530164 C |
136 | // ----------- Run ----------- |
137 | ||
5804c0db | 138 | function onDatabaseInitDone () { |
65fcc311 | 139 | const port = CONFIG.LISTEN.PORT |
5804c0db | 140 | // Run the migration scripts if needed |
6fcd19ba C |
141 | migrate() |
142 | .then(() => { | |
143 | return installApplication() | |
144 | }) | |
145 | .then(() => { | |
5804c0db C |
146 | // ----------- Make the server listening ----------- |
147 | server.listen(port, function () { | |
148 | // Activate the communication with friends | |
65fcc311 | 149 | activateSchedulers() |
052937db | 150 | |
227d02fe | 151 | // Activate job scheduler |
65fcc311 | 152 | JobScheduler.Instance.activate() |
227d02fe | 153 | |
f981dae8 C |
154 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE) |
155 | ||
5804c0db | 156 | logger.info('Server listening on port %d', port) |
556ddc31 | 157 | logger.info('Web server: %s', CONFIG.WEBSERVER.URL) |
5804c0db | 158 | }) |
8c308c2b | 159 | }) |
5804c0db | 160 | } |