Grit Docsv3.114.0

Batteries Overview

Built-in services for cache, storage, email, background jobs, cron, and AI.

Overview

Grit comes with "batteries included" — production-ready services that are scaffolded with every project. All services follow a graceful degradation pattern: if the backing service (Redis, MinIO, etc.) is unavailable, the app logs a warning but continues running.

Redis Cache

Files: internal/cache/, internal/middleware/cache.go

A Redis-based caching service with HTTP middleware:

// Cache a value
cache.Set(ctx, "key", value, 5*time.Minute)

// Get a cached value
val, err := cache.Get(ctx, "key")

// Delete a cached value
cache.Delete(ctx, "key")

The cache middleware automatically caches GET responses:

r.GET("/api/posts", middleware.Cache(cacheService, 5*time.Minute), handler.GetPosts)

S3 File Storage

Files: internal/storage/, internal/handlers/upload_handler.go

S3-compatible file storage with image processing:

  • Upload files to MinIO (dev) or S3/R2/B2 (production)
  • Automatic image resizing with configurable dimensions
  • Secure pre-signed URLs for downloads
  • Admin file browser at /system/files

Upload endpoint: POST /api/uploads (multipart form data)

Email (Resend)

Files: internal/mail/

Email service powered by Resend with 4 HTML templates:

  • Welcome — New user registration
  • Password Reset — Reset link email
  • Notification — Generic notifications
  • Invoice — Payment/billing emails
mailer.Send(ctx, mail.Message{
    To:       "user@example.com",
    Subject:  "Welcome!",
    Template: "welcome",
    Data:     map[string]string{"name": "John"},
})

Preview emails at /system/mail in the admin panel.

Background Jobs (asynq)

Files: internal/jobs/

Redis-based background job processing:

  • Email jobs — Send emails asynchronously
  • Image jobs — Process uploaded images
  • Cleanup jobs — Remove expired data
// Enqueue a job
client.Enqueue(jobs.NewEmailJob("user@example.com", "welcome"))

Monitor jobs at /system/jobs in the admin panel.

Cron Scheduler

Files: internal/cron/

Scheduled task execution built on asynq:

  • Define cron tasks with standard cron expressions
  • View scheduled tasks at /system/cron in the admin panel

AI Integration

Files: internal/ai/

Dual AI provider support:

  • Claude (Anthropic) — via the Anthropic API
  • OpenAI — via the OpenAI API

Features:

  • Text generation with streaming support
  • Configurable via environment variables
  • Admin handler for AI operations
# .env configuration (Vercel AI Gateway)
AI_GATEWAY_API_KEY=your-key        # One key for all models
AI_GATEWAY_MODEL=anthropic/claude-sonnet-4-6  # provider/model format

Configuration

All batteries are configured via environment variables in .env:

# Redis
REDIS_URL=localhost:6380

# S3 Storage
S3_ENDPOINT=localhost:9002
S3_ACCESS_KEY=minioadmin
S3_SECRET_KEY=minioadmin
S3_BUCKET=uploads
S3_REGION=us-east-1

# Email
RESEND_API_KEY=re_...
MAIL_FROM=noreply@example.com

# AI (Vercel AI Gateway — one key, hundreds of models)
AI_GATEWAY_API_KEY=your-key
AI_GATEWAY_MODEL=anthropic/claude-sonnet-4-6

Docker Services

All backing services are included in docker-compose.yml:

ServicePortPurpose
PostgreSQL5432Database
Redis6379Cache + Queue
MinIO9000/9001File storage
Mailhog8025Email testing

On this page