Go 自定義路由器設(shè)計(jì)

2022-05-13 16:28 更新

HTTP路由

HTTP路由組件負(fù)責(zé)將HTTP請(qǐng)求交到對(duì)應(yīng)的函數(shù)處理(或者是一個(gè)struct的方法),如前面小節(jié)所描述的結(jié)構(gòu)圖,路由在框架中相當(dāng)于一個(gè)事件處理器,而這個(gè)事件包括:

  • 用戶請(qǐng)求的路徑(path)(例如:/user/123,/article/123),當(dāng)然還有查詢串信息(例如?id=11)
  • HTTP的請(qǐng)求方法(method)(GET、POST、PUT、DELETE、PATCH等)

路由器就是根據(jù)用戶請(qǐng)求的事件信息轉(zhuǎn)發(fā)到相應(yīng)的處理函數(shù)(控制層)。

默認(rèn)的路由實(shí)現(xiàn)

在3.4小節(jié)有過介紹Go的http包的詳解,里面介紹了Go的http包如何設(shè)計(jì)和實(shí)現(xiàn)路由,這里繼續(xù)以一個(gè)例子來說明:

func fooHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
}

http.HandleFunc("/foo", fooHandler)

http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})

log.Fatal(http.ListenAndServe(":8080", nil))

上面的例子調(diào)用了http默認(rèn)的DefaultServeMux來添加路由,需要提供兩個(gè)參數(shù),第一個(gè)參數(shù)是希望用戶訪問此資源的URL路徑(保存在r.URL.Path),第二參數(shù)是即將要執(zhí)行的函數(shù),以提供用戶訪問的資源。路由的思路主要集中在兩點(diǎn):

  • 添加路由信息
  • 根據(jù)用戶請(qǐng)求轉(zhuǎn)發(fā)到要執(zhí)行的函數(shù)

Go默認(rèn)的路由添加是通過函數(shù)http.Handlehttp.HandleFunc等來添加,底層都是調(diào)用了DefaultServeMux.Handle(pattern string, handler Handler),這個(gè)函數(shù)會(huì)把路由信息存儲(chǔ)在一個(gè)map信息中map[string]muxEntry,這就解決了上面說的第一點(diǎn)。

Go監(jiān)聽端口,然后接收到tcp連接會(huì)扔給Handler來處理,上面的例子默認(rèn)nil即為http.DefaultServeMux,通過DefaultServeMux.ServeHTTP函數(shù)來進(jìn)行調(diào)度,遍歷之前存儲(chǔ)的map路由信息,和用戶訪問的URL進(jìn)行匹配,以查詢對(duì)應(yīng)注冊(cè)的處理函數(shù),這樣就實(shí)現(xiàn)了上面所說的第二點(diǎn)。

for k, v := range mux.m {
    if !pathMatch(k, path) {
        continue
    }
    if h == nil || len(k) > n {
        n = len(k)
        h = v.h
    }
}

beego框架路由實(shí)現(xiàn)

目前幾乎所有的Web應(yīng)用路由實(shí)現(xiàn)都是基于http默認(rèn)的路由器,但是Go自帶的路由器有幾個(gè)限制:

  • 不支持參數(shù)設(shè)定,例如/user/:uid 這種泛類型匹配
  • 無法很好的支持REST模式,無法限制訪問的方法,例如上面的例子中,用戶訪問/foo,可以用GET、POST、DELETE、HEAD等方式訪問
  • 一般網(wǎng)站的路由規(guī)則太多了,編寫繁瑣。我前面自己開發(fā)了一個(gè)API應(yīng)用,路由規(guī)則有三十幾條,這種路由多了之后其實(shí)可以進(jìn)一步簡(jiǎn)化,通過struct的方法進(jìn)行一種簡(jiǎn)化

beego框架的路由器基于上面的幾點(diǎn)限制考慮設(shè)計(jì)了一種REST方式的路由實(shí)現(xiàn),路由設(shè)計(jì)也是基于上面Go默認(rèn)設(shè)計(jì)的兩點(diǎn)來考慮:存儲(chǔ)路由和轉(zhuǎn)發(fā)路由

存儲(chǔ)路由

針對(duì)前面所說的限制點(diǎn),我們首先要解決參數(shù)支持就需要用到正則,第二和第三點(diǎn)我們通過一種變通的方法來解決,REST的方法對(duì)應(yīng)到struct的方法中去,然后路由到struct而不是函數(shù),這樣在轉(zhuǎn)發(fā)路由的時(shí)候就可以根據(jù)method來執(zhí)行不同的方法。

根據(jù)上面的思路,我們?cè)O(shè)計(jì)了兩個(gè)數(shù)據(jù)類型controllerInfo(保存路徑和對(duì)應(yīng)的struct,這里是一個(gè)reflect.Type類型)和ControllerRegistor(routers是一個(gè)slice用來保存用戶添加的路由信息,以及beego框架的應(yīng)用信息)

type controllerInfo struct {
    regex          *regexp.Regexp
    params         map[int]string
    controllerType reflect.Type
}

type ControllerRegistor struct {
    routers     []*controllerInfo
    Application *App
}

ControllerRegistor對(duì)外的接口函數(shù)有

func (p *ControllerRegistor) Add(pattern string, c ControllerInterface)

詳細(xì)的實(shí)現(xiàn)如下所示:

