Skip to content

Commit

Permalink
Merge pull request #11 from fabrix-app/v1.5
Browse files Browse the repository at this point in the history
V1.5
  • Loading branch information
scott-wyatt authored Oct 7, 2018
2 parents 50116e7 + f553f85 commit 62d90dd
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 50 deletions.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions archetype/src/config/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Express Configuration
* This supplements the config.web with express only configuration
*/
export const express = {
/**
* Spool-express uses helmet by default, it can be disabled by turning the value to false
*/
helmet: {}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const web = {
'compression',
'methodOverride',
'www',
'helmet',
'router',
'404',
'500'
Expand Down
7 changes: 7 additions & 0 deletions lib/ExpressSpool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ServerSpool } from '@fabrix/fabrix/dist/common/spools/server'
import { isArray } from 'lodash'
import { Express } from 'express'
import * as helmet from 'helmet'

import { Server } from './server'
import { Validator } from './validator'
Expand Down Expand Up @@ -50,6 +51,7 @@ export class ExpressSpool extends ServerSpool {
}

return Promise.all([
Validator.validateExpress(this.app.config.get('express')),
Validator.validateWebConfig(this.app.config.get('web'))
])
.catch(err => {
Expand All @@ -58,7 +60,12 @@ export class ExpressSpool extends ServerSpool {
}

configure () {
// Set a config that let's other spools know this is using express as a webserver
this.app.config.set('web.server', 'express')
// Set helmet for express if it is not explicitly disabled
if (this.app.config.get('express.helmet') === false) {
this.app.config.set('web.middlewares.helmet', helmet(this.app.config.get('express.helmet')))
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/config/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const express = {
helmet: {}
}
4 changes: 3 additions & 1 deletion lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { spool } from './spool'
import { web } from './web'
import { session } from './session'
import { router } from './router'
import { express } from './express'
export {
spool,
web,
session,
router
router,
express
}
7 changes: 6 additions & 1 deletion lib/config/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export const web = {
const accept = req.get('accept') || ''

// respond with html page
if (accept.indexOf('html') !== -1 && req.app.get('view engine') && !req.wantsJSON) {
if (
accept.indexOf('html') !== -1
&& req.app.get('view engine')
&& !req.wantsJSON
) {
res.render('404', {
url: req.url,
error: req.error
Expand Down Expand Up @@ -142,6 +146,7 @@ export const web = {
'compression',
'methodOverride',
'www',
'helmet',
'router',
'404',
'500'
Expand Down
4 changes: 4 additions & 0 deletions lib/schemas/expressConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as joi from 'joi'

export const expressConfig = joi.object().keys({})
.unknown()
1 change: 1 addition & 0 deletions lib/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { expressConfig } from './expressConfig'
export { webConfig } from './webConfig'
12 changes: 12 additions & 0 deletions lib/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as joi from 'joi'
import { webConfig } from './schemas/webConfig'
import { expressConfig } from './schemas/expressConfig'

export const Validator = {
/**
Expand All @@ -12,6 +13,17 @@ export const Validator = {
return reject(new TypeError('config.web: ' + err))
}

return resolve(value)
})
})
},
validateExpress (config) {
return new Promise((resolve, reject) => {
joi.validate(config, expressConfig, (err, value) => {
if (err) {
return reject(new TypeError('config.express: ' + err))
}

return resolve(value)
})
})
Expand Down
Loading

0 comments on commit 62d90dd

Please sign in to comment.