You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xhandler/xhandler_example_test.go

69 lines
1.6 KiB

package xhandler_test
import (
"log"
"net/http"
"time"
"context"
"git.freecumextremist.com/themusicgod1/xhandler"
)
type key int
const contextKey key = 0
func newContext(ctx context.Context, value string) context.Context {
return context.WithValue(ctx, contextKey, value)
}
func fromContext(ctx context.Context) (string, bool) {
value, ok := ctx.Value(contextKey).(string)
return value, ok
}
func ExampleHandle() {
var xh xhandler.HandlerC
xh = xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
value, _ := fromContext(ctx)
w.Write([]byte("Hello " + value))
})
xh = (func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ctx = newContext(ctx, "World")
next.ServeHTTPC(ctx, w, r)
})
})(xh)
ctx := context.Background()
// Bridge context aware handlers with http.Handler using xhandler.Handle()
http.Handle("/", xhandler.New(ctx, xh))
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
func ExampleHandleTimeout() {
var xh xhandler.HandlerC
xh = xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
if _, ok := ctx.Deadline(); ok {
w.Write([]byte(" with deadline"))
}
})
// This handler adds a timeout to the handler
xh = xhandler.TimeoutHandler(5 * time.Second)(xh)
ctx := context.Background()
// Bridge context aware handlers with http.Handler using xhandler.Handle()
http.Handle("/", xhandler.New(ctx, xh))
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}