Skip to content

Commit

Permalink
Merge pull request #14 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[feat] cleanup
  • Loading branch information
scott-wyatt authored May 8, 2019
2 parents 67852c1 + 956d06c commit 0e666e5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/ExpressSpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class ExpressSpool extends ServerSpool {
*/
async initialize () {

this.server = Server.createServer(this.app)
this.server = Server.createExpressServer(this.app)

return Promise.all([
Server.registerMiddlewares(this.app, this.server),
Expand Down
92 changes: 57 additions & 35 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@ import * as consolidate from 'consolidate'
import * as expressBoom from 'express-boom'


export const Server = {
export interface Server {
[key: string]: any
nativeServers: Map<string, {host: string, port: number, server: any}>
}

export const Server: Server = {
BreakException: {},
port: null,
portHttp: null,
host: null,
ssl: null,
redirectToHttps: false,
// nativeServer: null,
nativeServers: new Map(), // : Map<string, http>,
nativeServers: new Map(),
serverRoutes: {},
serverPolicies: {},
serverHandlers: {},
webConfig: {},
middlewares: {},
middlewaresOrder: [],

createServer(app: FabrixApp) {
createExpressServer(app: FabrixApp) {
const main = app.config.get('main')
const sess = app.config.get('session')
const express = app.config.get('web.express')
Expand Down Expand Up @@ -80,6 +85,7 @@ export const Server = {
this.middlewares.addMethods = (req: Request, res: Response, next) => {
req.log = app.log
req.fabrixApp = app

const accept = req.get('accept') || ''
req.wantsJSON = accept.indexOf('json') !== -1

Expand All @@ -99,36 +105,48 @@ export const Server = {
return str
}

res.serverError = err => {
this.middlewares['500'](err, req, res, next)
// Helper 500
if (!res.serverError) {
res.serverError = err => {
this.middlewares['500'](err, req, res, next)
}
}

res.notFound = () => {
this.middlewares['404'](req, res, next)
// Helper 404
if (!res.notFound) {
res.notFound = () => {
this.middlewares['404'](req, res, next)
}
}

res.forbidden = (msg) => {
res.serverError({
statusCode: 403,
code: 'E_FORBIDDEN',
message: msg || 'forbidden'
})
// Helper 403
if (!res.forbidden) {
res.forbidden = (msg) => {
res.serverError({
statusCode: 403,
code: 'E_FORBIDDEN',
message: msg || 'forbidden'
})
}
}

res.paginate = (count, limit = 0, offset = 0, sort = []) => {
limit = Number(limit)
offset = Number(offset)

const pages = Math.ceil(count / limit) === 0 ? 1 : Math.ceil(count / limit)
const page = Math.round(((offset + limit) / limit))

res.set('X-Pagination-Total', count)
res.set('X-Pagination-Pages', pages.toString())
res.set('X-Pagination-Page', page.toString())
res.set('X-Pagination-Offset', offset.toString())
res.set('X-Pagination-Limit', limit.toString())
res.set('X-Pagination-Sort', Server.sortToString(sort))
return res
// Helper Paginate Utility
if (!res.paginate) {
res.paginate = (count, limit = 0, offset = 0, sort = []) => {
limit = Number(limit)
offset = Number(offset)

const pages = Math.ceil(count / limit) === 0 ? 1 : Math.ceil(count / limit)
const page = Math.round(((offset + limit) / limit))

res.set('X-Pagination-Total', count)
res.set('X-Pagination-Pages', pages.toString())
res.set('X-Pagination-Page', page.toString())
res.set('X-Pagination-Offset', offset.toString())
res.set('X-Pagination-Limit', limit.toString())
res.set('X-Pagination-Sort', Server.sortToString(sort))
return res
}
}

next()
Expand All @@ -138,6 +156,10 @@ export const Server = {
return server
},

/**
* This shouldn't be here!
* @param sort
*/
sortToString(sort = []) {
if (typeof sort === 'string') {
return sort
Expand Down Expand Up @@ -364,10 +386,10 @@ export const Server = {

init(app, server)

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

return Promise.all(promises)
},
Expand All @@ -392,7 +414,7 @@ export const Server = {
this.nativeServers.set('http', {
server: http.createServer(server),
host: this.host,
port: this.portHttp,
port: this.portHttp || this.port,
})
}
return resolve()
Expand All @@ -408,9 +430,9 @@ export const Server = {
})
},

listenPromise(server) {
listenPromise(config: {host: string, port: number, server: any}) {
return new Promise((resolve, reject) => {
server.server.listen(server.port, server.host, function (err) {
config.server.listen(config.port, config.host, function (err) {
if (err) {
reject(err)
}
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@fabrix/spool-express",
"version": "1.6.1",
"description": "Spool Express - Binds the routes compiled in spool-router to a Express 4 Server.",
"version": "1.6.2",
"description": "Spool Express - Binds the routes compiled in spool-router to an Express 4 Server.",
"homepage": "https://fabrix.app",
"author": {
"name": "Scott Wyatt",
Expand Down Expand Up @@ -89,7 +89,7 @@
"pug": "^2.0.1",
"smokesignals": "^3",
"sqlite3": "^4.0.2",
"supertest": "^3.4.2",
"supertest": "^4.0.2",
"tslib": "~1.9.0",
"tslint": "~5.10.0",
"tslint-microsoft-contrib": "~5.0.3",
Expand Down

0 comments on commit 0e666e5

Please sign in to comment.