Summary
In this episode, we formalize URL routing by creating a dedicated routes package to centralize path definitions and avoid scattering magic strings throughout the codebase. We define routes for the homepage and article pages with dynamic ID parameters, then update the home view to reference these centralized routes using string replacement and conversion functions. Finally, we build a router package that configures Echo by registering our routes to their corresponding controllers, creating a clean structure that we can expand as our application grows.
Transcript
We have seen how controllers receive a request, pass them on to a model, and then pass a response to our view. But we still need a way to route URLs to the right controller function, and this is where the router comes in. We need a way to match an incoming HTTP request to a specific controller for a given path and method—so this is GET request, POST request, PUT request, or technically also a DELETE request. We have already created a route in our home view that will direct the user to the article controller, but now I want to formalize this a bit so we have a specific place where we reference and create routes. Before we create the router, let's quickly make a dedicated routes package. This package will contain all the paths for our application and by centralizing these, avoid scattering magic strings throughout the code base. We don't want one version in one place and another version in another and then wonder why we don't hit the correct controller. So in the router/routes folder, I'm going to create a file called routes.go. I'm going to create two variables: homepage set equal to the root path, which is just a slash, and then article page, which is going to be /articles and then an ID parameter. The colon ID parameter allows the controller to extract the ID from the URL. Now we can also go ahead and replace the URL or the path that we put in our home view so that we can use the route that we have created. So go into home here, and down here we have this hard-coded path. Instead, we can say strings.Replace. We're going to replace in our routes.ArticlePage—we are going to replace the colon ID with... and now we need to convert our integer, so strconv.Itoa and article ID. Replace that with a one. We still need the imports, so we need to import our routes from the routes package and we need to import the string conversion package. Now we reference a specific place for this path. If we were to update it for any reason, we would only have to update it in one place. Now we can actually create the router package. This is where we configure Echo via routes to a controller, its middleware, its logging, and all these kinds of things. So in the router folder, I'm going to create a file called router.go in package router, and we're going to define our router struct which will hold a field called handler, which is just a pointer to Echo. Then we need a new function to actually create this router struct, and it's just going to return the router. Here we're going to say "e"—which is just how Echo does it in the documentation, so that's why I always just default to calling the variable "e." Then we can create our two routes: we have the route to homepage, which is controllers.Home, then we have the route to our article page, which is routes.ArticlePage, so controllers.Article. We don't need to call the function. And then finally pass the Echo new instance that we have registered the routes on. Super simple. We will continuously add to this as our scope expands. For now, we just need to be able to register some routes and get back our router so that we can actually use our router to configure our server. We will actually use this handler that holds whatever we configure our router to be when we set up our server.
