Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lujjjh committed Jul 17, 2017
1 parent f3f1033 commit 8af587c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
13 changes: 3 additions & 10 deletions lib/express.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
'use strict'

const restc = require('./restc')
const serve = require('.')

module.exports = () => (req, res, next) => {
res.vary('Accept')
if (req.accepts([ 'json', 'html' ]) !== 'html') return next()
restc
.then(html => res.end(html))
.catch(error => {
console.error('[restc]', 'an error occurred when acquiring restc:', error.message)
next()
})
module.exports = options => (req, res, next) => {
serve(options)(req, res) || next()
}
27 changes: 27 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const fs = require('fs')
const path = require('path')

const content = fs.readFileSync(path.join(__dirname, '../faas/index.html'), 'utf-8')

module.exports = options => (req, res) => {
// set vary header for every response
res.setHeader('Vary', 'Accept, Origin')

// return if it is a cross-origin request
if ('origin' in req.headers) {
return false
}

// return if the client does not prefer text/html
let accept = req.headers.accept || ''
if (!/^text\/html(?:,|$)/.test(accept)) {
return false
}

// serve restc
res.send(content)

return true
}
12 changes: 4 additions & 8 deletions lib/koa.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict'

const restc = require('./restc')
const serve = require('.')
const response = require('./utils/response')

module.exports = () => function * (next) {
this.set('Vary', 'Accept')
if (this.accepts([ 'json', 'html' ]) !== 'html') return yield next
try {
this.body = yield restc
} catch (error) {
console.error('[restc]', 'an error occurred when acquiring restc:', error.message)
module.exports = options => function * (next) {
if (!serve(options)(this.request, response.fromKoaContext(this))) {
yield next
}
}
18 changes: 6 additions & 12 deletions lib/koa2.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
'use strict'

const restc = require('./restc')
const serve = require('.')
const response = require('./utils/response')

module.exports = () => (ctx, next) => {
ctx.set('Vary', 'Accept')
if (ctx.accepts([ 'json', 'html' ]) !== 'html') return next()
return restc
.then(html => {
ctx.body = html
})
.catch(error => {
console.error('[restc]', 'an error occurred when acquiring restc:', error.message)
return next()
})
module.exports = options => (ctx, next) => {
if (!serve(options)(ctx.request, response.fromKoaContext(ctx))) {
return next()
}
}
14 changes: 0 additions & 14 deletions lib/restc.js

This file was deleted.

16 changes: 16 additions & 0 deletions lib/utils/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

// patch Koa responses
exports.fromKoaContext = ctx => Object.create(ctx.response, {
send: {
value: payload => {
ctx.body = payload
}
},

setHeader: {
value: (k, v) => {
ctx.set(k, v)
}
}
})

0 comments on commit 8af587c

Please sign in to comment.