http是不是中间件_Go实战--Golang中http中间件

生命不止,继续go go go !!!

Middlewares(中间件)

wiki上对中间件的解释:
中间件(英语:Middleware),又译中间件,是提供系统软件和应用软件之间连接的软件,以便于软件各部件之间的沟通,特别是应用软件对于系统软件的集中的逻辑,在现代信息技术应用框架如Web服务、面向服务的体系结构等中应用比较广泛。

通俗的讲:
当你正在构建一个Web应用程序有可能要运行许多(甚至全部)的HTTP请求一些共享功能:
你可能想记录每一个request
gzip压缩的每个response
验证
等等
实现这个共享功能的一种方法是将其设置为中间件。

Middleware in Webservice
If you have some code that needs to be run for some common requests, you need some way to stack on top of each other and run them in sequence. This problem is solved elegantly through middleware packages.
看看上面的英文描述,是不是更清晰一点点了。

Go语言中实现和使用middleware是非常简单

  • Implement our middleware so that it satisfies the http.Handler interface.(使我们的中间件能搞满足 http.handlers 这个接口)
  • Build up a chain of handlers containing both our middleware handler and our normal application handler, which we can register with a http.ServeMux.(建立一个 handlers 链,使其能够满足中间件的 handler 和 正常应用的 handler,并且能够注册到 http.ServeMux)

如果不了解golang总的http handler,可以参考:
A Recap of Request Handling in Go

  • ServeMux
    A ServeMux is essentially a HTTP request router (or multiplexor). It compares incoming requests against a list of predefined URL paths, and calls the associated handler for the path whenever a match is found.
  • Handlers
    Handlers are responsible for writing response headers and bodies. Almost any object can be a handler, so long as it satisfies the http.Handler interface.

一个带有多个中间件的例子

package mainimport (    "log"    "net/http")func middlewareOne(next http.Handler) http.Handler {    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {        log.Println("Executing middlewareOne")        next.ServeHTTP(w, r)        log.Println("Executing middlewareOne again")    })}func middlewareTwo(next http.Handler) http.Handler {    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {        log.Println("Executing middlewareTwo")        if r.URL.Path != "/" {            return        }        next.ServeHTTP(w, r)        log.Println("Executing middlewareTwo again")    })}func final(w http.ResponseWriter, r *http.Request) {    log.Println("Executing finalHandler")    w.Write([]byte("OK"))}func main() {    finalHandler := http.HandlerFunc(final)    http.Handle("/", middlewareOne(middlewareTwo(finalHandler)))    http.ListenAndServe(":8080", nil)}1234567891011121314151617181920212223242526272829303132333435363738

浏览器访问:http://localhost:8080/
结果:
2018/02/01 13:04:48 Executing middlewareOne
2018/02/01 13:04:48 Executing middlewareTwo
2018/02/01 13:04:48 Executing finalHandler
2018/02/01 13:04:48 Executing middlewareTwo again
2018/02/01 13:04:48 Executing middlewareOne again

使用中间件检查请求体的存在

package mainimport (    "bytes"    "net/http")func enforceXMLHandler(next http.Handler) http.Handler {    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {        // Check for a request body        if r.ContentLength == 0 {            http.Error(w, http.StatusText(400), 400)            return        }        // Check its MIME type        buf := new(bytes.Buffer)        buf.ReadFrom(r.Body)        if http.DetectContentType(buf.Bytes()) != "text/xml; charset=utf-8" {            http.Error(w, http.StatusText(415), 415)            return        }        next.ServeHTTP(w, r)    })}func main() {    finalHandler := http.HandlerFunc(final)    http.Handle("/", enforceXMLHandler(finalHandler))    http.ListenAndServe(":8080", nil)}func final(w http.ResponseWriter, r *http.Request) {    w.Write([]byte("OK"))}1234567891011121314151617181920212223242526272829303132333435

使用中间件写日志

package mainimport (    "fmt"    "log"    "net/http"    "time")func loggingHandle(next http.Handler) http.Handler {    fn := func(w http.ResponseWriter, r *http.Request) {        t1 := time.Now()        next.ServeHTTP(w, r)        t2 := time.Now()        log.Printf("[%s] %q %v", r.Method, r.URL.String(), t2.Sub(t1))    }    return http.HandlerFunc(fn)}func aboutHandler(w http.ResponseWriter, r *http.Request) {    fmt.Fprintf(w, "you are on about page")}func main() {    aboutHandler := http.HandlerFunc(aboutHandler)    http.Handle("/", loggingHandle(aboutHandler))    http.ListenAndServe(":8080", nil)}1234567891011121314151617181920212223242526272829

浏览器访问:http://localhost:8080/
结果:
2018/02/01 13:16:56 [GET] “/” 499.5µs

goji/httpauth

HTTP Authentication middlewares

获取:
go get github.com/goji/httpauth

package mainimport (    "net/http"    "github.com/goji/httpauth")func main() {    finalHandler := http.HandlerFunc(final)    authHandler := httpauth.SimpleBasicAuth("username", "password")    http.Handle("/", authHandler(finalHandler))    http.ListenAndServe(":8080", nil)}func final(w http.ResponseWriter, r *http.Request) {    w.Write([]byte("OK"))}1234567891011121314151617181920

curl测试:

curl --i username:password@localhost:8080HTTP/1.1 200 OKDate: Thu, 01 Feb 2018 05:27:33 GMTContent-Length: 2Content-Type: text/plain; charset=utf-8OK1234567

gorilla/handlers

请参考:
Go实战–Gorilla web toolkit使用之gorilla/handlers

package mainimport (  "github.com/gorilla/handlers"  "net/http"  "os")func main() {  finalHandler := http.HandlerFunc(final)  logFile, err := os.OpenFile("server.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)  if err != nil {    panic(err)  }  http.Handle("/", handlers.LoggingHandler(logFile, finalHandler))  http.ListenAndServe(":3000", nil)}func final(w http.ResponseWriter, r *http.Request) {  w.Write([]byte("OK"))}1234567891011121314151617181920212223

justinas/alice

Painless middleware chaining for Go

github地址:
https://github.com/justinas/alice

获取:
go get github.com/justinas/alice

To start you call New() passing in each handler you want to execute, in the order that you want them to execute. That returns a structure off which you will call Then(), which tells Alice the final handler that must execute, which needs to be our router. This sample assumes the logger middleware function we setup previously is in a directory named middleware.

func NewHttpServer(address string, port int) *http.Server {   router := setupHttpRouter()   server := alice.New(middleware.Logger).Then(router)   listener := &http.Server{      Addr:    fmt.Sprintf("%s:%d", address, port),      Handler: server,   }   return listener}123456789101112

urfave/negroni

Idiomatic HTTP Middleware for Golang

github地址:
https://github.com/urfave/negroni

获取:
go get github.com/urfave/negroni

negroni.Classic()
provides some default middleware that is useful for most applications:

  • negroni.Recovery - Panic Recovery Middleware.
  • negroni.Logger - Request/Response Logger Middleware.
  • negroni.Static - Static File serving under the “public” directory.

官方例子:

package mainimport (    "fmt"    "net/http"    "github.com/urfave/negroni")func main() {    mux := http.NewServeMux()    mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {        fmt.Fprintf(w, "Welcome to the home page!")    })    n := negroni.Classic() // Includes some default middlewares    n.UseHandler(mux)    http.ListenAndServe(":3000", n)}123456789101112131415161718192021

关于negroni,之后会更加详细的介绍。

6d1524e7676d8ff63bbd01ce3462db75.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值