mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 16:13:24 +02:00
Separate websocket to its own role. Necessary because the app was getting pretty big already, but mostly because our Windows PCs don't like to connect to websockets on port 80, which is what we use for the app.
This commit is contained in:
50
lib/roles/app/middleware/auth.js
Normal file
50
lib/roles/app/middleware/auth.js
Normal file
@@ -0,0 +1,50 @@
|
||||
var jwtutil = require('../../../util/jwtutil')
|
||||
var urlutil = require('../../../util/urlutil')
|
||||
|
||||
var dbapi = require('../../../db/api')
|
||||
|
||||
module.exports = function(options) {
|
||||
return function(req, res, next) {
|
||||
if (req.query.jwt) {
|
||||
// Coming from auth client
|
||||
var data = jwtutil.decode(req.query.jwt, options.secret)
|
||||
, redir = urlutil.removeParam(req.url, 'jwt')
|
||||
if (data) {
|
||||
// Redirect once to get rid of the token
|
||||
dbapi.saveUserAfterLogin({
|
||||
name: data.name
|
||||
, email: data.email
|
||||
, ip: req.ip
|
||||
})
|
||||
.then(function() {
|
||||
req.session.jwt = data
|
||||
res.redirect(redir)
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
else {
|
||||
// Invalid token, forward to auth client
|
||||
res.redirect(options.authUrl)
|
||||
}
|
||||
}
|
||||
else if (req.session && req.session.jwt) {
|
||||
dbapi.loadUser(req.session.jwt.email)
|
||||
.then(function(user) {
|
||||
if (user) {
|
||||
// Continue existing session
|
||||
req.user = user
|
||||
next()
|
||||
}
|
||||
else {
|
||||
// We no longer have the user in the database
|
||||
res.redirect(options.authUrl)
|
||||
}
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
else {
|
||||
// No session, forward to auth client
|
||||
res.redirect(options.authUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
8
lib/roles/app/middleware/browser-icons.js
Normal file
8
lib/roles/app/middleware/browser-icons.js
Normal file
@@ -0,0 +1,8 @@
|
||||
var express = require('express')
|
||||
|
||||
var pathutil = require('../../../util/pathutil')
|
||||
|
||||
module.exports = function() {
|
||||
return express.static(
|
||||
pathutil.root('node_modules/stf-browser-db/dist'))
|
||||
}
|
||||
8
lib/roles/app/middleware/device-icons.js
Normal file
8
lib/roles/app/middleware/device-icons.js
Normal file
@@ -0,0 +1,8 @@
|
||||
var express = require('express')
|
||||
|
||||
var pathutil = require('../../../util/pathutil')
|
||||
|
||||
module.exports = function() {
|
||||
return express.static(
|
||||
pathutil.root('node_modules/stf-device-db/dist'))
|
||||
}
|
||||
114
lib/roles/app/middleware/webpack.js
Normal file
114
lib/roles/app/middleware/webpack.js
Normal file
@@ -0,0 +1,114 @@
|
||||
var path = require('path')
|
||||
var url = require('url')
|
||||
|
||||
var webpack = require('webpack')
|
||||
var mime = require('mime')
|
||||
var Promise = require('bluebird')
|
||||
var _ = require('lodash')
|
||||
var MemoryOutputFileSystem = require('webpack/lib/MemoryOutputFileSystem')
|
||||
var MemoryInputFileSystem =
|
||||
require('webpack/node_modules/enhanced-resolve/lib/MemoryInputFileSystem')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var lifecycle = require('../../../util/lifecycle')
|
||||
var globalOptions = require('../../../../webpack.config')
|
||||
|
||||
// Similar to webpack-dev-middleware, but integrates with our custom
|
||||
// lifecycle, behaves more like normal express middleware, and removes
|
||||
// all unnecessary features.
|
||||
module.exports = function(options) {
|
||||
var log = logger.createLogger('middleware:webpack')
|
||||
options = _.defaults(options || {}, globalOptions)
|
||||
|
||||
var storage = Object.create(null)
|
||||
var fs = new MemoryInputFileSystem(storage)
|
||||
|
||||
var compiler = webpack(options)
|
||||
compiler.outputFileSystem = new MemoryOutputFileSystem(storage)
|
||||
|
||||
var valid = false
|
||||
var queue = []
|
||||
|
||||
log.info('Creating bundle')
|
||||
var watching = compiler.watch(options.watchDelay, function(err) {
|
||||
if (err) {
|
||||
log.fatal('Webpack had an error', err.stack)
|
||||
lifecycle.fatal()
|
||||
}
|
||||
})
|
||||
|
||||
lifecycle.observe(function() {
|
||||
if (watching.watcher) {
|
||||
watching.watcher.close()
|
||||
}
|
||||
})
|
||||
|
||||
function doneListener(stats) {
|
||||
process.nextTick(function() {
|
||||
if (valid) {
|
||||
log.info(stats.toString(options.stats))
|
||||
|
||||
if (stats.hasErrors()) {
|
||||
log.error('Bundle has errors')
|
||||
}
|
||||
else if (stats.hasWarnings()) {
|
||||
log.warn('Bundle has warnings')
|
||||
}
|
||||
else {
|
||||
log.info('Bundle is now valid')
|
||||
}
|
||||
|
||||
queue.forEach(function(resolver) {
|
||||
resolver.resolve()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
valid = true
|
||||
}
|
||||
|
||||
function invalidate() {
|
||||
if (valid) {
|
||||
log.info('Bundle is now invalid')
|
||||
valid = false
|
||||
}
|
||||
}
|
||||
|
||||
compiler.plugin('done', doneListener)
|
||||
compiler.plugin('invalid', invalidate)
|
||||
compiler.plugin('compile', invalidate)
|
||||
|
||||
function bundle() {
|
||||
if (valid) {
|
||||
return Promise.resolve()
|
||||
}
|
||||
else {
|
||||
log.info('Waiting for bundle to finish')
|
||||
var resolver = Promise.defer()
|
||||
queue.push(resolver)
|
||||
return resolver.promise
|
||||
}
|
||||
}
|
||||
|
||||
return function(req, res, next) {
|
||||
var parsedUrl = url.parse(req.url)
|
||||
|
||||
var target = path.join(
|
||||
compiler.outputPath
|
||||
, parsedUrl.pathname
|
||||
)
|
||||
|
||||
bundle()
|
||||
.then(function() {
|
||||
try {
|
||||
var body = fs.readFileSync(target)
|
||||
res.set('Content-Type', mime.lookup(target))
|
||||
res.end(body)
|
||||
}
|
||||
catch (err) {
|
||||
return next()
|
||||
}
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user