Ticker

6/recent/ticker-posts

Gin vs Mux for Creating APIs

Introduction

In this blog, I will be discussing the two popular frameworks of Golang i.e., Gin Web Framework and Gorilla Mux. Both frameworks have different roles to play in the world of Golang. Developers have other options as well to choose from but Gin and Mux have been popular throughout time and proved their worth of being popular among the developers. Gin and Mux are two open-source libraries that are frequently used to create APIs and online applications. I will be comparing their features so that you can be decisive on what to use and when to use them according to your project. Besides the theoretical explanation, I will also explain two APIs, one based on Gin Web Framework and the other based on Gorilla Mux. This would give you a clear picture of both frameworks and help you to understand the differences between them.


Gin vs Mux


Prerequisites


Before, proceeding towards the learning part, there are a few prerequisites that I would like to mention for the ease of learning and understanding the concept. Since Golang is used to build both Gin and Mux, you should be well-versed in it. As a result, it's crucial that you comprehend the language's syntax.


The fundamental ideas behind web development should be familiar to you. In order to understand how Gin and Mux are used to create web apps and APIs, it will be necessary to have a basic awareness of web development terms like routing, middleware, and request processing.


Since Gin and Mux both utilize HTTP to handle requests and answers, it's also crucial to be familiar with them. As a result, having a fundamental understanding of how HTTP functions will help you learn more from this blog. The topics covered in this blog should be easy for you to follow if you have a basic grasp of these requirements.


Also Read>>> NIMI MOCK TEST


API based on Mux


In this section of the blog, I will explain the code for API based on Gorilla Mux Framework. The explanation will be regarding only on POST method i.e., a CreateUser API. We will have the main directory in which, the main.go file and go.mod file will be placed and other than that, it includes the controller, database, and models folder that has the models.go, databasesetup.go and controller.go are included respectively.


In the main.go file, first I have mentioned the imports like “net/http”, “Mux”, and “controller” package. Proceeding forward, I have created the main function which is going to be the entry point for the program that states r as a variable with “mux.Newrouter init” then, I created a URL “/createuser” which calls the functions from the controller package. I have also used the POST method additionally.


Also Read>>> www.sdfcu.org


Then, with the “ListenAndServe” method, I instructed the program to start the server on port 8080.  Please refer to image 1  below for the code of main.go.


Gin vs Mux


Now, let us proceed toward, the controller part. In the controller, I mentioned the required packages and started by declaring a variable names Collection of type “*mongo.Collection” which has the database in it.


Gin vs Mux


Then, I created a function that, I called on main.go named as CreateUser. Here basically, the aforementioned controller code is in charge of managing the addition of new users to the system. CreateUser has "http.ResponseWriter" and "*http.Request" as arguments. The request body is read by the "JSON decoder". The request's JSON payload is then decoded and copied into the "models. User" object. A 400 Bad Request status code is returned if there is an issue with the request's decoding.


The user is then added to a MongoDB collection using the "InsertOne" method. When something goes wrong, it leaves with a 500 Internal Server Error status code. Please refer to image 2 for better clarification.


API based on GIN


Gin vs Mux

Here, I'll start with the main.go file and use the Gin Web Framework to build a POST API. The server in the code above establishes a route for a POST request to the URL "/createuser," which is handled by the "CreateUser" function from the "controllers" package and listens on port 8080. logger middleware is also used by the router. When the server is started, the "log.Fatal" function is utilized, and if there is a problem, it will quit the application with a non-zero exit code.


For the controller part, I have mentioned the packages and declared a variable UserCollection. The created a function CreateUser which creates a user. It takes the data from the request body and binds it to the User struct. If there's an error, it returns a JSON response with the error message. I have then used the insert method, A JSON response with a status code of 500, and an error message is returned if the insert operation is unsuccessful. If it is successful, a JSON response with a success message and status code 200 is returned. To communicate with the mongodb, it uses the go mongo driver. Please refer to image 4 for better clarity.


Gin vs Mux


Models & Database


Gin vs Mux

Moving on to the struct, there are three fields in it:


1) The Mongo Object ID of the type "primitive.ObjectID," which is used as the user's primary key in the MongoDB collection. However, I didn't utilize primitive for Mux. In order to understand well, please refer to Image 6.

2) A string name that reflects the user's name.

3) Type int age, which denotes the user's age.


Gin vs Mux


If you are unsure about how to link your application to the MongoDB database, you may read my previous blog post, "How to connect the Golang App with MongoDB", which covers this topic.


Conclusion


I'm sure you've observed that Mux and Gin are both capable Golang frameworks for creating web apps and APIs. Gin is renowned for its exceptional performance, concise and expressive syntax, integrated middleware, group routing, and integrated error handling. Mux, on the other hand, places a greater emphasis on flexibility and use. Although it lacks built-in middleware, group routing, or error handling, it is nevertheless a fantastic choice for tasks that call for more adaptability and simplicity. I must also include that, this blog may have given you an idea as an organization, about the scalability potential of golang. Therefore, this blog is the answer to your question that why you should hire golang developers for your projects.


Coming back to Gin and Mux, the decision between Gin and Mux will ultimately be based on the particular requirements of your project. It's also crucial to remember that each framework has its own set of benefits and drawbacks, and the golang web developer must eventually pick which one to employ depending on their particular use case and requirements. Hope the blog was helpful to you.


Also Read>>> Sesteel