Last active
January 3, 2016 23:09
-
-
Save garthk/8533351 to your computer and use it in GitHub Desktop.
hapi plugin to add routes directly from the composer manifest
This file contains hidden or 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
'use strict'; | |
exports.register = function (plugin, options, next) { | |
function addRoutes(server) { | |
var serverPluginSettings = server.settings.plugins || {}, | |
serverSpecificSettings = serverPluginSettings.routify || {}, | |
routes = serverSpecificSettings.routes || options.routes; | |
if (routes) { | |
server.route(routes); | |
} | |
} | |
var selection = plugin.select('routify'); | |
selection.servers.forEach(addRoutes); | |
next(); | |
}; |
This file contains hidden or 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
{ | |
"pack": { | |
"cache": "memory" | |
}, | |
"servers": [ | |
{ | |
"port": 8000, | |
"options": { | |
"labels": [ | |
"routify" | |
], | |
"plugins": { | |
"routify": { | |
"routes": [ | |
{ | |
"path": "/REST-API-ROOT/{args*}", | |
"method": "GET", | |
"handler": { | |
"proxy": { | |
"host": "usual-server.example.com", | |
"port": 80, | |
"protocol": "http", | |
"passThrough": true | |
} | |
}, | |
"config": { | |
"description": "proxy most requests to usual API server (not throttled)" | |
} | |
}, | |
{ | |
"path": "/REST-API-ROOT/that_specific_api/{args*}", | |
"method": "GET", | |
"handler": { | |
"proxy": { | |
"host": "0.0.0.0", | |
"port": 8001, | |
"protocol": "http", | |
"passThrough": true | |
} | |
}, | |
"config": { | |
"description": "proxy section_data requests to usual API server via the throttling proxy" | |
} | |
} | |
] | |
} | |
} | |
} | |
}, | |
{ | |
"port": 8001, | |
"options": { | |
"labels": [ | |
"routify" | |
], | |
"maxSockets": 5, | |
"plugins": { | |
"routify": { | |
"routes": [ | |
{ | |
"path": "/REST-API-ROOT/{args*}", | |
"method": "GET", | |
"handler": { | |
"proxy": { | |
"host": "usual-server.example.com", | |
"port": 80, | |
"protocol": "http", | |
"passThrough": true | |
} | |
}, | |
"config": { | |
"description": "proxy requests to usual API server (throttled)" | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
], | |
"plugins": { | |
"routify": {}, | |
"furball": {}, | |
"good": { | |
"extendedRequests": true, | |
"subscribers": { | |
"console": [ | |
"ops", | |
"request", | |
"log", | |
"error", | |
"status" | |
], | |
"./server.log": [ | |
"request", | |
"log" | |
] | |
} | |
}, | |
"poop": { | |
"logPath": "server-crash.log" | |
}, | |
"lout": {} | |
} | |
} |
With that manifest.json
, package.json
pointing main
to hapi-restify.js
, and use of npm link
to ensure you can require('routify')
:
npm install --save-dev hapi furball good poop lout
node_modules/.bin/hapi -c manifest.json
Hi @garthk and thanks for this gist.
I'm having a hard time understanding where your routify
package comes from. The routify
package has been unpublished, and I can't find an alternative suitable for Hapi.
Do you have any alternative way to define routes from a manifest?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
… then, in
manifest.json
, once you canrequire('routify')
from the node REPL:routify
toplugins
plugins.routify.routes
routify
toservers[n].options.labels
servers[n].plugins.routify.routes