Skip to content

Commit

Permalink
Merge pull request #15 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[feat] fix default port for http servers, fix helmet
  • Loading branch information
scott-wyatt authored May 8, 2019
2 parents 0e666e5 + 2738d54 commit e642820
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/ExpressSpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ExpressSpool extends ServerSpool {
// 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) {
if (this.app.config.get('express.helmet') !== false) {
this.app.config.set('web.middlewares.helmet', helmet(this.app.config.get('express.helmet')))
}
}
Expand Down Expand Up @@ -124,5 +124,11 @@ export class ExpressSpool extends ServerSpool {
if (!(this.app.routes instanceof Object)) {
throw new Error('Sanity Failed: app.routes is not an object!')
}
if (
this.app.config.get('express.helmet') !== false
&& !this.app.config.get('web.middlewares.helmet')
) {
throw new Error('Sanity Failed: Helmet was not set on web.middlware when configured at express.helmet')
}
}
}
11 changes: 7 additions & 4 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface Server {

export const Server: Server = {
BreakException: {},
port: null,
port: null, // process.env.PORT,
portHttp: null,
host: null,
ssl: null,
Expand All @@ -46,6 +46,7 @@ export const Server: Server = {
}

const server = express()

this.webConfig = Object.assign({}, app.config.get('web'))
this.middlewares = app.config.get('web.middlewares') || {}
this.middlewaresOrder = Object.values(app.config.get('web.middlewares.order') || [])
Expand Down Expand Up @@ -365,6 +366,7 @@ export const Server: Server = {
}
})
server.use(router)
return router
},

/**
Expand All @@ -388,7 +390,7 @@ export const Server: Server = {

const promises = Array.from(this.nativeServers.values())
.map((s) => {
return Server.listenPromise(s)
return Server.listenPromise(app, s)
})

return Promise.all(promises)
Expand Down Expand Up @@ -423,19 +425,20 @@ export const Server: Server = {
this.nativeServers.set('http', {
server: http.createServer(server),
host: this.host,
port: this.portHttp,
port: this.portHttp || this.port,
})
return resolve()
}
})
},

listenPromise(config: {host: string, port: number, server: any}) {
listenPromise(app: FabrixApp, config: {host: string, port: number, server: any}) {
return new Promise((resolve, reject) => {
config.server.listen(config.port, config.host, function (err) {
if (err) {
reject(err)
}
app.log.info(`express: ${config.host} listening on ${config.port}`)
resolve()
})
})
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-express",
"version": "1.6.2",
"version": "1.6.3",
"description": "Spool Express - Binds the routes compiled in spool-router to an Express 4 Server.",
"homepage": "https://fabrix.app",
"author": {
Expand Down

0 comments on commit e642820

Please sign in to comment.