Documentation ¶
Overview ¶
Package chain aids the composition of nested http.Handler instances.
Example ¶
// Nested handlers write either "0" or "1" to the response body before // and after ServeHTTP() is called. // // endHandler writes "_END_" to the response body. ch := chain.New(nestedHandler0, nestedHandler1) ch = ch.Append(nestedHandler0) mux := http.NewServeMux() mux.Handle("/010_End", ch.EndFn(endHandler)) server := httptest.NewServer(mux) resp, err := respBody(server.URL + "/010_End") if err != nil { fmt.Println(err) } fmt.Println("Chain 010 Resp:", resp)
Output: Chain 010 Resp: 010_END_010
Example (Advanced) ¶
// Nested handlers write either "0" or "1" to the response body before // and after ServeHTTP() is called. // // endHandler writes "_END_" to the response body. ch00 := chain.New(nestedHandler0, nestedHandler0) ch001 := ch00.Append(nestedHandler1) ch1 := chain.New(nestedHandler1) ch1001 := ch1.Merge(ch001) mux := http.NewServeMux() mux.Handle("/00_End", ch00.EndFn(endHandler)) mux.Handle("/001_End", ch001.EndFn(endHandler)) mux.Handle("/1001_End", ch1001.EndFn(endHandler)) server := httptest.NewServer(mux) resp00, err := respBody(server.URL + "/00_End") if err != nil { fmt.Println(err) } resp001, err := respBody(server.URL + "/001_End") if err != nil { fmt.Println(err) } resp1001, err := respBody(server.URL + "/1001_End") if err != nil { fmt.Println(err) } fmt.Println("Chain 00 Resp:", resp00) fmt.Println("Chain 001 Resp:", resp001) fmt.Println("Chain 1001 Resp:", resp1001)
Output: Chain 00 Resp: 00_END_00 Chain 001 Resp: 001_END_100 Chain 1001 Resp: 1001_END_1001
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain contains the current nested http.Handler data.
func (*Chain) Append ¶
Append receives one or more nested http.Handler instances, and appends the value to the returned Chain.
func (*Chain) Copy ¶
Copy receives one Chain instance, and copies it's handlers into the receiver's handlers slice.
func (*Chain) End ¶
End receives an http.Handler, and returns an http.Handler comprised of all nested http.Handler data where the received http.Handler is the endpoint.
Click to show internal directories.
Click to hide internal directories.