]>
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 C |
31 | import { database as db } from './server/initializers/database' |
32 | db.init(false, 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 | } | |
65fcc311 | 41 | checkFFmpeg(function (err) { |
4e284e97 C |
42 | if (err) { |
43 | throw err | |
44 | } | |
45 | }) | |
b65c27aa | 46 | |
65fcc311 | 47 | const errorMessage = checkConfig() |
b65c27aa C |
48 | if (errorMessage !== null) { |
49 | throw new Error(errorMessage) | |
69b0a27c C |
50 | } |
51 | ||
00057e85 | 52 | // ----------- PeerTube modules ----------- |
65fcc311 C |
53 | import { migrate, installApplication } from './server/initializers' |
54 | import { JobScheduler, activateSchedulers } from './server/lib' | |
55 | import * as customValidators from './server/helpers/custom-validators' | |
56 | import { apiRouter, clientsRouter, staticRouter } from './server/controllers' | |
a030a9b2 | 57 | |
a030a9b2 C |
58 | // ----------- Command line ----------- |
59 | ||
60 | // ----------- App ----------- | |
61 | ||
1840c2f7 C |
62 | // Enable cors for develop |
63 | if (isTestInstance()) { | |
64 | app.use(cors({ | |
65 | origin: 'http://localhost:3000', | |
66 | credentials: true | |
67 | })) | |
68 | } | |
69 | ||
a030a9b2 | 70 | // For the logger |
e02643f3 C |
71 | app.use(morgan('combined', { |
72 | stream: { write: logger.info } | |
73 | })) | |
a030a9b2 | 74 | // For body requests |
def16d33 | 75 | app.use(bodyParser.json({ limit: '500kb' })) |
a030a9b2 C |
76 | app.use(bodyParser.urlencoded({ extended: false })) |
77 | // Validate some params for the API | |
78 | app.use(expressValidator({ | |
65fcc311 | 79 | customValidators: customValidators |
a030a9b2 C |
80 | })) |
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 |
65fcc311 | 141 | migrate(function (err) { |
00d6b0dd | 142 | if (err) throw err |
a030a9b2 | 143 | |
65fcc311 | 144 | installApplication(function (err) { |
5804c0db C |
145 | if (err) throw err |
146 | ||
147 | // ----------- Make the server listening ----------- | |
148 | server.listen(port, function () { | |
149 | // Activate the communication with friends | |
65fcc311 | 150 | activateSchedulers() |
052937db | 151 | |
227d02fe | 152 | // Activate job scheduler |
65fcc311 | 153 | JobScheduler.Instance.activate() |
227d02fe | 154 | |
5804c0db | 155 | logger.info('Server listening on port %d', port) |
65fcc311 | 156 | logger.info('Webserver: %s', CONFIG.WEBSERVER.URL) |
5804c0db | 157 | }) |
8c308c2b C |
158 | }) |
159 | }) | |
5804c0db | 160 | } |