Back to blog
Building Modern APIs with Go and Gin
9/26/2026API Architecture
A well-structured API separates concerns into distinct layers. In Grit, we use a three-layer architecture:
- Handlers — Parse requests, validate input, return responses
- Services — Business logic, data transformation, orchestration
- 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