import Hapi from 'hapi'
import Path from 'path'
import controllers from './controllers/index'
import _ from './extensions'
import Lazy from 'lazy.js'


spawnServer('App', 3000, controllers, {
    engines: {
        hbs: require('handlebars')
    },
    path: Path.join(__dirname, './views'),
    layoutPath: Path.join(__dirname, './views/layouts'),
    layout: 'layout'
})

function spawnServer(name, port, controllers, viewOptions) {

    var server = new Hapi.Server()

    server.connection({
        port
    })

	if (viewOptions)
		server.views(viewOptions);

    controllers(server)

    server.start(() => {
        console.log(name + ' server running at:', server.info.uri)
        outputRoutes(server)
    })
}

function outputRoutes(server) {
    console.log('Available Routes:')
    Lazy(server.table()[0].table)
        .map(route => route.method + ' ' + route.path)
        .sort()
        .each(e => console.log(e))
}

import Hapi from 'hapi'
import Path from 'path'
import controllers from './controllers/index'
import _ from './extensions'
import Lazy from 'lazy.js'


spawnServer('App', 3000, controllers, {
    engines: {
        hbs: require('handlebars')
    },
    path: Path.join(__dirname, './views'),
    layoutPath: Path.join(__dirname, './views/layouts'),
    layout: 'layout'
})

function spawnServer(name, port, controllers, viewOptions) {

    var server = new Hapi.Server()

    server.connection({
        port
    })

	if (viewOptions)
		server.views(viewOptions);

    server.ext('onRequest', function (request, reply) {
        var methodOverride = request.query['hapi_method'];

        if (methodOverride) {
            request.setMethod(methodOverride);
        }

        return reply.continue();
    });

    controllers(server)

    server.start(() => {
        console.log(name + ' server running at:', server.info.uri)
        outputRoutes(server)
    })
}

function outputRoutes(server) {
    console.log('Available Routes:')
    Lazy(server.table()[0].table)
        .map(route => route.method + ' ' + route.path)
        .sort()
        .each(e => console.log(e))
}

Add hapi extension to redirect route. New behavior looks at query string for keyword 'hapi_method'. The value will override the current route-method. This is currently used to map http-POST requests to PUT-defined routes. Ideally, a form-value would be used to do the same thing, but there does not appear to be a way to read form-values early in the request pipeline.
Add hapi extension to redirect route.
New behavior looks at query string for keyword 'hapi_method'. The value will override the current route-method.
This is currently used to map http-POST requests to PUT-defined routes.
Ideally, a form-value would be used to do the same thing, but there does not appear to be a way to read
form-values early in the request pipeline.

Hapi authors does not too keen to add this feature: https://github.com/hapijs/hapi/issues/1217
