Setting Up the Routes
The registration functionality will need two routes — one for displaying the registration page and another to handle the POST request submitted from the registration page.
While defining routes in Gin, we can group two or more routes together under a common parent if we want to. Since both the new routes are related, we can create a grouped route as shown below:
// routes.go
package main
func initializeRoutes() {
router.GET("/", showIndexPage)
userRoutes := router.Group("/u")
{
userRoutes.GET("/register", showRegistrationPage)
userRoutes.POST("/register", register)
}
router.GET("/article/view/:article_id", getArticle)
}
Grouping routes together allows you to apply middleware on all routes in a group instead of doing so separately for each route. In the above snippet, the first route will use the showRegistrationPage
function to display the registration page at the /u/register
path. The second route will handle all the POST requests to the same path, making use of the register
route handler.