第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何組織 gorilla mux 路由?

如何組織 gorilla mux 路由?

Go
蕪湖不蕪 2021-12-06 15:18:08
我正在使用Gorilla Mux編寫 REST API,但在組織我的路線時遇到問題,目前我所有的路線都在這樣的main.go文件中定義//main.gopackage mainimport (    "NovAPI/routes"    "fmt"    "github.com/gorilla/mux"    "net/http")func main() {    router := mux.NewRouter().StrictSlash(true)    router.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {        fmt.Fprintln(res, "Hello")    })    router.HandleFunc("/user", func(res http.ResponseWriter, req *http.Request) {        fmt.Fprintln(res, "User")    })    router.HandleFunc("/route2", func(res http.ResponseWriter, req *http.Request) {        fmt.Fprintln(res, "Route2")    })    router.HandleFunc("/route3", func(res http.ResponseWriter, req *http.Request) {        fmt.Fprintln(res, "Route3")    })    // route declarations continue like this    http.ListenAndServe(":1128", router)}所以我想要做的是取出這個路由聲明并將其拆分為多個文件,我該怎么做呢?提前致謝。
查看完整描述

3 回答

?
皈依舞

TA貢獻1851條經(jīng)驗 獲得超3個贊

您可以將您的路由器獨立模塊化成不同的包,并將它們安裝在主路由器上


詳細說明以下問題,您可以提出這種方法,這使其具有相當?shù)目蓴U展性(并且在某種程度上更易于測試)


/api/router.go


package api


import (

    "net/http"


    "github.com/gorilla/mux"

)


func Router() *mux.Router {

    router := mux.NewRouter()

    router.HandleFunc("/", home)

    return router

}


func home(w http.ResponseWriter, req *http.Request) {

    w.Write([]byte("hello from API"))

}

/main.go


package main


import (

    "log"

    "net/http"

    "strings"


    "github.com/...yourPath.../api"

    "github.com/...yourPath.../user"

    "github.com/gorilla/mux"

)


func main() {

    router := mux.NewRouter()


    router.HandleFunc("/", home)

    mount(router, "/api", api.Router())

    mount(router, "/user", user.Router())


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

}


func mount(r *mux.Router, path string, handler http.Handler) {

    r.PathPrefix(path).Handler(

        http.StripPrefix(

            strings.TrimSuffix(path, "/"),

            handler,

        ),

    )

}


func home(w http.ResponseWriter, req *http.Request) {

    w.Write([]byte("Home"))

}


查看完整回答
反對 回復(fù) 2021-12-06
?
12345678_0001

TA貢獻1802條經(jīng)驗 獲得超5個贊

這樣的事情怎么辦?


//main.go

package main


import (

    "NovAPI/routes"

    "fmt"

    "github.com/gorilla/mux"

    "net/http"

)


func main() {


    router := mux.NewRouter().StrictSlash(true)


    router.HandleFunc("/hello", HelloHandler)

    router.HandleFunc("/user", UserHandler)

    router.HandleFunc("/route2", Route2Handler)

    router.HandleFunc("/route3", Route3Handler)

    // route declarations continue like this


    http.ListenAndServe(":1128", router)


}


func HelloHandler(res http.ResponseWriter, req *http.Request) {

    fmt.Fprintln(res, "Hello")

}


func UserHandler(res http.ResponseWriter, req *http.Request) {

    fmt.Fprintln(res, "User")

}


func Route2Handler(res http.ResponseWriter, req *http.Request) {

    fmt.Fprintln(res, "Route2")

}


func Route3Handler(res http.ResponseWriter, req *http.Request) {

    fmt.Fprintln(res, "Route3")

}

這樣您就可以將處理程序放在其他文件中,甚至其他包中。


如果您最終獲得了像數(shù)據(jù)庫這樣的附加依賴項,您甚至可以使用構(gòu)造函數(shù)技巧來避免需要全局變量:


//main.go


func main() {

    db := sql.Open(…)


    //...


    router.HandleFunc("/hello", NewHelloHandler(db))


    //...

}


func NewHelloHandler(db *sql.DB) func(http.ResponseWriter, *http.Request) {

    return func(res http.ResponseWriter, req *http.Request) {

        // db is in the local scope, and you can even inject it to test your

        // handler

        fmt.Fprintln(res, "Hello")

    }

}


查看完整回答
反對 回復(fù) 2021-12-06
?
鳳凰求蠱

TA貢獻1825條經(jīng)驗 獲得超4個贊

我喜歡查看 github 中的其他項目以獲取有關(guān)如何做事的想法,對于這些情況,我通常首先查看Docker repo。他們的做法是這樣的:


對于系統(tǒng)的路由,在system_routes.go 中定義所有處理程序,然后在system.go 中的 NewRouter 函數(shù)上初始化這些路由。


type systemRouter struct {

    backend Backend

    routes  []router.Route

}


func NewRouter(b Backend) router.Router {

    r := &systemRouter{

        backend: b,

    }


    r.routes = []router.Route{

        local.NewOptionsRoute("/", optionsHandler),

        local.NewGetRoute("/_ping", pingHandler),

        local.NewGetRoute("/events", r.getEvents),

        local.NewGetRoute("/info", r.getInfo),

        local.NewGetRoute("/version", r.getVersion),

        local.NewPostRoute("/auth", r.postAuth),

    }


    return r

}


// Routes return all the API routes dedicated to the docker system.

func (s *systemRouter) Routes() []router.Route {

    return s.routes

}

請注意,systemRouter 實現(xiàn)了router.Router接口,Routes 函數(shù)返回一個 []router.Route,它們的處理程序定義為


func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error

而不是 Go 的標準 http 處理程序:


func(w http.ResponseWriter, r *http.Request)

所以他們有額外的代碼可以在makeHttpHandler函數(shù)中將Docker API 處理程序轉(zhuǎn)換為 Go HTTP 處理程序。


最后,為了將這些路由添加到他們的 mux 路由器,在他們的server.go 上,他們實現(xiàn)了其他幾個功能來將中間件添加到他們的處理程序中。


如果您認為這是您正在尋找的東西,那么請花時間分析 Docker 代碼的路線,如果您需要我詳細說明或遺漏任何內(nèi)容,請發(fā)表評論。


查看完整回答
反對 回復(fù) 2021-12-06
  • 3 回答
  • 0 關(guān)注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號