The GoFr Effect: How to Develop APIs in Record Time
Dreading lengthy API development cycles? In today’s fast-paced world, time is of the essence. That’s where GoFr comes in! This innovative framework empowers developers to build robust APIs with incredible speed and efficiency.
This article dives deep into the world of GoFr, exploring how it streamlines API creation. Discover:
- The power of simplicity: GoFr’s intuitive syntax lets you focus on functionality, not boilerplate code.
- Rapid prototyping: Build basic APIs in a flash, allowing for quick iteration and testing.
- Built-in features: GoFr comes pre-equipped with essential functionalities like routing, validation, and error handling.
- Focus on performance: GoFr is built for speed, ensuring your APIs handle requests with lightning-fast response times.
Whether you’re a seasoned Go developer or just starting out, GoFr can revolutionise your API development process. Learn how to harness its power and get your APIs up and running in record time!
API Development
I am assuming that you already have a working environment setup for go on your system.
Lets dive deep and explore how easy it is to build a production level micro-service in few seconds.
Setting up the project
- Create a directory for you project
mkdir gofr-sample-app
- Initialise your go module
go mod init gofr-sample-app
- Get GoFr as a dependency
go get gofr.dev
Connecting to DB
- Create a new directory
configs
in your project and a new file.env
within it. - Add below configs to the .env file:
APP_NAME=gofr-sample-app
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
DB_NAME=employees
DB_PORT=2001
DB_DIALECT=mysql
- Make sure you have a docker instance running in your local for MySQL, if not you can start it with the below command:
docker run --name gofr-mysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=employees -p 2001:3306 -d mysql:8.0.30
Starting the application
- Create a new file
main.go
at the root of your project. And add below code to the file:
package main
import "gofr.dev/pkg/gofr"
func main() {
app := gofr.New()
type employee struct {
Id int
Name string
Age int
Designation string
}
app.AddRESTHandlers(&employee{})
app.Run()
}
- Now you can run your application, using the below command:
go run main.go
Voila, you have now a fully functional CRUD micro-service for employee management.
- You can access the endpoints (`/employee`, and `/employee/{id}`) from localhost:9000
GoFr also exports metrics which can be observed from localhost:2121/metrics.
GoFr has many more exiting features, which helps in developing blazing fast APIs. Want to explore them? Visit gofr.dev
You can also go through the article on Why use GoFr by Aryan Mehrotra.
Happy Coding!