示例#1
0
// AddRoutes adds routes to a router instance. If there are middlewares defined
// for a route, a new negroni app is created and wrapped as a http.Handler
func AddRoutes(routes []Route, router *mux.Router) {
	var (
		handler http.Handler
		n       *negroni.Negroni
	)

	for _, route := range routes {
		// Add any specified middlewares
		if len(route.Middlewares) > 0 {
			n = negroni.New()

			for _, middleware := range route.Middlewares {
				n.Use(middleware)
			}

			// Wrap the handler in the negroni app with middlewares
			n.Use(negroni.Wrap(route.HandlerFunc))
			handler = n
		} else {
			handler = route.HandlerFunc
		}

		router.Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)
	}
}
示例#2
0
func BonitosrvQuery(n *negroni.Negroni, method string,
	url string, data io.Reader) *httptest.ResponseRecorder {

	request, _ := http.NewRequest(method, url, data)
	response := httptest.NewRecorder()
	n.ServeHTTP(response, request)
	return response
}
示例#3
0
// Configure creates a default newrelic client and adds middleware and such
func Configure(n *negroni.Negroni) error {
	cfg, err := config.LoadEnv()
	if err != nil {
		return err
	}

	ctl := control.NewAgentController(cfg)

	n.Use(NewRelicMiddleware(ctl, cfg))
	return nil
}
示例#4
0
// Register wires up all middleware for the web app
func Register(n *negroni.Negroni) {
	n.Use(negroni.NewRecovery())
	n.Use(negroni.NewLogger())
}
示例#5
0
文件: inject.go 项目: Xe/middleware
// Inject adds x-request-id and x-forwarded-for support to an existing negroni instance.
func Inject(n *negroni.Negroni) {
	n.Use(negroni.HandlerFunc(xff.XFF))
	n.Use(xrequestid.New(26))
}