7

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Dzintars
  • 1,410
  • 21
  • 29

1 Answers1

39

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
}
leaf bebop
  • 7,643
  • 2
  • 17
  • 27