josephspurrier/gowebapp

How to split route into several files?

bcodus opened this issue · 2 comments

func routes() gets large and messy soon. I'm wondering how can I split it into several files, ech representing a particular entity?
What I want is something like:

func routes() *httprouter.Router {
	r := httprouter.New()
	r.NotFound = alice.
		New().
		ThenFunc(controller.Error404)

        notesRoutes()
        articlesRoutes()
        userRoutes()
       
     
	return r
}

I'd suggest after you create an instance of r := httprouter.New(), you then store it into a struct and create methods off that in different files or you could just return the return and then call functions in other files, but you will have to pass in *http.Router. Either way works.

I've done it by creating a new file api.go

func ApiRoutes(r *httprouter.Router) {
// Your routes
}

in existing route.go call it just before return

	// Call API Routes
	ApiRoutes(r)
	return r