自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go語言的Http 中間件實現(xiàn)

網(wǎng)絡 網(wǎng)絡管理
這篇文章,我會給大家介紹怎么自己去實現(xiàn)一個自定義的middleware模式。以及通過使用第三方的中間件軟件包的一些具體的實例。

當你正在構建一個Web應用程序有可能要運行許多(甚至全部)的HTTP請求一些共享功能,你可能想記錄每一個request,gzip壓縮的每個response,或者做一些繁重的處理或者緩存檢查。

實現(xiàn)這個共享功能的一種方法是將其設置為中間件,他可以作為一個獨立的程序,在正常的handlers處理之前。根本不需要重寫代碼:如果你想用一個中間件,就把它加上應用中;如果你改變主意了,去掉就好了。就這么簡單。

  1. ServeMux => Middleware Handler => Application Handler 

這篇文章,我會給大家介紹怎么自己去實現(xiàn)一個自定義的middleware模式。以及通過使用第三方的中間件軟件包的一些具體的實例。

基本原則:

在Go語言中實現(xiàn)和使用middleware是非常簡單的。

使我們的中間件能搞滿足 http.handlers 這個接口

建立一個 handlers 鏈,使其能夠滿足中間件的 handler 和 正常應用的 handler,并且能夠注冊到 http.ServeMux

我來解釋如何實現(xiàn):

首先你要知道go 的http handle,這里假設你是知道的

  1. func messageHandler(message string) http.Handler { 
  2.  
  3. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 
  4.  
  5. w.Write([]byte(message) 
  6.  
  7. }) 
  8.  

這上面這個代碼片段里面我們的邏輯很簡單只是一個簡單的 w.Write() 然后我們使用 http.HandlerFunc 適配器來轉(zhuǎn)化這個閉包,并返回。

我們可以使用一個相同的方法來創(chuàng)建一個 handler 鏈??梢允褂?handler 代替參數(shù) string 傳進閉包,然后把控制 handler 給傳進來的 handler,并且調(diào)用 ServeHTTP() 方法。

這給了我們一個完整的模式構建中間件:

  1. func exampleMiddleware(next http.Handler) http.Handler { 
  2.  
  3. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 
  4.  
  5. // Our middleware logic goes here... 
  6.  
  7. next.ServeHTTP(w, r) 
  8.  
  9. }) 
  10.  

你注意到這個中間件有一個這樣的函數(shù)結構 func(http.Handler) http.Handler 。它接受一個 handler 作為參數(shù),并且返回一個 handler。這里有兩個很有用的原因:

因為這個函數(shù)返回一個句柄可以直接供中間件注冊

我們可以建立任意長度的 handler 鏈來通過中間件的方法互相嵌套

比如:

  1. http.Handle("/", middlewareOne(middlewareTwo(finalHandler))) 

控制流說明:

