0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Why Use GoFr for Golang Backend?

Posted at

Why use GoFr for Golang Backend?

GoFr is an opinionated Go Framework for accelerated microservice development, according to its official website, it features and promises battle tested code on actual production environment, it has…

Image

About GoFr framework

GoFr is an opinionated Go Framework for accelerated microservice development, according to its official website, it features and promises battle tested code on actual production environment, it has built in observability tools such as health-check and heartbeat URLs which suffice Kubernetes liveness and readiness probe, tracing, metrics, and structured logs with different level to monitor your application.

It also has multiple different data sources support such as mysql, postgres, kafka, google, mqtt etc.

By all means it makes sure, you just need to focus on your business’s core logic, while it makes sure, your applications are production ready from the time of POC.

Why did I choose GoFr instead of implementing http server by myself or using any other framework?

It removed my worries about making my application consistent and allowing me to observe my application while I can focus on my business logic.

  1. REST API Principles: GoFr ensures adherence to REST principles with its structured framework, simplifying API design.
  2. Logging: Built-in logging features facilitate easy monitoring of incoming requests.
  3. Metrics and Tracing: GoFr supports effortless integration of metrics and tracing, aiding performance monitoring and request tracing.
  4. CORS Configuration: Easily configure Cross-Origin Resource Sharing (CORS) policies through GoFr’s built-in options.
  5. Dynamic Log Level: Change log levels remotely without application restarts, enhancing operational flexibility.
  6. Kubernetes Compatibility: Built-in health check and heartbeat endpoints support Kubernetes deployments.
  7. Database Integration: Native support for popular databases simplifies database connection management.
  8. IoT Integration: Native MQTT support and default connection to HiveMQ enable seamless IoT integration.

Apart from that, the best one among all of these is the way it takes very less lines of code to register a route, while it is doing a lot of other things.

These features are something which are not available in any of the popular frameworks like Gin, Echo, Beego, Fiber.

Sample Route Registration

package main  
  
import "gofr.dev/pkg/gofr"  
  
func main() {  
 // Create a new application  
 a := gofr.New()  
  
 // Add the routes  
 a.GET("/hello", func (c \*gofr.Context) (interface{}, error) {  
 return “Hello World”, nil  
})  
  
 // Run the application  
 a.Run()  
}

Just with three lines and a single primary dependency our Hello World application is production ready.

Logs on app startup with app and metrics port

We get the following well formatted response when we hit our /hello endpoint.

Response from GoFr server

Kubernetes liveness and readiness probe

GoFr by default create routes for liveness and readiness probe where in on /.well-known/healthendpoint it checks the status of all datasources, services connected to the current service.

Health Check Response

Where, for /.well-known/alive endpoint it checks if the current service is accessible

Aliveness Response

Tracing with GoFr

By adding the config `TRACE_EXPORTER=gofr` in the config file .env.
Tracing was enabled and traces were being exported to tracer.gofr.dev.

Tracing on accessing /hello endpoint

And we see the traces for our service, a more complicated sample of this where we can see how our request flows when multiple different endpoints are hit within a same request.

Trace with Redis

Visualising Metrics

In every GoFr application metrics are exported on port 2121at /metrics endpoint, which can be visualized on grafana using GoFr’s Grafana Dashboard.

Dashboard with simulated production system.

System Information with no of Go-routines and memory utilization of application.

Request Response time along with Latency Distribution Graph

Request Count over time and different status codes returned

It also contains metrics for different data sources connected to the application.

Response time and latency for outbound requests

Redis query count along with count based on different commands

Similar, graphs are available for different PubSub systems, SQL databases etc.

Errors in GoFr

We just need to return the error received, and GoFr by default formats the error in the following format.
An error returned from MySQL is being depicted in the following example:

{  
    "error": {  
        "message": "Error 1146 (42S02): Table 'test\_db.EMPLOYEE' doesn't exist"  
    }  
}

Conclusion

While writing code using gofr, I didn’t worry about minor things and focussed on my main business logic which not only increased my productivity, but I was able to monitor my application, which was not possible by using any other framework, or relying on default golang packages.

Moreover, I didn’t compromise on performance as from the following article GoFr vs Echo: A Performance and Ease of Use Comparison.
GoFr surpassed Echo’s performance if I had to setup tracing metrics logging and other features myself.

Support GoFr by giving a ⭐star: https://github.com/gofr-dev/gofr

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?