aboutsummaryrefslogtreecommitdiffhomepage
path: root/server.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2015-06-09 17:41:40 +0200
committerChocobozzz <florian.bigard@gmail.com>2015-10-29 23:14:54 +0100
commit8c308c2bf7f658945d80be9d5880361238635f5b (patch)
tree2130ae60af58e59dab3df07a5d5cdd5174f91ae8 /server.js
parent8cb4b4e00ee57eb98dfe1455b6d2de36fc561797 (diff)
downloadPeerTube-8c308c2bf7f658945d80be9d5880361238635f5b.tar.gz
PeerTube-8c308c2bf7f658945d80be9d5880361238635f5b.tar.zst
PeerTube-8c308c2bf7f658945d80be9d5880361238635f5b.zip
Spawn
Diffstat (limited to 'server.js')
-rw-r--r--server.js165
1 files changed, 165 insertions, 0 deletions
diff --git a/server.js b/server.js
new file mode 100644
index 000000000..bff7ddd57
--- /dev/null
+++ b/server.js
@@ -0,0 +1,165 @@
1;(function () {
2 'use strict'
3
4 // ----------- Node modules -----------
5 var express = require('express')
6 var path = require('path')
7 var morgan = require('morgan')
8 var bodyParser = require('body-parser')
9 var multer = require('multer')
10 var TrackerServer = require('bittorrent-tracker').Server
11 var WebSocketServer = require('ws').Server
12 var app = express()
13 var http = require('http')
14
15 // ----------- Checker -----------
16 var checker = require('./src/checker')
17
18 var miss = checker.checkConfig()
19 if (miss.length !== 0) {
20 // Do not use logger module
21 console.error('Miss some configurations keys.', { miss: miss })
22 process.exit(0)
23 }
24
25 checker.createDirectories()
26
27 // ----------- PeTube modules -----------
28 var config = require('config')
29 var logger = require('./src/logger')
30 var routes = require('./routes')
31 var api = require('./routes/api')
32 var utils = require('./src/utils')
33 var videos = require('./src/videos')
34 var webtorrent = require('./src/webTorrentNode')
35
36 var compression
37 var port = config.get('listen.port')
38 var uploads = config.get('storage.uploads')
39
40 // ----------- Command line -----------
41
42 // ----------- App -----------
43 app.use(morgan('combined', { stream: logger.stream }))
44 app.use(bodyParser.json())
45 app.use(multer({ dest: uploads }))
46 app.use(bodyParser.urlencoded({ extended: false }))
47
48 // ----------- Views, routes and static files -----------
49
50 if (process.env.NODE_ENV === 'production') {
51 // logger.log('Production : static files in dist/\n')
52
53 // GZip compression
54 compression = require('compression')
55 app.use(compression())
56
57 // A month
58 var maxAge = 86400000 * 30
59
60 // TODO
61 app.get(/^\/(index|(partials\/[a-z\/]+))?$/, function (req, res, next) {
62 if (req.url === '/') {
63 req.url = '/index'
64 }
65
66 req.url += '.html'
67 next()
68 })
69
70 app.use(express.static(path.join(__dirname, '/dist/public'), { maxAge: maxAge }))
71 app.use(express.static(path.join(__dirname, '/dist/views'), { maxAge: maxAge }))
72 } else {
73 // Livereload
74 app.use(require('connect-livereload')({
75 port: 35729
76 }))
77
78 require('segfault-handler').registerHandler()
79
80 app.use(express.static(path.join(__dirname, '/public'), { maxAge: 0 }))
81
82 // Jade template from ./views directory
83 app.set('views', path.join(__dirname, '/views'))
84 app.set('view engine', 'jade')
85
86 // Views routes
87 app.use('/', routes)
88 }
89
90 // ----------- Routes -----------
91 app.use('/api/videos', api.videos)
92 app.use('/api/remotevideos', api.remoteVideos)
93 app.use('/api/pods', api.pods)
94
95 // ----------- Tracker -----------
96
97 var trackerServer = new TrackerServer({
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
112 var server = http.createServer(app)
113 var wss = new WebSocketServer({server: server, path: '/tracker/socket'})
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) {
122 var err = new Error('Not Found')
123 err.status = 404
124 next(err)
125 })
126
127 // Prod : no stacktraces leaked to user
128 if (process.env.NODE_ENV === 'production') {
129 app.use(function (err, req, res, next) {
130 logger.error('Error : ' + err.message, { error: err })
131 res.status(err.status || 500)
132 res.render('error', {
133 message: err.message,
134 error: {}
135 })
136 })
137 } else {
138 app.use(function (err, req, res, next) {
139 logger.error('Error : ' + err.message, { error: err })
140 res.status(err.status || 500)
141 res.render('error', {
142 message: err.message,
143 error: err
144 })
145 })
146 }
147
148 // ----------- Create the certificates if they don't already exist -----------
149 utils.createCertsIfNotExist(function (err) {
150 if (err) throw err
151 // Create/activate the webtorrent module
152 webtorrent.create(function () {
153 // ----------- Make the server listening -----------
154 server.listen(port, function () {
155 videos.seedAll(function () {
156 logger.info('Seeded all the videos')
157 logger.info('Server listening on port %d', port)
158 app.emit('ready')
159 })
160 })
161 })
162 })
163
164 module.exports = app
165})()