1
0
Fork 0

Views refractoring

This commit is contained in:
Chocobozzz 2015-11-07 18:22:05 +01:00
parent 34ca3b5225
commit e85782f7da
5 changed files with 49 additions and 66 deletions

View file

@ -17,7 +17,6 @@
"blueimp-file-upload": "^9.10.1", "blueimp-file-upload": "^9.10.1",
"body-parser": "^1.12.4", "body-parser": "^1.12.4",
"bootstrap": "^3.3.5", "bootstrap": "^3.3.5",
"compression": "^1.5.0",
"config": "^1.14.0", "config": "^1.14.0",
"connect-livereload": "^0.5.3", "connect-livereload": "^0.5.3",
"debug": "^2.2.0", "debug": "^2.2.0",

View file

@ -1,10 +1,12 @@
;(function () { ;(function () {
'use strict' 'use strict'
var api = {} var express = require('express')
api.videos = require('./videos') var router = express.Router()
api.remoteVideos = require('./remoteVideos')
api.pods = require('./pods')
module.exports = api router.use('/videos', require('./videos'))
router.use('/remotevideos', require('./remoteVideos'))
router.use('/pods', require('./pods'))
module.exports = router
})() })()

View file

@ -1,24 +1,10 @@
;(function () { ;(function () {
'use strict' 'use strict'
function getPartial (req, res) { var routes = {
var directory = req.params.directory api: require('./api/' + global.API_VERSION),
var name = req.params.name views: require('./views')
res.render('partials/' + directory + '/' + name)
} }
function getIndex (req, res) { module.exports = routes
res.render('index')
}
var express = require('express')
var middleware = require('../middlewares')
var router = express.Router()
router.get('/partials/:directory/:name', middleware.cache(), getPartial)
router.get(/^\/(index)?$/, middleware.cache(), getIndex)
module.exports = router
})() })()

24
routes/views.js Normal file
View file

@ -0,0 +1,24 @@
;(function () {
'use strict'
function getPartial (req, res) {
var directory = req.params.directory
var name = req.params.name
res.render('partials/' + directory + '/' + name)
}
function getIndex (req, res) {
res.render('index')
}
var express = require('express')
var middleware = require('../middlewares')
var router = express.Router()
router.get('/partials/:directory/:name', middleware.cache(), getPartial)
router.get(/^\/(index)?$/, middleware.cache(), getIndex)
module.exports = router
})()

View file

@ -32,12 +32,10 @@
var config = require('config') var config = require('config')
var logger = require('./src/logger') var logger = require('./src/logger')
var routes = require('./routes') var routes = require('./routes')
var api = require('./routes/api/' + global.API_VERSION)
var utils = require('./src/utils') var utils = require('./src/utils')
var videos = require('./src/videos') var videos = require('./src/videos')
var webtorrent = require('./src/webTorrentNode') var webtorrent = require('./src/webTorrentNode')
var compression
var port = config.get('listen.port') var port = config.get('listen.port')
var uploads = config.get('storage.uploads') var uploads = config.get('storage.uploads')
@ -52,29 +50,6 @@
// ----------- Views, routes and static files ----------- // ----------- Views, routes and static files -----------
if (process.env.NODE_ENV === 'production') {
// logger.log('Production : static files in dist/\n')
// GZip compression
compression = require('compression')
app.use(compression())
// A month
var maxAge = 86400000 * 30
// TODO
app.get(/^\/(index|(partials\/[a-z\/]+))?$/, function (req, res, next) {
if (req.url === '/') {
req.url = '/index'
}
req.url += '.html'
next()
})
app.use(express.static(path.join(__dirname, '/dist/public'), { maxAge: maxAge }))
app.use(express.static(path.join(__dirname, '/dist/views'), { maxAge: maxAge }))
} else {
// Livereload // Livereload
app.use(require('connect-livereload')({ app.use(require('connect-livereload')({
port: 35729 port: 35729
@ -88,15 +63,12 @@
app.set('views', path.join(__dirname, '/views')) app.set('views', path.join(__dirname, '/views'))
app.set('view engine', 'jade') app.set('view engine', 'jade')
// Views routes // API
app.use('/', routes)
}
// ----------- Routes -----------
var api_route = '/api/' + global.API_VERSION var api_route = '/api/' + global.API_VERSION
app.use(api_route + '/videos', api.videos) app.use(api_route, routes.api)
app.use(api_route + '/remotevideos', api.remoteVideos)
app.use(api_route + '/pods', api.pods) // Views routes
app.use('/', routes.views)
// ----------- Tracker ----------- // ----------- Tracker -----------