aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-11-25 12:32:21 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-11-25 14:21:41 +0100
commit79530164b6c8f96a6ccf6d33b591d565f1575e67 (patch)
treef610a32ad5cb7151247af12dcc1534d4adb7de37
parent8e124f999b8cf7d461ef7a923711d18edc69d84c (diff)
downloadPeerTube-79530164b6c8f96a6ccf6d33b591d565f1575e67.tar.gz
PeerTube-79530164b6c8f96a6ccf6d33b591d565f1575e67.tar.zst
PeerTube-79530164b6c8f96a6ccf6d33b591d565f1575e67.zip
Server: move static/client routes in controllers/
-rw-r--r--server.js33
-rw-r--r--server/controllers/client.js13
-rw-r--r--server/controllers/index.js4
-rw-r--r--server/controllers/static.js46
4 files changed, 68 insertions, 28 deletions
diff --git a/server.js b/server.js
index 16e27e852..6eb022000 100644
--- a/server.js
+++ b/server.js
@@ -2,7 +2,6 @@
2 2
3// ----------- Node modules ----------- 3// ----------- Node modules -----------
4const bodyParser = require('body-parser') 4const bodyParser = require('body-parser')
5const cors = require('cors')
6const express = require('express') 5const express = require('express')
7const expressValidator = require('express-validator') 6const expressValidator = require('express-validator')
8const http = require('http') 7const http = require('http')
@@ -66,35 +65,17 @@ app.use(expressValidator({
66 65
67// ----------- Views, routes and static files ----------- 66// ----------- Views, routes and static files -----------
68 67
69// API routes 68// API
70const apiRoute = '/api/' + constants.API_VERSION 69const apiRoute = '/api/' + constants.API_VERSION
71app.use(apiRoute, routes.api) 70app.use(apiRoute, routes.api)
72app.use('/', routes.client)
73
74// Static client files
75// TODO: move in client
76app.use('/client', express.static(path.join(__dirname, '/client/dist'), { maxAge: constants.STATIC_MAX_AGE }))
77// 404 for static files not found
78app.use('/client/*', function (req, res, next) {
79 res.sendStatus(404)
80})
81
82const torrentsPhysicalPath = constants.CONFIG.STORAGE.TORRENTS_DIR
83app.use(constants.STATIC_PATHS.TORRENTS, cors(), express.static(torrentsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }))
84 71
85// Videos path for webseeding 72// Client files
86const videosPhysicalPath = constants.CONFIG.STORAGE.VIDEOS_DIR 73app.use('/', routes.client)
87app.use(constants.STATIC_PATHS.WEBSEED, cors(), express.static(videosPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }))
88
89// Thumbnails path for express
90const thumbnailsPhysicalPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR
91app.use(constants.STATIC_PATHS.THUMBNAILS, express.static(thumbnailsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }))
92 74
93// Video previews path for express 75// Static files
94const previewsPhysicalPath = constants.CONFIG.STORAGE.PREVIEWS_DIR 76app.use('/', routes.static)
95app.use(constants.STATIC_PATHS.PREVIEWS, express.static(previewsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE }))
96 77
97// Always serve index client page 78// Always serve index client page (the client is a single page application, let it handle routing)
98app.use('/*', function (req, res, next) { 79app.use('/*', function (req, res, next) {
99 res.sendFile(path.join(__dirname, './client/dist/index.html')) 80 res.sendFile(path.join(__dirname, './client/dist/index.html'))
100}) 81})
@@ -136,6 +117,8 @@ app.use(function (err, req, res, next) {
136 res.sendStatus(err.status || 500) 117 res.sendStatus(err.status || 500)
137}) 118})
138 119
120// ----------- Run -----------
121
139const port = constants.CONFIG.LISTEN.PORT 122const port = constants.CONFIG.LISTEN.PORT
140installer.installApplication(function (err) { 123installer.installApplication(function (err) {
141 if (err) throw err 124 if (err) throw err
diff --git a/server/controllers/client.js b/server/controllers/client.js
index 61e094980..572db6133 100644
--- a/server/controllers/client.js
+++ b/server/controllers/client.js
@@ -13,8 +13,9 @@ const Video = mongoose.model('Video')
13const router = express.Router() 13const router = express.Router()
14 14
15const opengraphComment = '<!-- opengraph tags -->' 15const opengraphComment = '<!-- opengraph tags -->'
16const embedPath = path.join(__dirname, '../../client/dist/standalone/videos/embed.html') 16const distPath = path.join(__dirname, '../../client/dist')
17const indexPath = path.join(__dirname, '../../client/dist/index.html') 17const embedPath = path.join(distPath, 'standalone/videos/embed.html')
18const indexPath = path.join(distPath, 'index.html')
18 19
19// Special route that add OpenGraph tags 20// Special route that add OpenGraph tags
20// Do not use a template engine for a so little thing 21// Do not use a template engine for a so little thing
@@ -24,6 +25,14 @@ router.use('/videos/embed', function (req, res, next) {
24 res.sendFile(embedPath) 25 res.sendFile(embedPath)
25}) 26})
26 27
28// Static HTML/CSS/JS client files
29router.use('/client', express.static(distPath, { maxAge: constants.STATIC_MAX_AGE }))
30
31// 404 for static files not found
32router.use('/client/*', function (req, res, next) {
33 res.sendStatus(404)
34})
35
27// --------------------------------------------------------------------------- 36// ---------------------------------------------------------------------------
28 37
29module.exports = router 38module.exports = router
diff --git a/server/controllers/index.js b/server/controllers/index.js
index 90b481e52..c9ca297ef 100644
--- a/server/controllers/index.js
+++ b/server/controllers/index.js
@@ -2,8 +2,10 @@
2 2
3const apiController = require('./api/') 3const apiController = require('./api/')
4const clientController = require('./client') 4const clientController = require('./client')
5const staticController = require('./static')
5 6
6module.exports = { 7module.exports = {
7 api: apiController, 8 api: apiController,
8 client: clientController 9 client: clientController,
10 static: staticController
9} 11}
diff --git a/server/controllers/static.js b/server/controllers/static.js
new file mode 100644
index 000000000..a91ff044d
--- /dev/null
+++ b/server/controllers/static.js
@@ -0,0 +1,46 @@
1'use strict'
2
3const express = require('express')
4const cors = require('cors')
5
6const constants = require('../initializers/constants')
7
8const router = express.Router()
9
10/*
11 Cors is very important to let other pods access torrent and video files
12*/
13
14const torrentsPhysicalPath = constants.CONFIG.STORAGE.TORRENTS_DIR
15router.use(
16 constants.STATIC_PATHS.TORRENTS,
17 cors(),
18 express.static(torrentsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE })
19)
20
21// Videos path for webseeding
22const videosPhysicalPath = constants.CONFIG.STORAGE.VIDEOS_DIR
23router.use(
24 constants.STATIC_PATHS.WEBSEED,
25 cors(),
26 express.static(videosPhysicalPath, { maxAge: constants.STATIC_MAX_AGE })
27)
28
29// Thumbnails path for express
30const thumbnailsPhysicalPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR
31router.use(
32 constants.STATIC_PATHS.THUMBNAILS,
33 express.static(thumbnailsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE })
34)
35
36// Video previews path for express
37const previewsPhysicalPath = constants.CONFIG.STORAGE.PREVIEWS_DIR
38router.use(
39 constants.STATIC_PATHS.PREVIEWS,
40 express.static(previewsPhysicalPath, { maxAge: constants.STATIC_MAX_AGE })
41)
42
43// ---------------------------------------------------------------------------
44
45module.exports = router
46