Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 561
Dzintars Avatar asked Feb 04 '18 19:02

Dzintars


1 Answers

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
}
like image 180
leaf bebop Avatar answered Oct 23 '22 06:10

leaf bebop