Skip to content

Instantly share code, notes, and snippets.

@garthk
Last active January 3, 2016 23:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garthk/8533351 to your computer and use it in GitHub Desktop.
Save garthk/8533351 to your computer and use it in GitHub Desktop.
hapi plugin to add routes directly from the composer manifest
'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();
};
{
"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": {}
}
}
@garthk
Copy link
Author

garthk commented Jan 21, 2014

… then, in manifest.json, once you can require('routify') from the node REPL:

  • add routify to plugins
  • set any global routes in plugins.routify.routes
  • enable the plugin on a server by adding routify to servers[n].options.labels
  • set any server-specific routes in servers[n].plugins.routify.routes

@garthk
Copy link
Author

garthk commented Jan 21, 2014

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

@garthk
Copy link
Author

garthk commented Jan 21, 2014

WARNINGS:

The throttling above won't work until hapi#1346 is fixed.

You can't install lout normally until lout#39 is fixed; npm install --save-dev 'git://github.com/spumko/lout.git#master' instead.

@MattiSG
Copy link

MattiSG commented Nov 17, 2014

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