加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang Http Handlers as Middleware

发布时间:2020-12-16 18:58:31 所属栏目:大数据 来源:网络整理
导读:From: http://capotej.com/ 转者按:本文介绍了如何hook一个http的处理函数,从而加入自定义的内容。 Most modern web stacks allow the “filtering” of requests via stackable/composable middleware,allowing you to cleanly separate cross-cutting con
From: http://capotej.com/

转者按:本文介绍了如何hook一个http的处理函数,从而加入自定义的内容。


Most modern web stacks allow the “filtering” of requests via stackable/composable middleware,allowing you to cleanly separate cross-cutting concerns from your web application. This weekend I needed to hook into go’s http.FileServerand was pleasantly surprised how easy it was to do.

Let’s start with a basic file server for/tmp:

main.go
1
2
3
func main() {  http.ListenAndServe(":8080", FileServer(Dir"/tmp"))) } 

This starts up a local file server at :8080. How can we hook into this so we can run some code before file requests are served? Let’s look at the method signature forhttp.ListenAndServe:

1
addr stringhandler Handler) error

So it looks likehttp.FileServerreturns aHandlerthat knows how to serve files given a root directory. Now let’s look at theHandlerinterface:

type Handler interface ServeHTTPResponseWriter*Request)  Because of go’s granular interfaces,any object can be aHandlerso long as it implementsServeHTTP. It seems all we need to do is construct our ownHandlerthat wrapshttp.FileServer’s handler. There’s a built in helper for turning ordinary functions into handlers calledhttp.HandlerFunc:

HandlerFunc func)

Then we just wraphttp.FileServerlike so:

main.go
1
2
3
4
5
6
7
8
9
10
11
12
OurLoggingHandlerh Handler {  return HandlerFunc(w r ) fmtPrintln(*rURL)  hw)  }) }  fileHandler := ))  wrappedHandler fileHandlerwrappedHandler Go has a bunch of other builtinhandlerslikeTimeoutHandlerandRedirectHandlerthat can be mixed and matched the same way.


另一种方式参考https://github.com/philsong/golang_samples/blob/master/src/emvdecoder/emvdecoder.go

type TraceHandler struct {
        h http.Handler
        n int
}

func (r *TraceHandler) ServeHTTP(w http.ResponseWriter,req *http.Request) {
        r.n++
        fmt.Printf("counter = %dn",r.n) //why counter always zero
        fmt.Println("get",req.URL.Path," from ",req.RemoteAddr)
        r.h.ServeHTTP(w,req)
}

func main() {
        port := "9090" //Default port
        if len(os.Args) > 1 {
                port = strings.Join(os.Args[1:2],"")
        }
        h := http.StripPrefix("/icclogs/",http.FileServer(http.Dir("./logs/")))
        http.Handle("/icclogs/",&TraceHandler{h: h,n: 0})

        println("Listening on port ",port,"...")
        err := http.ListenAndServe(":"+port,nil) //设置监听的端口

        if err != nil {
                log.Fatal("ListenAndServe: ",err)
        }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!