diff options
| author | Felix Hanley <felix@userspace.com.au> | 2016-05-12 13:28:05 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2016-05-12 13:28:05 +0000 |
| commit | e1c3d6f7db06d592538f1162b2b6b9f1b6efa330 (patch) | |
| tree | c26ee9ad763a2bc48cde0e2c46f831a40f55ed28 /src/vendor/github.com/rs/xhandler/chain_example_test.go | |
| parent | bb8476ffe78210d1a4c0b4bbee6da99da1bc15c5 (diff) | |
| download | go-dict2rest-e1c3d6f7db06d592538f1162b2b6b9f1b6efa330.tar.gz go-dict2rest-e1c3d6f7db06d592538f1162b2b6b9f1b6efa330.tar.bz2 | |
Shuffle vendor directory up a level o_0
Diffstat (limited to 'src/vendor/github.com/rs/xhandler/chain_example_test.go')
| -rw-r--r-- | src/vendor/github.com/rs/xhandler/chain_example_test.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/vendor/github.com/rs/xhandler/chain_example_test.go b/src/vendor/github.com/rs/xhandler/chain_example_test.go new file mode 100644 index 0000000..005b5b2 --- /dev/null +++ b/src/vendor/github.com/rs/xhandler/chain_example_test.go @@ -0,0 +1,52 @@ +package xhandler_test + +import ( + "fmt" + "net/http" + "strings" + "time" + + "github.com/rs/cors" + "github.com/rs/xhandler" + "golang.org/x/net/context" +) + +func ExampleChain() { + c := xhandler.Chain{} + // Append a context-aware middleware handler + c.UseC(xhandler.CloseHandler) + + // Mix it with a non-context-aware middleware handler + c.Use(cors.Default().Handler) + + // Another context-aware middleware handler + c.UseC(xhandler.TimeoutHandler(2 * time.Second)) + + mux := http.NewServeMux() + + // Use c.Handler to terminate the chain with your final handler + mux.Handle("/", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Welcome to the home page!") + }))) + + // You can reuse the same chain for other handlers + mux.Handle("/api", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Welcome to the API!") + }))) +} + +func ExampleIf() { + c := xhandler.Chain{} + + // Add timeout handler only if the path match a prefix + c.UseC(xhandler.If( + func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool { + return strings.HasPrefix(r.URL.Path, "/with-timeout/") + }, + xhandler.TimeoutHandler(2*time.Second), + )) + + http.Handle("/", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Welcome to the home page!") + }))) +} |
