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