DEV Community

Aditya
Aditya

Posted on • Originally published at adityakumar.io

Deep dive into Golly: Turbo (Part - 2)

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()
Enter fullscreen mode Exit fullscreen mode

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,
}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
  1. Add a POST route
router.Post("api/v1/users", addUser)
Enter fullscreen mode Exit fullscreen mode
  1. Add a PUT route
router.Put("api/v1/users/:id", updateUser)
Enter fullscreen mode Exit fullscreen mode
  1. Add a DELETE route
router.Delete("api/v1/users/:id", deleteUser)
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Contributing

  • To contribute to samples, please follow the Repository
  • Any suggestions to improve the library are welcomed, we want this to be a community driven project and help it grow.
  • If you feel any improvements in the package, feel free to raise the PR - Project
  • If you find any bugs that we may have missed, feel free to raise the issues - Raise an Issue
  • Looking forward to your contributions..

Project Details

golly-samples

GitHub logo 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
Enter fullscreen mode Exit fullscreen mode

Examples

This repository is organized by individual libraries within Golly. Each directory contains…




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🚀

Top comments (0)