func ExampleAddChain() { c := xhandler.Chain{} close := xhandler.CloseHandler cors := cors.Default().Handler timeout := xhandler.TimeoutHandler(2 * time.Second) auth := func(next xhandler.HandlerC) xhandler.HandlerC { return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { if v := ctx.Value("Authorization"); v == nil { http.Error(w, "Not authorized", http.StatusUnauthorized) return } next.ServeHTTPC(ctx, w, r) }) } c.Add(close, cors, timeout) 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!") }))) // Create a new chain from an existing one, and add route-specific middleware to it protected := c.With(auth) mux.Handle("/admin", protected.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "protected endpoint!") }))) }