]>
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 | |
00057e85 | 26 | // ----------- Database ----------- |
65fcc311 C |
27 | // Do not use barels because we don't want to load all modules here (we need to initialize database first) |
28 | import { logger } from './server/helpers/logger' | |
29 | import { API_VERSION, CONFIG } from './server/initializers/constants' | |
feb4bdfd | 30 | // Initialize database and models |
e02643f3 | 31 | import { database as db } from './server/initializers/database' |
6fcd19ba | 32 | db.init(false).then(() => onDatabaseInitDone()) |
00057e85 | 33 | |
69b0a27c | 34 | // ----------- Checker ----------- |
65fcc311 | 35 | import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker' |
69b0a27c | 36 | |
65fcc311 | 37 | const missed = checkMissedConfig() |
b65c27aa C |
38 | if (missed.length !== 0) { |
39 | throw new Error('Miss some configurations keys : ' + missed) | |
40 | } | |
6fcd19ba | 41 | checkFFmpeg() |
b65c27aa | 42 | |
65fcc311 | 43 | const errorMessage = checkConfig() |
b65c27aa C |
44 | if (errorMessage !== null) { |
45 | throw new Error(errorMessage) | |
69b0a27c C |
46 | } |
47 | ||
00057e85 | 48 | // ----------- PeerTube modules ----------- |
65fcc311 | 49 | import { migrate, installApplication } from './server/initializers' |
f981dae8 | 50 | import { JobScheduler, activateSchedulers, VideosPreviewCache } from './server/lib' |
65fcc311 C |
51 | import * as customValidators from './server/helpers/custom-validators' |
52 | import { apiRouter, clientsRouter, staticRouter } from './server/controllers' | |
a030a9b2 | 53 | |
a030a9b2 C |
54 | // ----------- Command line ----------- |
55 | ||
56 | // ----------- App ----------- | |
57 | ||
1840c2f7 C |
58 | // Enable cors for develop |
59 | if (isTestInstance()) { | |
60 | app.use(cors({ | |
61 | origin: 'http://localhost:3000', | |
62 | credentials: true | |
63 | })) | |
64 | } | |
65 | ||
a030a9b2 | 66 | // For the logger |
e02643f3 C |
67 | app.use(morgan('combined', { |
68 | stream: { write: logger.info } | |
69 | })) | |
a030a9b2 | 70 | // For body requests |
def16d33 | 71 | app.use(bodyParser.json({ limit: '500kb' })) |
a030a9b2 C |
72 | app.use(bodyParser.urlencoded({ extended: false })) |
73 | // Validate some params for the API | |
74 | app.use(expressValidator({ | |
65fcc311 | 75 | customValidators: customValidators |
a030a9b2 C |
76 | })) |
77 | ||
78 | // ----------- Views, routes and static files ----------- | |
79 | ||
79530164 | 80 | // API |
65fcc311 C |
81 | const apiRoute = '/api/' + API_VERSION |
82 | app.use(apiRoute, apiRouter) | |
052937db | 83 | |
79530164 | 84 | // Client files |
65fcc311 | 85 | app.use('/', clientsRouter) |
cbe2f7c3 | 86 | |
79530164 | 87 | // Static files |
65fcc311 | 88 | app.use('/', staticRouter) |
6a94a109 | 89 | |
79530164 | 90 | // Always serve index client page (the client is a single page application, let it handle routing) |
6f4e2522 | 91 | app.use('/*', function (req, res, next) { |
e02643f3 | 92 | res.sendFile(path.join(__dirname, '../client/dist/index.html')) |
6f4e2522 | 93 | }) |
a030a9b2 C |
94 | |
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 | ||
118 | // ----------- Errors ----------- | |
119 | ||
120 | // Catch 404 and forward to error handler | |
121 | app.use(function (req, res, next) { | |
13ce1d01 | 122 | const err = new Error('Not Found') |
65fcc311 | 123 | err['status'] = 404 |
a030a9b2 C |
124 | next(err) |
125 | }) | |
126 | ||
6f4e2522 C |
127 | app.use(function (err, req, res, next) { |
128 | logger.error(err) | |
129 | res.sendStatus(err.status || 500) | |
130 | }) | |
a030a9b2 | 131 | |
79530164 C |
132 | // ----------- Run ----------- |
133 | ||
5804c0db | 134 | function onDatabaseInitDone () { |
65fcc311 | 135 | const port = CONFIG.LISTEN.PORT |
5804c0db | 136 | // Run the migration scripts if needed |
6fcd19ba C |
137 | migrate() |
138 | .then(() => { | |
139 | return installApplication() | |
140 | }) | |
141 | .then(() => { | |
5804c0db C |
142 | // ----------- Make the server listening ----------- |
143 | server.listen(port, function () { | |
144 | // Activate the communication with friends | |
65fcc311 | 145 | activateSchedulers() |
052937db | 146 | |
227d02fe | 147 | // Activate job scheduler |
65fcc311 | 148 | JobScheduler.Instance.activate() |
227d02fe | 149 | |
f981dae8 C |
150 | VideosPreviewCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE) |
151 | ||
5804c0db | 152 | logger.info('Server listening on port %d', port) |
65fcc311 | 153 | logger.info('Webserver: %s', CONFIG.WEBSERVER.URL) |
5804c0db | 154 | }) |
8c308c2b | 155 | }) |
5804c0db | 156 | } |