aboutsummaryrefslogtreecommitdiffhomepage
path: root/server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server.ts')
-rw-r--r--server.ts142
1 files changed, 142 insertions, 0 deletions
diff --git a/server.ts b/server.ts
new file mode 100644
index 000000000..119c0c61d
--- /dev/null
+++ b/server.ts
@@ -0,0 +1,142 @@
1// ----------- Node modules -----------
2import bodyParser = require('body-parser')
3import express = require('express')
4const expressValidator = require('express-validator')
5import http = require('http')
6import morgan = require('morgan')
7import path = require('path')
8import bittorrentTracker = require('bittorrent-tracker')
9import { Server as WebSocketServer } from 'ws'
10
11const TrackerServer = bittorrentTracker.Server
12
13process.title = 'peertube'
14
15// Create our main app
16const app = express()
17
18// ----------- Database -----------
19// Do not use barels because we don't want to load all modules here (we need to initialize database first)
20import { logger } from './server/helpers/logger'
21import { API_VERSION, CONFIG } from './server/initializers/constants'
22// Initialize database and models
23const db = require('./server/initializers/database')
24db.init(onDatabaseInitDone)
25
26// ----------- Checker -----------
27import { checkMissedConfig, checkFFmpeg, checkConfig } from './server/initializers/checker'
28
29const missed = checkMissedConfig()
30if (missed.length !== 0) {
31 throw new Error('Miss some configurations keys : ' + missed)
32}
33checkFFmpeg(function (err) {
34 if (err) {
35 throw err
36 }
37})
38
39const errorMessage = checkConfig()
40if (errorMessage !== null) {
41 throw new Error(errorMessage)
42}
43
44// ----------- PeerTube modules -----------
45import { migrate, installApplication } from './server/initializers'
46import { JobScheduler, activateSchedulers } from './server/lib'
47import * as customValidators from './server/helpers/custom-validators'
48import { apiRouter, clientsRouter, staticRouter } from './server/controllers'
49
50// ----------- Command line -----------
51
52// ----------- App -----------
53
54// For the logger
55// app.use(morgan('combined', { stream: logger.stream }))
56// For body requests
57app.use(bodyParser.json({ limit: '500kb' }))
58app.use(bodyParser.urlencoded({ extended: false }))
59// Validate some params for the API
60app.use(expressValidator({
61 customValidators: customValidators
62}))
63
64// ----------- Views, routes and static files -----------
65
66// API
67const apiRoute = '/api/' + API_VERSION
68app.use(apiRoute, apiRouter)
69
70// Client files
71app.use('/', clientsRouter)
72
73// Static files
74app.use('/', staticRouter)
75
76// Always serve index client page (the client is a single page application, let it handle routing)
77app.use('/*', function (req, res, next) {
78 res.sendFile(path.join(__dirname, './client/dist/index.html'))
79})
80
81// ----------- Tracker -----------
82
83const trackerServer = new TrackerServer({
84 http: false,
85 udp: false,
86 ws: false,
87 dht: false
88})
89
90trackerServer.on('error', function (err) {
91 logger.error(err)
92})
93
94trackerServer.on('warning', function (err) {
95 logger.error(err)
96})
97
98const server = http.createServer(app)
99const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
100wss.on('connection', function (ws) {
101 trackerServer.onWebSocketConnection(ws)
102})
103
104// ----------- Errors -----------
105
106// Catch 404 and forward to error handler
107app.use(function (req, res, next) {
108 const err = new Error('Not Found')
109 err['status'] = 404
110 next(err)
111})
112
113app.use(function (err, req, res, next) {
114 logger.error(err)
115 res.sendStatus(err.status || 500)
116})
117
118// ----------- Run -----------
119
120function onDatabaseInitDone () {
121 const port = CONFIG.LISTEN.PORT
122 // Run the migration scripts if needed
123 migrate(function (err) {
124 if (err) throw err
125
126 installApplication(function (err) {
127 if (err) throw err
128
129 // ----------- Make the server listening -----------
130 server.listen(port, function () {
131 // Activate the communication with friends
132 activateSchedulers()
133
134 // Activate job scheduler
135 JobScheduler.Instance.activate()
136
137 logger.info('Server listening on port %d', port)
138 logger.info('Webserver: %s', CONFIG.WEBSERVER.URL)
139 })
140 })
141 })
142}