dongqiaogouk86049 2018-02-04 19:49
浏览 88
已采纳

如何将GO Gorilla Mux路线拆分为同一包的2个独立文件

I have separate file routes.go (package routes) where I store all my routes and handlers. But I want to split this file in 2 files. I want to rename my routes.go to main.go and create new additional file moduleX.go (package routes). How can I do that? I want to store all my routes in multiple files of the same "package routes".

package routes

import (
    "github.com/gorilla/mux"
    "net/http"
    "github.com/---/001/models"
    "github.com/---/001/sessions"
    "github.com/---/001/utils"
    "github.com/---/001/middleware"
)

func NewRouter() *mux.Router {
    r := mux.NewRouter()
    r.HandleFunc("/", middleware.AuthRequired(indexGetHandler)).Methods("GET")
    r.HandleFunc("/", middleware.AuthRequired(indexPostHandler)).Methods("POST")
    r.HandleFunc("/signup", signupGetHandler).Methods("GET")
    r.HandleFunc("/signup", signupPostHandler).Methods("POST")
    r.HandleFunc("/signin", signinGetHandler).Methods("GET")
    r.HandleFunc("/signin", signinPostHandler).Methods("POST")
    r.HandleFunc("/signout", signoutGetHandler).Methods("GET")
    r.HandleFunc("/services", middleware.AuthRequired(servicesHandler)).Methods("GET")
    fs := http.FileServer(http.Dir("./static/"))
    r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
    return r
}

I want move all "/signup" and "/signin" routes and handlers outside of this main file. And then somehow to pass them back to this NewRouter function. You can provide me just a book or some online example.

</div>
  • 写回答

1条回答 默认 最新

  • douyao6842 2018-02-04 20:33
    关注

    You can use another function that modifies the router to do that.

    //In another file
    func addSignHandler(r *mux.Router) {
        r.HandleFunc("/signup", signupGetHandler).Methods("GET")
        r.HandleFunc("/signup", signupPostHandler).Methods("POST")
        r.HandleFunc("/signin", signinGetHandler).Methods("GET")
        r.HandleFunc("/signin", signinPostHandler).Methods("POST")
        r.HandleFunc("/signout", signoutGetHandler).Methods("GET")
    }
    

    And to use it:

    func NewRouter() *mux.Router {
        r := mux.NewRouter()
        r.HandleFunc("/", middleware.AuthRequired(indexGetHandler)).Methods("GET")
        r.HandleFunc("/", middleware.AuthRequired(indexPostHandler)).Methods("POST")
    
        addSignHandler(r)
    
        r.HandleFunc("/services", middleware.AuthRequired(servicesHandler)).Methods("GET")
        fs := http.FileServer(http.Dir("./static/"))
        r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
        return r
    }
    

    Or even better, you can refactor your code to make it more consistent:

    func addMainHandler(r *mux.Router) {
        r.HandleFunc("/", middleware.AuthRequired(indexGetHandler)).Methods("GET")
        r.HandleFunc("/", middleware.AuthRequired(indexPostHandler)).Methods("POST")
        r.HandleFunc("/services", middleware.AuthRequired(servicesHandler)).Methods("GET")
        fs := http.FileServer(http.Dir("./static/"))
        r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
    }
    

    And simplify the NewRouter to:

    func NewRouter() *mux.Router {
        r := mux.NewRouter()
        addMainHandler(r)
        addSignHandler(r)
        return r
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集