讓我們來看一個帶有多個中間件的例子,并且把日志輸出到控制臺:

  1. package main 
  2.  
  3. import ( 
  4.  
  5. "log" 
  6.  
  7. "net/http" 
  8.  
  9.  
  10. func middlewareOne(next http.Handler) http.Handler { 
  11.  
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 
  13.  
  14. log.Println("Executing middlewareOne"
  15.  
  16. next.ServeHTTP(w, r) 
  17.  
  18. log.Println("Executing middlewareOne again"
  19.  
  20. }) 
  21.  
  22.  
  23. func middlewareTwo(next http.Handler) http.Handler { 
  24.  
  25. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 
  26.  
  27. log.Println("Executing middlewareTwo"
  28.  
  29. if r.URL.Path != "/" { 
  30.  
  31. return 
  32.  
  33.  
  34. next.ServeHTTP(w, r) 
  35.  
  36. log.Println("Executing middlewareTwo again"
  37.  
  38. }) 
  39.  
  40.  
  41. func final(w http.ResponseWriter, r *http.Request) { 
  42.  
  43. log.Println("Executing finalHandler"
  44.  
  45. w.Write([]byte("OK")) 
  46.  
  47.  
  48. func main() { 
  49.  
  50. finalHandler := http.HandlerFunc(final
  51.  
  52. http.Handle("/", middlewareOne(middlewareTwo(finalHandler))) 
  53.  
  54. http.ListenAndServe(":3000", nil) 
  55.  

然后我們執(zhí)行 go run main.go 在瀏覽器打開http://localhost:3000。 你會看到下面的輸出。

 [[159858]]

我們能夠很清楚的看到handle的流程控制。我們嵌套他們的返回順序。我們可以通過中間件中得 return 隨時停止handle鏈的控制。

在上面的代碼中我們在middlewareTwo function包含了retrun 語句。我們在瀏覽器中打開http://localhost:3000/foo,我們會看到。

  1. 2015/12/19 04:21:57 Executing middlewareOne 
  2.  
  3. 2015/12/19 04:21:57 Executing middlewareTwo 
  4.  
  5. 2015/12/19 04:21:57 Executing middlewareOne again 
  6.  
  7. 2015/12/19 04:21:57 Executing middlewareOne 
  8.  
  9. 2015/12/19 04:21:57 Executing middlewareTwo 
  10.  
  11. 2015/12/19 04:21:57 Executing middlewareOne again 

我們實現(xiàn)一個真實的項目的示例:

我們實現(xiàn)一個判斷請求是不是XMl的功能,我們要實現(xiàn)一個中間件。用來檢查的請求體的存在。檢查請求體,以確保它是XML。如果其中檢查失敗,我希望我們的中間件輸出錯誤信息然后終止我們的handle處理。

  1. package main 
  2.  
  3. import ( 
  4.  
  5. "bytes" 
  6.  
  7. "net/http" 
  8.  
  9.  
  10. func enforceXMLHandler(next http.Handler) http.Handler { 
  11.  
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 
  13.  
  14. // Check for a request body 
  15.  
  16. if r.ContentLength == 0 { 
  17.  
  18. http.Error(w, http.StatusText(400), 400) 
  19.  
  20. return 
  21.  
  22.  
  23. // Check its MIME type 
  24.  
  25. buf := new(bytes.Buffer) 
  26.  
  27. buf.ReadFrom(r.Body) 
  28.  
  29. if http.DetectContentType(buf.Bytes()) != "text/xml; charset=utf-8" { 
  30.  
  31. http.Error(w, http.StatusText(415), 415) 
  32.  
  33. return 
  34.  
  35.  
  36. next.ServeHTTP(w, r) 
  37.  
  38. }) 
  39.  
  40.  
  41. func main() { 
  42.  
  43. finalHandler := http.HandlerFunc(final
  44.  
  45. http.Handle("/", enforceXMLHandler(finalHandler)) 
  46.  
  47. http.ListenAndServe(":3000", nil) 
  48.  
  49.  
  50. func final(w http.ResponseWriter, r *http.Request) { 
  51.  
  52. w.Write([]byte("OK")) 
  53.  

為了檢驗我們的中間件是否實現(xiàn)了這個功能,我們首先創(chuàng)建一個XML文件。

  1. $ cat > books.xml  
  2. H. G. Wells  
  3. 8.50   

然后通過使用cURL來進行模擬請求:

  1. $ curl -i localhost:3000 
  2.  
  3. HTTP/1.1 400 Bad Request 
  4.  
  5. Content-Type: text/plain; charset=utf-8 
  6.  
  7. Content-Length: 12 
  8.  
  9. Bad Request 
  10.  
  11. $ curl -i -d "This is not XML" localhost:3000 
  12.  
  13. HTTP/1.1 415 Unsupported Media Type 
  14.  
  15. Content-Type: text/plain; charset=utf-8 
  16.  
  17. Content-Length: 23 
  18.  
  19. Unsupported Media Type 
  20.  
  21. $ curl -i -d @books.xml localhost:3000 
  22.  
  23. HTTP/1.1 200 OK 
  24.  
  25. Date: Fri, 17 Oct 2014 13:42:10 GMT 
  26.  
  27. Content-Length: 2 
  28.  
  29. Content-Type: text/plain; charset=utf-8 
  30.  
  31. OK 

接下來給大家介紹一下第三方中間件的使用:

秉承不造輪子的原則,其實在Github上有很多實現(xiàn)了一些功能的中間件。比如這里給大家介紹2個基礎驗證的中間件goji/httpauth和Gorilla’s LoggingHandler

首先我們需要引入第三方包

  1. $ go get github.com/goji/httpauth 
  1. package main 
  2.  
  3. import ( 
  4.  
  5. "github.com/goji/httpauth" 
  6.  
  7. "net/http" 
  8.  
  9.  
  10. func main() { 
  11.  
  12. finalHandler := http.HandlerFunc(final
  13.  
  14. authHandler := httpauth.SimpleBasicAuth("username""password"
  15.  
  16. http.Handle("/", authHandler(finalHandler)) 
  17.  
  18. http.ListenAndServe(":3000", nil) 
  19.  
  20.  
  21. func final(w http.ResponseWriter, r *http.Request) { 
  22.  
  23. w.Write([]byte("OK")) 
  24.  

如果你運行這個例子,你應該得到你所期望的有效和無效的憑證響應

  1. $ curl -i username:password@localhost:3000 
  2.  
  3. HTTP/1.1 200 OK 
  4.  
  5. Content-Length: 2 
  6.  
  7. Content-Type: text/plain; charset=utf-8 
  8.  
  9. OK 
  10.  
  11. $ curl -i username:wrongpassword@localhost:3000 
  12.  
  13. HTTP/1.1 401 Unauthorized 
  14.  
  15. Content-Type: text/plain; charset=utf-8 
  16.  
  17. Www-Authenticate: Basic realm=""Restricted"" 
  18.  
  19. Content-Length: 13 
  20.  
  21. Unauthorized 

Gorilla’s LoggingHandler和Apache-style logs有一些區(qū)別

以下是我們在其中寫入日志到server.log文件一個簡單的例子:

首先還是引入第三包

  1. go get github.com/gorilla/handlers 
  1. package main 
  2.  
  3. import ( 
  4.  
  5. "github.com/gorilla/handlers" 
  6.  
  7. "net/http" 
  8.  
  9. "os" 
  10.  
  11.  
  12. func main() { 
  13.  
  14. finalHandler := http.HandlerFunc(final
  15.  
  16. logFile, err := os.OpenFile("server.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) 
  17.  
  18. if err != nil { 
  19.  
  20. panic(err) 
  21.  
  22.  
  23. http.Handle("/", handlers.LoggingHandler(logFile, finalHandler)) 
  24.  
  25. http.ListenAndServe(":3000", nil) 
  26.  
  27.  
  28. func final(w http.ResponseWriter, r *http.Request) { 
  29.  
  30. w.Write([]byte("OK")) 
  31.  

在一個簡單的情況下,這樣我們的代碼是相當清楚的。但是,如果我們想用LoggingHandler作為一個更大的中間件鏈中的一部分會發(fā)生什么?我們可以很容易地結束了一個聲明,看起來像這樣:

  1. http.Handle("/", handlers.LoggingHandler(logFile, authHandler(enforceXMLHandler(finalHandler)))) 

不過這看起來太糟糕了。

我們可以通過創(chuàng)建一個構造函數(shù)打來整理一下我們給它取名為(myLoggingHandler)

和signature func(http.Handler) http.Handler.這樣就會是我們的代碼更加整潔和可讀性:

 

  1. func myLoggingHandler(h http.Handler) http.Handler { 
  2.  
  3. logFile, err := os.OpenFile("server.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) 
  4.  
  5. if err != nil { 
  6.  
  7. panic(err) 
  8.  
  9.  
  10. return handlers.LoggingHandler(logFile, h) 
  11.  
  12.  
  13. func main() { 
  14.  
  15. finalHandler := http.HandlerFunc(final
  16.  
  17. http.Handle("/", myLoggingHandler(finalHandler)) 
  18.  
  19. http.ListenAndServe(":3000", nil) 
  20.  
  1. $ cat server.log 
  2.  
  3. 127.0.0.1 - - [21/Oct/2014:18:56:43 +0100] "GET / HTTP/1.1" 200 2 
  4.  
  5. 127.0.0.1 - - [21/Oct/2014:18:56:36 +0100] "POST / HTTP/1.1" 200 2 
  6.  
  7. 127.0.0.1 - - [21/Oct/2014:18:56:43 +0100] "PUT / HTTP/1.1" 200 2 

這里還有一個比較完整結構的中間件使用的示例:

  1. package main 
  2.  
  3. import ( 
  4.  
  5. "bytes" 
  6.  
  7. "github.com/goji/httpauth" 
  8.  
  9. "github.com/gorilla/handlers" 
  10.  
  11. "net/http" 
  12.  
  13. "os" 
  14.  
  15.  
  16. func enforceXMLHandler(next http.Handler) http.Handler { 
  17.  
  18. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 
  19.  
  20. if r.ContentLength == 0 { 
  21.  
  22. http.Error(w, http.StatusText(400), 400) 
  23.  
  24. return 
  25.  
  26.  
  27. buf := new(bytes.Buffer) 
  28.  
  29. buf.ReadFrom(r.Body) 
  30.  
  31. if http.DetectContentType(buf.Bytes()) != "text/xml; charset=utf-8" { 
  32.  
  33. http.Error(w, http.StatusText(415), 415) 
  34.  
  35. return 
  36.  
  37.  
  38. next.ServeHTTP(w, r) 
  39.  
  40. }) 
  41.  
  42.  
  43. func myLoggingHandler(h http.Handler) http.Handler { 
  44.  
  45. logFile, err := os.OpenFile("server.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) 
  46.  
  47. if err != nil { 
  48.  
  49. panic(err) 
  50.  
  51.  
  52. return handlers.LoggingHandler(logFile, h) 
  53.  
  54.  
  55. func main() { 
  56.  
  57. indexHandler := http.HandlerFunc(index) 
  58.  
  59. authHandler := httpauth.SimpleBasicAuth("username""password"
  60.  
  61. http.Handle("/", myLoggingHandler(authHandler(enforceXMLHandler(indexHandler)))) 
  62.  
  63. http.ListenAndServe(":3000", nil) 
  64.  
  65.  
  66. func index(w http.ResponseWriter, r *http.Request) { 
  67.  
  68. w.Write([]byte("OK")) 
  69.  

有很多人不太喜歡中間件的設計模式,不過我還是慢喜歡的。

責任編輯:何妍 來源: 謝權'blog
相關推薦

2013-12-12 10:55:21

2024-05-06 12:30:51

Go語言中間件

2021-10-06 19:03:35

Go中間件Middleware

2022-11-18 07:54:02

Go中間件項目

2014-06-20 09:18:54

Dustjs中間件

2016-11-11 21:00:46

中間件

2021-02-11 08:21:02

中間件開發(fā)CRUD

2011-05-24 15:10:48

2024-02-06 14:05:00

Go中間件框架

2017-12-11 13:30:49

Go語言數(shù)據(jù)庫中間件

2018-07-29 12:27:30

云中間件云計算API

2018-02-01 10:19:22

中間件服務器系統(tǒng)

2015-02-07 21:52:45

PaaS中間件

2021-07-19 07:55:24

Redux中間件原理

2010-03-24 17:59:20

2013-03-13 10:37:22

中間件Windows

2018-05-02 16:23:24

中間件RPC容器

2023-11-27 07:10:06

日志中間件

2021-06-15 10:01:02

應用系統(tǒng)軟件

2023-06-29 10:10:06

Rocket MQ消息中間件
點贊
收藏

51CTO技術棧公眾號