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 %v\n",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",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",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,之后会更加详细的介绍。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


类型转换 1、int转string 2、string转int 3、string转float 4、用户结构类型转换
package main import s "strings" import "fmt" var p = fmt.Println func main() { p("Contains: ", s.Contains("test&quo
类使用:实现一个people中有一个sayhi的方法调用功能,代码如下: 接口使用:实现上面功能,代码如下:
html代码: beego代码:
1、读取文件信息: 2、读取文件夹下的所有文件: 3、写入文件信息 4、删除文件,成功返回true,失败返回false
配置环境:Windows7+推荐IDE:LiteIDEGO下载地址:http://www.golangtc.com/downloadBeego开发文档地址:http://beego.me/docs/intro/ 安装步骤: 一、GO环境安装 二、配置系统变量 三、Beego安装 一、GO环境安装 根
golang获取程序运行路径:
Golang的文档和社区资源:为什么它可以帮助开发人员快速上手?
Golang:AI 开发者的实用工具
Golang的标准库:为什么它可以大幅度提高开发效率?
Golang的部署和运维:如何将应用程序部署到生产环境中?
高性能AI开发:Golang的优势所在
本篇文章和大家了解一下go语言开发优雅得关闭协程的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。1.简介本文将介绍首先为什么需要主...
这篇文章主要介绍了Go关闭goroutine协程的方法,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。1.简介本文将介绍首先为什么需要主动关闭gor...
本篇文章和大家了解一下go关闭GracefulShutdown服务的几种方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。目录Shutdown方法Regi...
这篇文章主要介绍了Go语言如何实现LRU算法的核心思想和实现过程,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。GO实现Redis的LRU例子常
今天小编给大家分享的是Go简单实现多租户数据库隔离的方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会...
这篇“Linux系统中怎么安装NSQ的Go语言客户端”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希
本文小编为大家详细介绍“怎么在Go语言中实现锁机制”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么在Go语言中实现锁机制”文章能帮助大家解决疑惑,下面...
今天小编给大家分享一下Go语言中interface类型怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考