Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to split route into several files? #53

Open
bcodus opened this issue Nov 6, 2021 · 2 comments
Open

How to split route into several files? #53

bcodus opened this issue Nov 6, 2021 · 2 comments

Comments

@bcodus
Copy link

bcodus commented Nov 6, 2021

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
}
@bcodus bcodus changed the title How to split route into How to split route into several files? Nov 6, 2021
@josephspurrier
Copy link
Owner

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.

@karfianto
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants