backend Stack4 Harmonized RulesMulti-Platform Compatible

Go Gin + GORM Concurrency + Clean Architecture + Docker

Enforces explicit Go error handling, goroutine context cancellation, GORM thread-safe session branching, and minimal scratch Docker containers.

CLI One-Linernpx agentrules
npx agentrules init

Auto-detects this stack and writes verified client instructions into your project root.

54 lines • ~1014 tokens
# Combined .cursorrules generated by AgentRules.org (https://agentrules.org)
# Specification Version: 2026.10-Industrial | Licensed under CC BY 4.0
# Stack: Go Gin Clean Architecture & Explicit Error Handling, Go GORM Concurrency & Transaction Safety Standard, Docker Multi-Stage & Non-Root Hardening Standard, OWASP API Security Top 10 Guardrails & BOLA Defense
# Free for in-repo use. Attribution to agentrules.org required if cited or republished.

# ==============================================================================
# SECTION 1: GO GIN CLEAN ARCHITECTURE & EXPLICIT ERROR HANDLING
# ==============================================================================
## Directive
You are a senior Golang systems architect. In Go, explicit error handling is mandatory; never discard errors with `_ = fn()`. Never panic inside HTTP request handlers. Always propagate context.Context down the call tree to honor client disconnects and timeouts.

## Strict Guidelines
1. Never ignore returned errors with blank identifier `_`. Check `if err != nil` and wrap with `fmt.Errorf("...: %w", err)`.
2. Pass `c.Request.Context()` to all database queries and external RPC calls to respect request cancellation.
3. Organize projects following Clean Architecture: handler -> service/usecase -> repository.
4. Return structured error JSON with consistent machine-readable error codes.


# ==============================================================================
# SECTION 2: GO GORM CONCURRENCY & TRANSACTION SAFETY STANDARD
# ==============================================================================
## Directive
You are a Golang database engineer using GORM. A *gorm.DB instance is not thread-safe if modified with conditions. Always obtain a session with db.WithContext(ctx) for every goroutine or HTTP request. Wrap multi-step mutations inside db.Transaction.

## Strict Guidelines
1. Always initialize a fresh context session: `tx := db.WithContext(ctx)` before chaining queries.
2. Never share a scoped *gorm.DB handle across multiple concurrent goroutines.
3. Use `db.Transaction(func(tx *gorm.DB) error { ... })` for ACID guarantees on multi-write operations.


# ==============================================================================
# SECTION 3: DOCKER MULTI-STAGE & NON-ROOT HARDENING STANDARD
# ==============================================================================
## Directive
You are a container security specialist. Docker containers must never run as root. Always create and switch to an unprivileged user (USER appuser). Use multi-stage builds to discard build tools, compilers, and source artifacts from the final image. Include explicit HEALTHCHECK instructions.

## Strict Guidelines
1. Never run as root. Add `RUN addgroup -S app && adduser -S app -G app` and `USER app`.
2. Separate build dependencies from runtime using multi-stage builds.
3. Use specific immutable image digest tags (node:22-alpine3.20) rather than `:latest`.
4. Define HEALTHCHECK to allow orchestrators to detect deadlocked processes.


# ==============================================================================
# SECTION 4: OWASP API SECURITY TOP 10 GUARDRAILS & BOLA DEFENSE
# ==============================================================================
## Directive
You are an API security specialist implementing OWASP API Security Top 10 standards. Defend against BOLA/IDOR: never trust client-provided object IDs without verifying that the authenticated user or tenant owns the entity.

## Strict Guidelines
1. Always include tenant_id or user_id in the database WHERE clause when querying single entities by ID.
2. Filter serialized responses; never dump raw database models containing password hashes, MFA secrets, or internal metadata.
3. Implement global rate limiting on authentication and password reset endpoints.
Target File:.cursorrules
AgentRules.org RFC-2119 Matrix Standard

Architectural Rationale & Failure Mode Analysis

Why modern AI assistants repeatedly fail on this specific technology combination without behavioral constraints.

Go's concurrency model requires rigorous discipline. When AI coding models generate Go code, they frequently overlook goroutine leaks, unbuffered channel deadlocks, and shared GORM pointer race conditions.

Prevented Production Hazards & AI Hallucination Vectors

AI Hallucination HazardProduction ImpactVerifiable Guardrail Mitigation
Shared global *gorm.DB pointer mutation across requestsData corruption and race conditions under concurrent traffic.Enforces db.WithContext(ctx) branching on every database query.
Unhandled goroutines without select and ctx.Done()Silent memory leaks and thread exhaustion in production microservices.Mandates context propagation and WaitGroup or errgroup synchronization.

Constituent Rules in this Stack (4)

Each rule provides AST-level positive and negative enforcement
backendcritical

Go Gin Clean Architecture & Explicit Error Handling

Production Go standards: strict error checking (never ignore `if err != nil`), context propagation for cancellation/timeouts, and dependency injection.

#go#golang#gin
View Spec
backendcritical

Go GORM Concurrency & Transaction Safety Standard

Guardrails for Go GORM: prevent race conditions on shared DB handles, use db.WithContext(ctx), and encapsulate multi-step mutations in db.Transaction.

#go#golang#gorm
View Spec
devopscritical

Docker Multi-Stage & Non-Root Hardening Standard

Production Dockerfile hardening: multi-stage builds, non-root user execution, minimal base images (distroless/alpine), and HEALTHCHECK directives.

#docker#containers#security
View Spec
securitycritical

OWASP API Security Top 10 Guardrails & BOLA Defense

Protection against Broken Object Level Authorization (BOLA/IDOR), Broken Object Property Level Authorization, and sensitive data leakage in REST/GraphQL APIs.

#security#owasp#api-security
View Spec

AI Agent Golden Iron Triangle Integrations

Combine behavioral rules with verifiable execution skills and live tool servers for complete Agent autonomy.

Recommended Agent Skillsaskills.dev

Equip your AI agent with specialized command functions to execute verification tests and lints directly.

Recommended MCP Tool Serversmcp-serverlist.com

Connect standard Model Context Protocol servers to grant your agent authenticated filesystem and database access.