dououde4065 2017-06-06 11:04
浏览 45
已采纳

跨软件包共享大猩猩/多路复用器路由器

I'm having some issues with implementing a slight MVC design with gorilla/mux.

The layout of the modules is as follows:

main.go
-- controllers
---- base.controller.go
---- example.controller.go
-- models
---- base.model.go
---- example.controller.go

All the files in controllers is in the controllers package, same with models and then the main.go is the main package.

Currently I'm just trying to get the Base Controller to be able to be shared with the main package which is working, although it's throwing some errors when trying to implement routes. The build is not throwing any errors, but the routes are not available. If I implement the Walk function in the Gorilla/Mux documentation to print out all the registered routes for the mux.Router then it gives me this error:

&{%!!(MISSING)s(*mux.Router=&{ [0xc4200901b0] map[] true false false false}) %!!(MISSING)s(http.HandlerFunc=0xc8df0) [%!!(MISSING)s(*mux.routeRegexp=&{/ false false true false 0xc420095360 / [] []})] %!!(MISSING)s(*mux.routeRegexpGroup=&{ 0xc420016240 []}) %!!(MISSING)s(bool=true) %!!(MISSING)s(bool=false) %!!(MISSING)s(bool=false) %!!(MISSING)s(bool=false) %!!(MISSING)s(mux.BuildVarsFunc=)}

The reasoning for the global var V1Router *mux.Router is firstly to access it in the main package and also to create subrouters in the other controllers.

I am fairly new to Go, but I'm trying my best to learn the best practices! Any help would be greatly appreciated!

Example code below:

base.controllers.go

package controllers

import (
        "fmt"
        "bytes"
        "net/http"
        "github.com/gorilla/mux"
)

var V1Router *mux.Router

func init () {
    V1Router = mux.NewRouter()
    V1Router.StrictSlash(true)
    V1Router.HandleFunc("/", BaseHandler)    
}

// Base route to access the API Documentation.
func BaseHandler (w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, Gophers!")
}

main.go

package main

import (
    "net/http"
    "log"
    "github.com/projectrepo/project/models"
    "github.com/projectrepo/project/controllers"
    "github.com/gorilla/mux"
)

func main () {
    http.Handle("/v1", controllers.V1Router)
    if err := http.ListenAndServe(":8000", nil); err != nil {
        log.Fatal("Serving error.")
    }
}

In response to the comments, I tried this solution with the same result:

package main

import (
    "net/http"
    "log"
    "github.com/projectrepo/project/models"
    "github.com/projectrepo/project/controllers"
    "github.com/gorilla/mux"
)

func main () {
    r := mux.NewRouter()
    r.Handle("/v1", controllers.V1Router)
    if err := http.ListenAndServe(":8000", r); err != nil {
        log.Fatal("Serving error.")
    }
}
  • 写回答

1条回答 默认 最新

  • dtrvzd1171 2017-06-06 13:42
    关注

    Gorilla mux.Router is supposed to be used to create mapping between a set of predefined rules (e.g. host, path, protocol, scheme, etc...) and it's handler (http.Handler or http.HandlerFunc). Gorilla mux can be used to replace standard server mux. If you combine gorilla/mux with built in http server mux as your original question, i.e.

    func main () {
        http.Handle("/v1", controllers.V1Router)
        if err := http.ListenAndServe(":8000", nil); err != nil {
            log.Fatal("Serving error.")
        }
    }
    

    what actually happen when a client access /v1 is controllers.V1Router will be called with request path /v1 passed to V1Router1. In the controllers.V1Router, you defined that / will be handled by BaseHandler. However, since incoming request path is /v1, it won't match to your routing table. If you want to define sub routing, you can do as follows (this is what I mean in first comment):

    func main () {
        r := mux.NewRouter()
        v1 := r.PathPrefix("/v1").Subrouter()
        controllers.RegisterHandlers(v1)
    
        if err := http.ListenAndServe(":8000", r); err != nil {
            log.Fatal("Serving error.")
        }
    }
    

    Then in the controllers (base.controllers.go) define

    //Register handlers and it's sub router
    func RegisterHandlers(r *mux.Router) {
        //base handler, i.e. /v1
        r.StrictSlash(true)
        r.HandleFunc("/", BaseHandler)
    
        //example sub-router, i.e. /v1/example
        ex := r.PathPrefix("/example").Subrouter()
        ex.HandleFunc("/", ExampleHandler)
    
        //other handlers...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下