Turbo Router: Path Params, Query Params, and Middlewares
Welcome to the third and final part of our blog series on Turbo. In the first two parts, we covered the core utilities of Turbo, focusing on client handling, logging, messaging, and other foundational aspects. Now, we’re diving into one of the most crucial components of any web framework—routing.
In this post, we’ll explore Turbo Router, a high-performance routing utility for Golang. We’ll cover how to work with path parameters, query parameters, and how to efficiently manage your application’s flow using middlewares and filters.
By the end of this article, you'll have a strong understanding of how to leverage Turbo Router to build scalable and maintainable web applications.
1. Handling Path Parameters
Path parameters (also called URL parameters) allow us to capture values in the URL directly. For example, in a URL like /users/123, the value 123 is a dynamic path parameter representing a user ID.
With Turbo Router, handling path parameters is simple. You define your routes with placeholders for dynamic segments.
Here’s how to define a route with path parameters:
router := turbo.NewRouter()
router.Get("/users/:id")
In this example:
- The :id in the route "/users/:id" captures the path parameter.
- To fetch the path param, internal function is provided
getIntPathParms(id string, r *http.Request) int {}
where you pass in the path param string key and fetch theInt
value. - When a request is made to /users/123, the handler receives 123 as the userID.
2. Working with Query Parameters
Query parameters are a powerful way to pass data in a URL without making the path dynamic. They appear after a ? in the URL, like /search?query=golang.
With Turbo Router, accessing query parameters is straightforward:
router.Get("/search", handlerFunc (w http.ResponseWriter, r *http.Request) {
val, err := GetQueryParams("query", r)
})
In this case:
GetQueryParams("query", r) fetches the value of the query parameter query.
3. Using Middlewares and Filters
Middlewares are functions that run before your route handler. They allow you to perform actions like authentication, logging, or modifying the request/response. With Turbo Router, you can apply middlewares globally or to specific routes.
To define a simple middleware:
func LoggerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Info("Filter Added")
logger.Info(r.RequestURI)
next.ServeHTTP(w, r)
logger.Info("Filter Added again")
})
}
In this example:
LoggerMiddleware
logs the Request URL for each request before calling the next handler.
If you want to apply it to a specific route:
turboRouter := turbo.NewRouter()
turboRouter.Get("/api/v1", ResponseHandler).AddFilter(LoggerMiddleware)
Full detailed examples can be found in below repository.
nandlabs / golly-samples
This repository contains the samples for Golly
Golly Samples Repository
Welcome to the Golly Examples Repository! This repository contains a collection of sample code snippets and examples from the individual libraries of the Golly project. Each example is designed to demonstrate the usage and capabilities of different modules within the Golly ecosystem.
Table of Contents
Introduction
Golly is an open-source project aimed at providing robust tools and libraries for various applications. This repository showcases practical examples to help developers understand and utilize the different components of Golly effectively.
Getting Started
To get started with the examples, you'll need to have Golly installed. You can follow the instructions on the Golly GitHub page to set it up.
Prerequisites
- Golang (version 1.18 or higher)
- Golly installed
Installation
Clone this repository to your local machine:
git clone https://github.com/yourusername/golly-examples.git
cd golly-examples
Examples
This repository is organized by individual libraries within Golly. Each directory contains…
Conclusion
Turbo Router is a powerful yet simple tool for routing in Golang applications. With support for dynamic Path Parameters, easy-to-access Query Parameters, and flexible Middlewares, you can build robust, scalable web applications. By leveraging these features, you can keep your code clean, efficient, and maintainable.
Whether you’re building a small API or a large web application, Turbo Router can streamline your routing and middleware setup, allowing you to focus more on the logic that matters.
Happy coding!
Top comments (0)