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 can I structure my routes into seperate files? #270

Closed
moneszarrugh opened this issue Apr 12, 2020 · 6 comments
Closed

🤔 How can I structure my routes into seperate files? #270

moneszarrugh opened this issue Apr 12, 2020 · 6 comments

Comments

@moneszarrugh
Copy link
Contributor

Right now all of my routes are in the main function.

What is the correct way to structure my application so that I can separate routes into multiple files the same way as with express?

I want to handle the GET, POST etc. of each route in a separate file for that route

@welcome
Copy link

welcome bot commented Apr 12, 2020

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you want to chat with us or need help, join us on our Discord server: https://gofiber.io/discord

@renanbastos93
Copy link
Member

Hey @monzarrugh

If I understood you, I suggest you use a design pattern in your project, I am using currently clean architecture

https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html

@mthli
Copy link

mthli commented Apr 13, 2020

Structure routes into seperate files may caused routes conflict 🤔

@Fenny
Copy link
Member

Fenny commented Apr 13, 2020

@monzarrugh, does this help https://github.com/gofiber/boilerplate or https://blog.sallai.me/rest-api-with-fiber/?
Or are you looking to move whole groups into separate files?

@Fenny Fenny closed this as completed Apr 14, 2020
@moneszarrugh
Copy link
Contributor Author

moneszarrugh commented Apr 15, 2020

Or are you looking to move whole groups into separate files?

Yes I'd like to handle a group requests in a separate file

@Fenny
Copy link
Member

Fenny commented Apr 20, 2020

@monzarrugh you could pass app to a custom function to register routes from a separate file.

// ./main.go
package main

import (
  "./routes"
  "github.com/gofiber/fiber"
)

func main() {
  app := fiber.New()

  routes.Register(app)
  //app.Get("/admin",     handlerAdmin)
  //app.Post("/register", handlerRegister)
  //app.Post("/login",    handlerLogin)

  app.Listen(3000)
}
// ./routes/api.go
package routes

import (
  "github.com/gofiber/fiber"
)

func Register(app *fiber.App) {
  app.Get("/admin",     handlerAdmin)
  app.Post("/register", handlerRegister)
  app.Post("/login",    handlerLogin)
}

@Fenny Fenny changed the title How can I structure my routes into seperate files? 🤔 🤔 How can I structure my routes into seperate files? Apr 20, 2020
@Fenny Fenny closed this as completed Apr 20, 2020
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

4 participants