Created
March 8, 2016 02:56
-
-
Save marr/21ab117b578db27c6590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Koa from 'koa' | |
import { createReadStream } from 'utils/fs' | |
import historyApiFallback from 'koa-connect-history-api-fallback' | |
import morgan from 'koa-morgan' | |
import responseCalls from './middleware/responseCalls' | |
import webpack from 'webpack' | |
import webpackConfig from '../../webpack.config.dev.babel' | |
import koaWebpackDevMiddleware from 'koa-webpack-dev-middleware' | |
import koaWebpackHotMiddleware from 'koa-webpack-hot-middleware' | |
const env = process.env.NODE_ENV || 'development' | |
const app = new Koa() | |
app.use(responseCalls) | |
// Logging | |
app.use(morgan('short')); | |
// trust proxy | |
app.proxy = true | |
// Need this for koa v1.x middlewares | |
const convert = require('koa-convert') | |
const route = require('koa-route') | |
// sessions | |
const session = require('koa-generic-session') | |
app.keys = ['your-session-secret'] | |
app.use(convert(session())) | |
// body parser | |
const bodyParser = require('koa-bodyparser') | |
app.use(bodyParser()) | |
// authentication | |
require('./lib/authn') | |
const passport = require('koa-passport') | |
app.use(passport.initialize()) | |
app.use(passport.session()) | |
// routes | |
app.use(route.get('/logout', function(ctx) { | |
ctx.logout() | |
ctx.redirect('https://westfieldlabs.oktapreview.com') | |
})) | |
// Redirects for our OAuth2 identity provider | |
app.use(route.get('/login', | |
passport.authenticate('oauth2') | |
)) | |
app.use(route.get('/oauth/callback', | |
passport.authenticate('oauth2', { | |
failureRedirect: '/unauthorized', | |
successRedirect: '/' | |
}) | |
)) | |
app.use(async function(ctx, next) { | |
try { | |
await next() | |
} catch (err) { | |
if (401 === err.status) { | |
ctx.type = 'html' | |
ctx.body = createReadStream('401.html') | |
} else { | |
throw err | |
} | |
} | |
}) | |
if ('development' === env) { | |
// Load dev webpack config | |
const compiler = webpack(webpackConfig) | |
// Serve assets from memory | |
app.use(convert(koaWebpackDevMiddleware(compiler, { | |
noInfo: true, publicPath: webpackConfig.output.publicPath | |
}))) | |
// Webpack Hot reloading. | |
app.use(convert(koaWebpackHotMiddleware(compiler, { | |
log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000 | |
}))) | |
} | |
app.use(route.get('/unauthorized', function(ctx) { | |
ctx.throw(401) | |
})) | |
// Client side redirect to app | |
app.use(route.get('/', function(ctx) { | |
if (ctx.isAuthenticated()) { | |
ctx.redirect(`/token/${ctx.req.user.accessToken}`) | |
} else { | |
ctx.redirect('/login') | |
} | |
})) | |
if ('development' === env) { | |
// Serve index.html for development | |
app.use(route.get('*', function(ctx) { | |
ctx.type = 'html' | |
ctx.body = createReadStream('index.html') | |
})) | |
} else { | |
// rewrite all requests to index.html | |
app.use(convert(historyApiFallback({ | |
verbose: true | |
}))) | |
// static | |
const serve = require('koa-static') | |
app.use(convert(serve('dist'))) | |
} | |
export default app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment