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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作