Questions tagged [negroni]

Negroni is an idiomatic approach to web middleware in Go. It is tiny, non-intrusive, and encourages use of net/http Handlers.

Negroni is an idiomatic approach to web middleware in Go. It is tiny, non-intrusive, and encourages use of net/http Handlers.

The source code for Negroni can be found on GitHub.

The documentation can be found in the Negroni GoDoc Reference

21 questions
3
votes
1 answer

Passing arguments to Negroni middleware

Every request to my application should use some middleware. Using the Negroni docs I have implemented it like so: func MyMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { // do some stuff before next(rw, r) } and…
tommyd456
  • 10,443
  • 26
  • 89
  • 163
2
votes
1 answer

axios does not send POST to golang api

I have a golang api backend with a negroni middleware. I already implemented the CORS handler for negroni, so my api should allow cross origin resource sharing. // allow OPTIONS method of CORS requests c := cors.New(cors.Options{ AllowedOrigins:…
Andreas W
  • 225
  • 3
  • 11
2
votes
1 answer

Serving Subdirectories in HTTP handlers [GoLang]

I have the following code: r := mux.NewRouter() r.Handle("/", http.FileServer(http.Dir("./frontend/build/"))) r.Handle("/static", http.FileServer(http.Dir("./frontend/build/static/"))) r.PathPrefix("/api").Handler(auth) /api is supposed to be…
Nathan Hyland
  • 842
  • 2
  • 13
  • 28
2
votes
2 answers

Request context set in negroni middleware is lost in nested gorilla Subrouter

My basic main setup: muxRouter := mux.NewRouter() v1Router.Router(muxRouter.PathPrefix("/v1").Subrouter()) http.Handle("/", muxRouter) n := negroni.Classic() n.Use(negroni.HandlerFunc(apiRouter.Middleware)) n.UseHandler(muxRouter) s :=…
borislemke
  • 8,446
  • 5
  • 41
  • 54
2
votes
1 answer

Negroni: passing context from middleware to handlers

I'm trying to add a Gorilla Session to the Request Context in a Negroni middleware handler so that I can access it in my Gorilla Mux handlers. Here's a stripped down version of my code: // Session Middleware function func sessMid(w…
17xande
  • 2,430
  • 1
  • 24
  • 33
2
votes
1 answer

Can a custom HTTP handler be used globally when using Negroni or only per request?

To make sure error results are handled correctly across all requests I'm implementing a custom handler as described in http://blog.golang.org/error-handling-and-go. So instead of only accepting the w http.ResponseWriter, r *http.Request params the…
klotz
  • 1,951
  • 2
  • 22
  • 27
2
votes
1 answer

Negroni and Gorilla Context ClearHandler

Is is possible to use Gorilla's context.ClearHandler() as middleware for Negroni like I've seen it used as middleware for Alice? Something like: n.Use(context.ClearHandler()) At the moment I'm calling context.Clear(r) after every response but I…
tommyd456
  • 10,443
  • 26
  • 89
  • 163
2
votes
1 answer

Negroni Route Specific Middleware

I'm struggling to get my route specific middleware working with httprouter and Negroni. The login route requires Middleware2 and all the other routes require Middleware1. So far I have: func Main() { router := httprouter.New() …
tommyd456
  • 10,443
  • 26
  • 89
  • 163
1
vote
1 answer

httprouter and negroni for public and private routing middleware

I'm having a difficult time understanding how to use negroni and httprouter together. I have a couple of public routes, such as /api/v1/ping I have a bunch of private routes that need auth middleware, such as /api/v1/user If I want negroni Common…
user1354934
  • 8,139
  • 15
  • 50
  • 80
1
vote
0 answers

Negroni CORS Middleware only being called for OPTIONS request

I am building a web app in Golang, using mux as a router and Negroni for managing middleware for the API. I have the following middleware: Authentication middleware which checks that the client has a valid session and returns 401 if not Middleware…
1
vote
1 answer

Negroni continues to call other handlers after request is completed

My web-application in Go (using Gorilla mux and negroni) has about 20 handlers split into three groups based on what Middleware functions should be applied. Specifically: Group 1: Static requests (no middleware at all) GET /favicon.ico GET …
Dai
  • 141,631
  • 28
  • 261
  • 374
1
vote
1 answer

Serving index file when route not found under Negroni

I am using Golang, Negroni, and Gorilla mux for a web api server. I have my api routes under /api and I am using Negroni to serve static files from my /public directory using urls under /. I would like to serve my index.html file (containing a…
Kyle Goodwin
  • 253
  • 3
  • 12
1
vote
0 answers

Let middleware read response from http handler

I'm building a web service in Go. I use Negroni, unrolled/Render and Gorilla Mux. I'm trying to write some simple middleware myself for caching http responses and being completly new to Golang a few questions came up... Is there a way in which I…
Cyberlurk
  • 766
  • 3
  • 9
  • 30
0
votes
1 answer

Subroutes middlewares with Gorilla MUX and Negroni

I'm trying to add a middleware only on some routes. I wrote this code: func main() { router := mux.NewRouter().StrictSlash(false) admin_subrouter := router.PathPrefix("/admin").Subrouter() //handlers.CombinedLoggingHandler comes from…
Matias
  • 575
  • 5
  • 17
0
votes
1 answer

Can I use my function as `negroni` middleware

I have a function which I use as wrapper for every GET request: type HandlerFunc func(w http.ResponseWriter, req *http.Request) (interface{}, error) func WrapHandler(handler HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter,…
ceth
  • 44,198
  • 62
  • 180
  • 289
1
2