Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

Commit

Permalink
added filters / middleware to routes
Browse files Browse the repository at this point in the history
added experimental package
added benchmarks
  • Loading branch information
bradrydzewski committed Oct 10, 2012
1 parent 2796b1e commit 5498e2e
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 225 deletions.
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

38 changes: 2 additions & 36 deletions README.md
Expand Up @@ -3,11 +3,11 @@ a simple http routing API for the Go programming language

go get github.com/drone/routes

this project combines the best of `web.go` and `pat.go`. It uses `pat.go`'s named url parameters (ie `:param`) and `web.go`'s regular expression groups for url matching and parameter extraction, which provides significant performance improvements.

for more information see:
http://gopkgdoc.appspot.com/pkg/github.com/bradrydzewski/routes

[![](https://drone.io/drone/routes/status.png)](https://drone.io/drone/routes/latest)

## Getting Started

package main
Expand Down Expand Up @@ -77,37 +77,3 @@ Helper function to serve Xml OR Json, depending on the value of the `Accept` hea
routes.ServeFormatted(w, r, &mystruct)
}

## Security
You can restrict access to routes by assigning an `AuthHandler` to a route.

Here is an example using a custom `AuthHandler` per route. Image we are doing some type of Basic authentication:

func authHandler(w http.ResponseWriter, r *http.Request) bool {
user := r.URL.User.Username()
password := r.URL.User.Password()
if user != "xxx" && password != "xxx" {
// if we wanted, we could do an http.Redirect here
return false
}
return true
}

mux.Get("/:param", handler).SecureFunc(authHandler)

If you plan to use the same `AuthHandler` to secure all of your routes, you may want to set the `DefaultAuthHandler`:

routes.DefaulAuthHandler = authHandler
mux.Get("/:param", handler).Secure()
mux.Get("/:param", handler).Secure()

### OAuth2
In the above examples, we implemented our own custom `AuthHandler`. Check out the [auth.go](https://github.com/bradrydzewski/auth.go) API which provides custom AuthHandlers for OAuth2 providers such as Google and Github.

## Logging
Logging is enabled by default, but can be disabled:

mux.Logging = false

You can also specify your logger:

mux.Logger = log.New(os.Stdout, "", 0)
78 changes: 78 additions & 0 deletions bench/bench_test.go
@@ -0,0 +1,78 @@
package bench

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/drone/routes"
gorilla "code.google.com/p/gorilla/mux"
"github.com/bmizerany/pat"
)

func HandlerOk(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world")
w.WriteHeader(http.StatusOK)
}

// Benchmark_Routes runs a benchmark against our custom Mux using the
// default settings.
func Benchmark_Routes(b *testing.B) {

handler := routes.New()
handler.Get("/person/:last/:first", HandlerOk)

for i := 0; i < b.N; i++ {
r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
}

}

// Benchmark_Web runs a benchmark against the pat.go Mux using the
// default settings.
func Benchmark_Pat(b *testing.B) {



m := pat.New()
m.Get("/person/:last/:first", http.HandlerFunc(HandlerOk))

for i := 0; i < b.N; i++ {
r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil)
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
}
}

// Benchmark_Gorilla runs a benchmark against the Gorilla Mux using
// the default settings.
func Benchmark_GorillaHandler(b *testing.B) {


handler := gorilla.NewRouter()
handler.HandleFunc("/person/{last}/{first}", HandlerOk)

for i := 0; i < b.N; i++ {
r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
}
}

// Benchmark_ServeMux runs a benchmark against the ServeMux Go function.
// We use this to determine performance impact of our library, when compared
// to the out-of-the-box Mux provided by Go.
func Benchmark_ServeMux(b *testing.B) {

mux := http.NewServeMux()
mux.HandleFunc("/", HandlerOk)

for i := 0; i < b.N; i++ {
r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
}
}
22 changes: 0 additions & 22 deletions demo/demo.go

This file was deleted.

1 change: 1 addition & 0 deletions exp/routes
Submodule routes added at 2796b1

0 comments on commit 5498e2e

Please sign in to comment.