func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
    parts := strings.Split(pattern, "/")

    j := 0
    params := make(map[int]string)
    for i, part := range parts {
        if strings.HasPrefix(part, ":") {
            expr := "([^/]+)"

            //a user may choose to override the defult expression
            // similar to expressjs: ‘/user/:id([0-9]+)’

            if index := strings.Index(part, "("); index != -1 {
                expr = part[index:]
                part = part[:index]
            }
            params[j] = part
            parts[i] = expr
            j++
        }
    }

    //recreate the url pattern, with parameters replaced
    //by regular expressions. then compile the regex

    pattern = strings.Join(parts, "/")
    regex, regexErr := regexp.Compile(pattern)
    if regexErr != nil {

        //TODO add error handling here to avoid panic
        panic(regexErr)
        return
    }

    //now create the Route
    t := reflect.Indirect(reflect.ValueOf(c)).Type()
    route := &controllerInfo{}
    route.regex = regex
    route.params = params
    route.controllerType = t

    p.routers = append(p.routers, route)

}

靜態(tài)路由實(shí)現(xiàn)

上面我們實(shí)現(xiàn)的動(dòng)態(tài)路由的實(shí)現(xiàn),Go的http包默認(rèn)支持靜態(tài)文件處理FileServer,由于我們實(shí)現(xiàn)了自定義的路由器,那么靜態(tài)文件也需要自己設(shè)定,beego的靜態(tài)文件夾路徑保存在全局變量StaticDir中,StaticDir是一個(gè)map類型,實(shí)現(xiàn)如下:

func (app *App) SetStaticPath(url string, path string) *App {
    StaticDir[url] = path
    return app
}

應(yīng)用中設(shè)置靜態(tài)路徑可以使用如下方式實(shí)現(xiàn):

beego.SetStaticPath("/img","/static/img")

轉(zhuǎn)發(fā)路由

轉(zhuǎn)發(fā)路由是基于ControllerRegistor里的路由信息來進(jìn)行轉(zhuǎn)發(fā)的,詳細(xì)的實(shí)現(xiàn)如下代碼所示:

// AutoRoute
func (p *ControllerRegistor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    defer func() {
        if err := recover(); err != nil {
            if !RecoverPanic {
                // go back to panic
                panic(err)
            } else {
                Critical("Handler crashed with error", err)
                for i := 1; ; i += 1 {
                    _, file, line, ok := runtime.Caller(i)
                    if !ok {
                        break
                    }
                    Critical(file, line)
                }
            }
        }
    }()
    var started bool
    for prefix, staticDir := range StaticDir {
        if strings.HasPrefix(r.URL.Path, prefix) {
            file := staticDir + r.URL.Path[len(prefix):]
            http.ServeFile(w, r, file)
            started = true
            return
        }
    }
    requestPath := r.URL.Path

    //find a matching Route
    for _, route := range p.routers {

        //check if Route pattern matches url
        if !route.regex.MatchString(requestPath) {
            continue
        }

        //get submatches (params)
        matches := route.regex.FindStringSubmatch(requestPath)

        //double check that the Route matches the URL pattern.
        if len(matches[0]) != len(requestPath) {
            continue
        }

        params := make(map[string]string)
        if len(route.params) > 0 {
            //add url parameters to the query param map
            values := r.URL.Query()
            for i, match := range matches[1:] {
                values.Add(route.params[i], match)
                params[route.params[i]] = match
            }

            //reassemble query params and add to RawQuery
            r.URL.RawQuery = url.Values(values).Encode() + "&" + r.URL.RawQuery
            //r.URL.RawQuery = url.Values(values).Encode()
        }
        //Invoke the request handler
        vc := reflect.New(route.controllerType)
        init := vc.MethodByName("Init")
        in := make([]reflect.Value, 2)
        ct := &Context{ResponseWriter: w, Request: r, Params: params}
        in[0] = reflect.ValueOf(ct)
        in[1] = reflect.ValueOf(route.controllerType.Name())
        init.Call(in)
        in = make([]reflect.Value, 0)
        method := vc.MethodByName("Prepare")
        method.Call(in)
        if r.Method == "GET" {
            method = vc.MethodByName("Get")
            method.Call(in)
        } else if r.Method == "POST" {
            method = vc.MethodByName("Post")
            method.Call(in)
        } else if r.Method == "HEAD" {
            method = vc.MethodByName("Head")
            method.Call(in)
        } else if r.Method == "DELETE" {
            method = vc.MethodByName("Delete")
            method.Call(in)
        } else if r.Method == "PUT" {
            method = vc.MethodByName("Put")
            method.Call(in)
        } else if r.Method == "PATCH" {
            method = vc.MethodByName("Patch")
            method.Call(in)
        } else if r.Method == "OPTIONS" {
            method = vc.MethodByName("Options")
            method.Call(in)
        }
        if AutoRender {
            method = vc.MethodByName("Render")
            method.Call(in)
        }
        method = vc.MethodByName("Finish")
        method.Call(in)
        started = true
        break
    }

    //if no matches to url, throw a not found exception
    if started == false {
        http.NotFound(w, r)
    }
}

使用入門

基于這樣的路由設(shè)計(jì)之后就可以解決前面所說的三個(gè)限制點(diǎn),使用的方式如下所示:

基本的使用注冊(cè)路由:

beego.BeeApp.RegisterController("/", &controllers.MainController{})

參數(shù)注冊(cè):

beego.BeeApp.RegisterController("/:param", &controllers.UserController{})

正則匹配:

beego.BeeApp.RegisterController("/users/:uid([0-9]+)", &controllers.UserController{})
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)