简体   繁体   中英

How to split GO Gorilla Mux routes in 2 separate files of the same package

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM