本文整理汇总了Golang中github.com/rs/xhandler.Chain.Use方法的典型用法代码示例。如果您正苦于以下问题:Golang Chain.Use方法的具体用法?Golang Chain.Use怎么用?Golang Chain.Use使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/rs/xhandler.Chain
的用法示例。
在下文中一共展示了Chain.Use方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExampleNewHandler
func ExampleNewHandler() {
c := xhandler.Chain{}
// Install the metric handler with dogstatsd backend client and some env tags
flushInterval := 5 * time.Second
tags := []string{"role:my-service"}
statsdWriter, err := net.Dial("udp", "127.0.0.1:8126")
if err != nil {
log.Fatal(err)
}
c.Use(xstats.NewHandler(dogstatsd.New(statsdWriter, flushInterval), tags))
// Here is your handler
h := c.HandlerH(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the xstats request's instance from the context. You can safely assume it will
// be always there, if the handler is removed, xstats.FromContext will return a nop
// instance.
m := xstats.FromRequest(r)
// Count something
m.Count("requests", 1, "route:index")
}))
http.Handle("/", h)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
示例2: router
func router(a *server) http.Handler {
mux := xmux.New()
c := xhandler.Chain{}
c.Use(mwLogger)
c.Use(mwAuthenticationCheck(a.key))
mux.GET("/sites", c.HandlerCF(xhandler.HandlerFuncC(a.handleAllSites)))
mux.GET("/sites/:id", c.HandlerCF(xhandler.HandlerFuncC(a.handleSingleSite)))
mux.GET("/torrents", c.HandlerCF(xhandler.HandlerFuncC(a.handleTorrents)))
mux.POST("/download/:hash", c.HandlerCF(xhandler.HandlerFuncC(a.handleDownload)))
return xhandler.New(context.Background(), mux)
}
示例3: ExampleChain
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!")
})))
}
示例4: main
func main() {
c := xhandler.Chain{}
c.Use(recoverMiddleware)
c.Use(normalLoggingMiddleware)
c.Use(log15LoggingMiddleware)
c.Use(logrusLoggingMiddleware)
simpleHandler := xhandler.HandlerFuncC(simple)
accountHandler := xhandler.HandlerFuncC(account)
noteHandler := xhandler.HandlerFuncC(note)
mux := bone.New()
mux.Get("/account/:id", c.Handler(accountHandler))
mux.Get("/note/:id", c.Handler(noteHandler))
mux.Get("/simple", c.Handler(simpleHandler))
http.ListenAndServe(":8080", mux)
}