Back to blog

Building Modern APIs with Go and Gin

9/26/2026

API Architecture

A well-structured API separates concerns into distinct layers. In Grit, we use a three-layer architecture:

  1. Handlers — Parse requests, validate input, return responses
  2. Services — Business logic, data transformation, orchestration
  3. Models — Database schema, relationships, hooks

Example: Creating a Resource

When you run grit generate resource post, Grit creates all three layers automatically. Each handler is thin — it delegates to the service layer:

func (h *PostHandler) Create(c *gin.Context) {
    var input CreatePostInput
    if err := c.ShouldBindJSON(&input); err != nil {
        c.JSON(400, gin.H{"error": err.Error()})
        return
    }
    post, err := h.Service.Create(&input)
    // ...
}

Middleware Stack

Grit includes production-ready middleware out of the box:

  • Auth — JWT token validation with automatic refresh
  • CORS — Configurable cross-origin resource sharing
  • Logger — Structured request logging
  • Cache — Redis-backed response caching