Go实战--Golang中http中间件(goji/httpauth、urfave/negroni、gorilla/handlers、justinas/alice) - Go语言中文社区

Go实战--Golang中http中间件(goji/httpauth、urfave/negroni、gorilla/handlers、justinas/alice)


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

不知不觉,2018年第一个月份已经过完,看了看一月份uv统计:
北京 9887
深圳 5088
上海 4767

杭州 2834
广州 2559

成都 2059
武汉 1536
南京 1444

西安 982

言归正传!

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 main

import (
    "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)
}

浏览器访问: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 main

import (
    "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"))
}

使用中间件写日志

package main

import (
    "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 %vn", 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)
}

浏览器访问: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 main

import (
    "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"))
}

curl测试:

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

OK

gorilla/handlers

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

package main

import (
  "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"))
}

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
}

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 main

import (
    "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)
}

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

这里写图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/wangshubo1989/article/details/79227443
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-13 12:26:58
  • 阅读 ( 1243 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