Routers
This go boilerplate uses Echo. If you're ever in doubt, you can refer to Echo's documentation for more details on what's possible with routers.
This boilerplate is shipped with 3 routers, public, privdate and hidden routers - all of which ollow the same structure and procedure with slight differences in what is registered within each.
Why have 3 routers? Well, some projects may have public and protected routes, and such use case is straightforward. The latter implements an authentication middleware while the first does not. Attempting to achieve such behaviour within a single router can be tricky, so isolated routers running on different ports are used instead. The third "hidden" router is provided to enable a pattern commonly used to allow one microservice to communicate with another without exposing those routes to the public internet. With that said, wiring those 3 routers can easily be achieved through a different service like Kubernetes or NGINX.
All routers should go through the following process:
- Initialisation
- Checking for DevMode and enabling related middlewares
- Register common middleware
- Register health routes
- Register security middleware
- Register user-defined routes
- Register error handler
Have a look at pkg/api/routers/protectedApi.go
to familiarise yourself with router initialisation process. If you've already created your handlers from the previous section, all you need is to add your new route to this file as such:
func registerProtectedAPIRoutes() {
cats := protectedApiRouter.Echo.Group("/cats") // Your new REST resource
cats.GET("", catsHandlers.Index) // GET "/cats/" route and handler
cats.GET("/:id", catsHandlers.Get) // GET "/cats/:id" route and handler
cats.POST("", catsHandlers.Post) // POST "/cats/" route and handler
cats.PUT("/:id", catsHandlers.Put) // PUT "/cats/:id" route and handler
cats.DELETE("/:id", catsHandlers.Delete) // DELETE "/cats/:id" route and handler
// add more routes here ...
}