-2

I just started learning Go and want to create my own REST API.

The problem is simple: I want to have the routes of my api in a different file for example: routes/users.go that then I include in the "main" function and register those routes.

There are a high number of examples of restAPI's in Echo/Go but all of them have the routes in the main() function.

I checked a few examples/github starter kits but it seems that I cannot find a solution that I like.

func main() {
    e := echo.New()

    e.GET("/", func(c echo.Context) error {
        responseJSON := &JSResp{Msg: "Hello World!"}
        return c.JSON(http.StatusOK, responseJSON)
    })

     //I want to get rid of this
    e.GET("users", UserController.CreateUser)
    e.POST("users", UserController.UpdateUser)
    e.DELETE("users", UserController.DeleteUser)

    //would like something like
    // UserRoutes.initRoutes(e)

    e.Logger.Fatal(e.Start(":1323"))
}

//UserController.go
//CreateUser 
func CreateUser(c echo.Context) error {
    responseJSON := &JSResp{Msg: "Create User!"}
    return c.JSON(http.StatusOK, responseJSON)
}

//UserRoutes.go
func initRoutes(e) { //this is probably e* echo or something like that
//UserController is a package in this case that exports the CreateUser function
    e.GET("users", UserController.CreateUser) 
    return e;
}

Is there an easy way to make this? Coming from node.js and still having some syntax errors of course, will solve them, but I am struggling with the architecture of my code at the moment.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
doglover1337
  • 146
  • 4
  • 18

1 Answers1

4

I want to have the routes of my api in a different file for example: routes/users.go that then I include in the "main" function and register those routes.

This is possible, simply have your files in the routes package declare functions that take an instance of *echo.Echo and have them register the handlers.

// routes/users.go

func InitUserRoutes(e *echo.Echo) {
    e.GET("users", UserController.CreateUser)
    e.POST("users", UserController.UpdateUser)
    e.DELETE("users", UserController.DeleteUser)
}


// routes/posts.go

func InitPostRoutes(e *echo.Echo) {
    e.GET("posts", PostController.CreatePost)
    e.POST("posts", PostController.UpdatePost)
    e.DELETE("posts", PostController.DeletePost)
}

and then in main.go

import (
     "github.com/whatever/echo"
     "package/path/to/routes"
)

func main() {
    e := echo.New()
    routes.InitUserRoutes(e)
    routes.InitPostRoutes(e)
    // ...
}

Note that the InitXxx functions need to start with an upper case letter as opposed to your initRoutes example which has its first letter in lower case. This is because identifiers with lower case first letters are unexported, which makes them inaccessible from outside their own package. Put another way, for you to be able to reference an imported identifier you have to export it by having it start with an upper case letter.

More here: https://golang.org/ref/spec#Exported_identifiers

mkopriva
  • 35,176
  • 4
  • 57
  • 71
  • Thanks for you answer, got it work ! I also appreciate your time and effort in writing everything and the link regarding Exported_identifies, you are awesome ! – doglover1337 Aug 21 '19 at 16:48