Skip to content

Commit 7a36e3a

Browse files
committed
Example for negroni
1 parent 52c18ae commit 7a36e3a

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

examples/negroni/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/codegangsta/negroni"
7+
"github.com/gorilla/mux"
8+
"github.com/sebest/xff"
9+
)
10+
11+
func main() {
12+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13+
w.Write([]byte("hello from " + r.RemoteAddr + "\n"))
14+
})
15+
16+
mux := mux.NewRouter()
17+
mux.Handle("/", handler)
18+
19+
n := negroni.Classic()
20+
n.Use(negroni.HandlerFunc(xff.XFF))
21+
n.UseHandler(mux)
22+
n.Run(":3000")
23+
}

examples/server.go renamed to examples/nethttp/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ func main() {
1111
w.Write([]byte("hello from " + r.RemoteAddr + "\n"))
1212
})
1313

14-
http.ListenAndServe(":8080", xff.Handler(handler))
14+
http.ListenAndServe(":3000", xff.Handler(handler))
1515
}

xff.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,13 @@ func Handler(h http.Handler) http.Handler {
6565
})
6666
}
6767

68-
// XFF equals Handler for backward compatibility.
69-
var XFF = Handler
68+
// HandlerFunc is a Martini compatible handler
69+
func HandlerFunc(w http.ResponseWriter, r *http.Request) {
70+
r.RemoteAddr = GetRemoteAddr(r)
71+
}
72+
73+
// XFF is a Negroni compatible interface
74+
func XFF(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
75+
r.RemoteAddr = GetRemoteAddr(r)
76+
next(w, r)
77+
}

0 commit comments

Comments
 (0)