dongxie1907 2015-12-31 14:53
浏览 31
已采纳

大猩猩多路复用路线如何组织?

i am using Gorilla Mux for writing a REST API and i am having trouble organizing my routes, currently all of my routes are defined in the main.go file like this

//main.go
package main

import (
    "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)

}

so what i want to do is take out and split this route declaration into multiple files, how would i go about doing that? thanks in advance.

  • 写回答

3条回答 默认 最新

  • douzi2749 2015-12-31 15:05
    关注

    What about something like this ?

    //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")
    }
    

    That way you can put your handlers in other files, or even other packages.

    If you endup with additionnal dependencies like a database, you can even avoid the need of the global var using a constructor trick:

    //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")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测