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 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件