In today’s fast-paced world of web development, creating efficient and scalable APIs is crucial. Go, with its simplicity and performance, has become a popular choice for backend development. In this tutorial, we’ll explore how to build a robust API using Go and the GoFr framework.

Why Go and GoFr?

Go’s concurrency model and performance make it an excellent choice for building scalable web services. GoFr, a lightweight and high-performance web framework for Go, simplifies the API development process while maintaining the speed and efficiency Go is known for.

Introduction to GoFr

GoFr is a lightweight, high-performance web framework for Go that simplifies the process of building APIs. It provides a clean and intuitive API, making it easy for developers to create scalable web applications quickly.

Project Overview: Task Management API

We’ll build a Task Management API that allows users to create, read, update, and delete tasks. This project will demonstrate the key features of GoFr and how to structure a Go API project.

Setting Up Your Development Environment

First, ensure you have Go installed on your system. Then, create a new directory for your project and initialize a Go module:

mkdir task-api
cd task-api
go mod init github.com/hungaikev/task-api

Install GoFr and other necessary dependencies:

go get gofr.dev
go get github.com/rs/zerolog

Setting Up the Project

First, let’s set up our project structure:

task-api/
├── cmd/
│   └── main.go
├── internal/
│   ├── handlers/
│   │   └── tasks.go
│   ├── models/
│   │   └── task.go
│   ├── store/
│   │   └── store.go
│   └── tracing/
│       └── tracing.go
├── go.mod
└── go.sum

Initialize your Go module:

go mod init github.com/hungaikev/task-api

Install GoFr and other dependencies:

go get gofr.dev
go get github.com/rs/zerolog

Implementing the Store

Let’s look at the store.go file, which handles database operations:

package store

import (
    "database/sql"
    "errors"
    "github.com/hungaikev/task-api/internal/models"
    "github.com/rs/zerolog/log"
    "gofr.dev/pkg/gofr"
    "time"
)

type TaskStore interface {
    CreateTask(ctx *gofr.Context, task *models.Task) error
    GetTasks(ctx *gofr.Context) ([]models.Task, error)
    GetTask(ctx *gofr.Context, id int) (*models.Task, error)
    UpdateTask(ctx *gofr.Context, task *models.Task) error
    DeleteTask(ctx *gofr.Context, id int) error
}

type GoFrTaskStore struct{}

func NewTaskStore() TaskStore {
    return &GoFrTaskStore{}
}

// Implement CRUD methods here...

This file defines the TaskStore interface and implements it with GoFrTaskStore. Each method uses GoFr’s context to access the database.

Setting Up the Main Application

Now, let’s look at the main.go file:

package main

import (
    "github.com/hungaikev/task-api/internal/handlers"
    "github.com/hungaikev/task-api/internal/store"
    "github.com/hungaikev/task-api/internal/tracing"
    "os"

    "github.com/rs/zerolog"
    "github.com/rs/zerolog/log"
    "gofr.dev/pkg/gofr"
)

func main() {
    // Configure zerolog
    zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
    log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

    // Initialize tracer
    cleanup := tracing.InitTracer()
    defer cleanup()

    // Initialize GoFr app
    app := gofr.New()

    // Initialize store
    taskStore := store.NewTaskStore()

    // Initialize handlers
    taskHandler := handlers.NewTaskHandler(taskStore)

    // Routes
    app.POST("/tasks", taskHandler.CreateTask)
    app.GET("/tasks", taskHandler.GetTasks)
    app.GET("/tasks/{id}", taskHandler.GetTask)
    app.PUT("/tasks/{id}", taskHandler.UpdateTask)
    app.DELETE("/tasks/{id}", taskHandler.DeleteTask)

    // Start the server
    log.Info().Msg("Starting server")
    app.Start()
}

This file sets up the GoFr application, initializes the store and handlers, defines the routes, and starts the server.

Implementing Handlers

In the handlers package, you’ll implement the HTTP handlers for each route. Each handler will use the TaskStore to interact with the database.

Running the Application

To run your application:

go run cmd/main.go

Your API will now be running, typically on http://localhost:8000.

Testing the API

You can test your API using curl or any API testing tool. Here are some example requests:

  1. Create a task:
    curl -X POST http://localhost:8000/tasks \
      -H "Content-Type: application/json" \
      -d '{"title": "Learn GoFr", "description": "Build an API with GoFr", "status": "TODO"}'
    
  2. Get all tasks:
    curl http://localhost:8000/tasks
    
  3. Get a specific task:
    curl http://localhost:8000/tasks/1
    
  4. Update a task:
    curl -X PUT http://localhost:8000/tasks/1 \
      -H "Content-Type: application/json" \
      -d '{"title": "Learn GoFr", "description": "Build an API with GoFr", "status": "IN_PROGRESS"}'
    
  5. Delete a task:
    curl -X DELETE http://localhost:8000/tasks/1
    

Conclusion

In this tutorial, we’ve built a basic Task Management API using Go and the GoFr framework. We’ve covered:

  • Setting up a GoFr project
  • Implementing database interactions
  • Creating the main application structure
  • Defining API routes

GoFr simplifies many aspects of API development, allowing you to focus on your business logic rather than boilerplate code. As you continue to develop your API, consider adding features like:

  • Authentication and authorization
  • More comprehensive error handling
  • Data validation
  • API documentation using Swagger

The GoFr framework provides a solid foundation for building scalable and maintainable APIs in Go. Happy coding!


Need Help?

Are you facing challenging problems, or need an external perspective on a new idea or project? I can help! Whether you're looking to build a technology proof of concept before making a larger investment, or you need guidance on difficult issues, I'm here to assist.

Services Offered:

  • Problem-Solving: Tackling complex issues with innovative solutions.
  • Consultation: Providing expert advice and fresh viewpoints on your projects.
  • Proof of Concept: Developing preliminary models to test and validate your ideas.

If you're interested in working with me, please reach out via email at [email protected].

Let's turn your challenges into opportunities!


Buy Me A Coffee