Deep Dive into Golly: turbo (Part - 2)

August 1, 2024 (1mo ago)

turbo has a wide range of functionalities inbuilt which you can leverage to improve your application development.

Once you have created the object of turbo

router := tubro.NewRouter()

An instance of turbo is created which is used as an handler to the http.Server object below

srv := &http.Server{
	Handler:      router,
	Addr:         ":8080",
	ReadTimeout:  20 * time.Second,
	WriteTimeout: 20 * time.Second,
}

Multiple configurations can be done on the http.Server object above based on the requirements and specifications. Some of the configurations that you can provide are TLSConfig, IdleTimeout, ReadHeaderTimeout etc. More information regarding the http.Server object can be found in the documentation.

Now that the server has been configured, API routes need to be registered to the server in order to process those requests.


There are multiple ways with which we can add routes to our server handler.

  1. Adding routes based on different HTTP Methods
  2. Multiple HTTP Methods Registering

Adding routes based on different HTTP Methods

turbo supports all the basic HTTP methods needed to build an application, and you can follow the below ways on how to register a particular route

Any route that gets added using below format, contains 2 parameters

  1. the path of the API
  2. the handler which processes the request
  1. Add a GET route
router.Get("api/v1/users", getUsers)
  1. Add a POST route
router.Post("api/v1/users", addUser)
  1. Add a PUT route
router.Put("api/v1/users/:id", updateUser)
  1. Add a DELETE route
router.Delete("api/v1/users/:id", deleteUser)

Multiple HTTP Methods Registering

Router lets you register same handler with multiple methods such as ("POST", "PUT") for a single endpoint. With the help of Add function that can be achieved

router.Add("/api/v1/users", handleCustomers, "PUT", "POST")

Contributing

Project Details

Repository : golly-samples

This was an in-depth description of different ways you can register routes in turbo.
We will be discussing how to read query params, path params and adding filters to the turbo in the next post.

Stay Tuned!!🕵🏻‍♂️

And if you have any questions, I am happy to answer🚀