package module

import (
	"net/http"
	"strings"

	"github.com/gorilla/mux"
	"github.com/urfave/negroni"
)

// NegroniRoute helps with using middlewares, found on the interwebz; called by modules when registering APIs
func NegroniRoute(m *mux.Router, base string, path string, pathTypes string, f func(http.ResponseWriter, *http.Request), mids ...func(http.ResponseWriter, *http.Request, http.HandlerFunc)) {
	_routes := mux.NewRouter()
	_routes.HandleFunc(base+path, f).Methods(strings.Split(pathTypes, " ")...)

	_n := negroni.New()
	for i := range mids {
		_n.Use(negroni.HandlerFunc(mids[i]))
	}
	_n.UseHandler(_routes)
	m.Handle(path, _n)
}