Add more term fixes and markdown format fixes

This commit is contained in:
Will
2019-03-03 00:40:37 +08:00
parent accc3cc768
commit d5355ad2ec
69 changed files with 573 additions and 393 deletions

View File

@@ -9,6 +9,7 @@ HTTP 路由元件負責將 HTTP 請求交到對應的函式處理(或者是一
路由器就是根據使用者請求的事件資訊轉發到相應的處理函式(控制層)。
## 預設的路由實現
在 3.4 小節有過介紹 Go 的 http 套件的詳解,裡面介紹了 Go 的 http 套件如何設計和實現路由,這裡繼續以一個例子來說明:
```Go
func fooHandler(w http.ResponseWriter, r *http.Request) {
@@ -22,8 +23,8 @@ http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
})
log.Fatal(http.ListenAndServe(":8080", nil))
```
上面的例子呼叫了 http 預設的 DefaultServeMux 來新增路由,需要提供兩個參數,第一個參數是希望使用者訪問此資源的 URL 路徑(儲存在 r.URL.Path),第二參數是即將要執行的函式,以提供使用者訪問的資源。路由的思路主要集中在兩點:
- 新增路由資訊
@@ -32,6 +33,7 @@ log.Fatal(http.ListenAndServe(":8080", nil))
Go 預設的路由新增是透過函式`http.Handle``http.HandleFunc`等來新增,底層都是呼叫了`DefaultServeMux.Handle(pattern string, handler Handler)`,這個函式會把路由資訊儲存在一個 map 資訊中`map[string]muxEntry`,這就解決了上面說的第一點。
Go 監聽埠,然後接收到 tcp 連線會扔給 Handler 來處理,上面的例子預設 nil 即為`http.DefaultServeMux`,透過`DefaultServeMux.ServeHTTP`函式來進行排程,遍歷之前儲存的 map 路由資訊,和使用者訪問的 URL 進行匹配,以查詢對應註冊的處理函式,這樣就實現了上面所說的第二點。
```Go
for k, v := range mux.m {
@@ -52,12 +54,13 @@ for k, v := range mux.m {
- 無法很好的支援 REST 模式,無法限制訪問的方法,例如上面的例子中,使用者訪問/foo可以用 GET、POST、DELETE、HEAD 等方式訪問
- 一般網站的路由規則太多了,編寫繁瑣。我前面自己開發了一個 API 應用,路由規則有三十幾條,這種路由多了之後其實可以進一步簡化,透過 struct 的方法進行一種簡化
beego 框架的路由器基於上面的幾點限制考慮設計了一種 REST 方式的路由實現,路由設計也是基於上面 Go 預設設計的兩點來考慮:儲存路由和轉路由
beego 框架的路由器基於上面的幾點限制考慮設計了一種 REST 方式的路由實現,路由設計也是基於上面 Go 預設設計的兩點來考慮:儲存路由和轉路由
### 儲存路由
針對前面所說的限制點我們首先要解決參數支援就需要用到正則第二和第三點我們透過一種變通的方法來解決REST 的方法對應到 struct 的方法中去,然後路由到 struct 而不是函式,這樣在轉路由的時候就可以根據 method 來執行不同的方法。
針對前面所說的限制點我們首先要解決參數支援就需要用到正則第二和第三點我們透過一種變通的方法來解決REST 的方法對應到 struct 的方法中去,然後路由到 struct 而不是函式,這樣在轉路由的時候就可以根據 method 來執行不同的方法。
根據上面的思路,我們設計了兩個資料型別 controllerInfo(儲存路徑和對應的 struct這裡是一個 reflect.Type 型別)和 ControllerRegistor(routers 是一個 slice 用來儲存使用者新增的路由資訊,以及 beego 框架的應用資訊)
```Go
type controllerInfo struct {
@@ -70,14 +73,16 @@ type ControllerRegistor struct {
routers []*controllerInfo
Application *App
}
```
ControllerRegistor 對外的介面函式有
```Go
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface)
```
詳細的實現如下所示:
```Go
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
@@ -127,6 +132,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
```
### 靜態路由實現
上面我們實現的動態路由的實現Go 的 http 套件預設支援靜態檔案處理 FileServer由於我們實現了自訂的路由器那麼靜態檔案也需要自己設定beego 的靜態資料夾路徑儲存在全域性變數 StaticDir 中StaticDir 是一個 map 型別,實現如下:
```Go
func (app *App) SetStaticPath(url string, path string) *App {
@@ -134,16 +140,18 @@ func (app *App) SetStaticPath(url string, path string) *App {
return app
}
```
應用中設定靜態路徑可以使用如下方式實現:
```Go
```Go
beego.SetStaticPath("/img","/static/img")
```
### 轉發路由
轉發路由是基於 ControllerRegistor 裡的路由資訊來進行轉發的,詳細的實現如下程式碼所示:
```Go
### 轉向路由
轉向路由是基於 ControllerRegistor 裡的路由資訊來進行轉發的,詳細的實現如下程式碼所示:
```Go
// AutoRoute
func (p *ControllerRegistor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() {
@@ -256,16 +264,19 @@ func (p *ControllerRegistor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
基於這樣的路由設計之後就可以解決前面所說的三個限制點,使用的方式如下所示:
基本的使用註冊路由:
```Go
beego.BeeApp.RegisterController("/", &controllers.MainController{})
```
參數註冊:
```Go
beego.BeeApp.RegisterController("/:param", &controllers.UserController{})
```
正則匹配:
```Go
beego.BeeApp.RegisterController("/users/:uid([0-9]+)", &controllers.UserController{})