golang-auth-api
AGENTS.md130 starsChanged 16 months agoMITView on GitHub
This guide provides comprehensive instructions for AI coding agents working on the Auth API repository. Language: Go 1.23+ Framework: Gin for HTTP handling Database: PostgreSQL with GORM Caching: Redis Project Type: Modular REST API (Clean Architecture) This project has OpenCode skills that give you instant context about the codebase. Always load relevant skills before exploring files manually. This saves significant time and avoids redundant discovery. Load the project-map skill immediately. It contains the full module inventory, dependency graph, file paths,…
- Reads credentials
We split this AGENTS.md into the rules it gives an agent
22 rules. About 1 is common, 13 are distinctive, and 8 are very specific to this project.
≈1
Common · estimate
Many files like this have a rule of this kind.
≈13
Distinctive · estimate
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- could be automatic: a setting could enforce it
- Distinctive: line 137
Use interfaces for abstraction in service/repository layers
- Very specific: line 139
Use DTOs in pkg/dto/ for API request/response contracts
- Distinctive: line 140
All struct fields must have JSON tags for API endpoints
- Distinctive: line 153
Never expose raw database errors to clients: Follow the pattern:
- Very specific: line 165
Use custom error types from pkg/errors/:
- Distinctive: line 177
All services use constructor functions:
- Very specific: line 198
Use go-playground/validator struct tags:
- Distinctive: line 228
Always document HTTP handlers with Swagger annotations:
- Distinctive: line 245
After changes, regenerate docs:
- Distinctive: line 253
Use the log service for audit trails:
- Distinctive: line 297
Token blacklisting: Use Redis for logout functionality
- Common: line 298
Password hashing: Use bcrypt (never store plaintext)
- Distinctive: line 309
Always validate DTOs with struct tags + validator
- Distinctive: line 311
Use parameterized queries only (GORM handles automatically)
- Very specific: line 366
Check .env is configured with database/Redis credentials
- Very specific: line 367
Run make docker-dev to start PostgreSQL and Redis (optional, or use local services)
- Very specific: line 368
Run make setup to install Air and dependencies
- Very specific: line 372
Use make dev for hot-reload server
- Distinctive: line 373
Write tests alongside implementation
- Very specific: line 374
Run make fmt and make lint before committing
- Distinctive: line 375
Document HTTP endpoints with Swagger tags
- Distinctive: line 384
Follow commit message format above
≈ “Common” is an estimate: at least 1 in 5 of the 36,368 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AGENTS.md - Agentic Coding Guidelines
This guide provides comprehensive instructions for AI coding agents working on the Auth API repository.
Quick Reference
Language: Go 1.23+
Framework: Gin for HTTP handling
Database: PostgreSQL with GORM
Caching: Redis
Project Type: Modular REST API (Clean Architecture)
Session Startup — Load Skills Before Exploring
This project has OpenCode skills that give you instant context about the codebase. Always load relevant skills before exploring files manually. This saves significant time and avoids redundant discovery.
First action in every session
Load the project-map skill immediately. It contains the full module inventory, dependency graph, file paths, and architecture overview:
skill("project-map")Before working in a specific domain
Load the relevant skill before reading source files:
| Task area | Skill to load | What it covers |
| Routes, endpoints, middleware | route-map | All 200+ HTTP routes, auth layers, rate limits, handler mappings |
| Database models, migrations | data-model | All 17 GORM models, fields, relationships, indexes, ER diagram |
| Auth, JWT, login, 2FA, sessions | auth-flows | 4 auth systems, token lifecycle, RBAC, session management |
| Email, templates, SMTP | email-system | Template resolution chain, 3 rendering engines, variable pipeline |
| Admin GUI, HTMX templates | admin-gui | HTMX GUI structure, template rendering, CRUD patterns, CSRF |
Example workflow
1. skill("project-map") — Understand overall structure
2. skill("auth-flows") — Deep dive into auth before modifying login
3. Read specific files as needed — Now you know exactly which files to openDo NOT spend time grepping or globbing to discover the codebase structure when a skill already provides that information.
Build, Lint, Test Commands
Development
make dev # Hot reload development server (uses Air)
make run # Run application once
make setup # Install dependencies and AirTesting
make test # Run all tests
make test-totp # Run TOTP test (requires TEST_TOTP_SECRET env var)Single Test Execution
go test -v ./internal/user -run TestRegister # Specific test
go test -v ./internal/user -run TestRegister -count=1 # No caching
go test -v -race ./internal/user # Race detectorCode Quality
make fmt # Format code (go fmt)
make lint # Run golangci-lint
make security # Run gosec + nancy vulnerability scansBuild & Production
make build # Build binary for current OS
make build-prod # Cross-compile for Linux (CGO_ENABLED=0)Database
make migrate-up # Apply pending migrations
make migrate-down # Rollback last migration
make migrate-status # Show current schema
make migrate-backup # Create database backupCode Style Guidelines
Imports Organization
// Standard library
import (
"context"
"fmt"
"net/http"
)
// Third-party packages
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// Internal packages
import (
"github.com/gjovanovicst/auth_api/internal/user"
"github.com/gjovanovicst/auth_api/pkg/dto"
)Formatting & Naming
- Functions: CamelCase starting with verb (
NewService,GetByID) - Constants: UPPER_SNAKE_CASE for constants
- Private functions: camelCase prefix (e.g.,
validateEmail) - Exported types: PascalCase (e.g.,
UserService) - File names: snake_case (e.g.,
handler.go,repository.go)
Type System
- Use interfaces for abstraction in service/repository layers
- Define domain models in
pkg/models/with GORM tags - Use DTOs in
pkg/dto/for API request/response contracts - All struct fields must have JSON tags for API endpoints
type User struct {
ID uint `gorm:"primaryKey" json:"id"`
Email string `gorm:"unique;not null" json:"email"`
Password string `gorm:"not null" json:"-"` // Hidden from responses
CreatedAt time.Time `json:"created_at"`
}Error Handling
Never expose raw database errors to clients. Follow the pattern:
if err != nil {
// Log internal error if needed
c.JSON(http.StatusInternalServerError, dto.ErrorResponse{
Error: "An error occurred", // Generic message
})
return
}Use custom error types from pkg/errors/:
if err != nil {
appErr := errors.NewAppError(errors.ErrConflict, "Email already registered")
c.JSON(appErr.Code, gin.H{"error": appErr.Message})
return
}Dependency Injection
All services use constructor functions:
// Repository layer
func NewRepository(db *gorm.DB) Repository {
return &repository{db: db}
}
// Service layer
func NewService(repo Repository, cache Cache) Service {
return &service{repo: repo, cache: cache}
}
// Handler layer
func NewHandler(service Service) *Handler {
return &Handler{Service: service}
}Validation
Use go-playground/validator struct tags:
type LoginRequest struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required,min=8,max=128"`
}
validate := validator.New()
if err := validate.Struct(req); err != nil {
c.JSON(http.StatusBadRequest, dto.ErrorResponse{Error: err.Error()})
return
}Database Queries
// Always parameterize queries (GORM handles this automatically)
var user models.User
if err := r.db.Where("email = ?", email).First(&user).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil // Not found, no error
}
return nil, err
}Swagger Documentation
Always document HTTP handlers with Swagger annotations:
// @Summary User login
// @Description Authenticate user with email and password
// @Tags authentication
// @Accept json
// @Produce json
// @Param request body dto.LoginRequest true "Login credentials"
// @Success 200 {object} dto.LoginResponse
// @Failure 400 {object} dto.ErrorResponse
// @Router /login [post]
func (h *Handler) Login(c *gin.Context) {
// Implementation
}After changes, regenerate docs:
make swag-initLogging & Activity Tracking
Use the log service for audit trails:
logService.CreateLog(ctx, "USER_LOGIN", "User logged in successfully",
userID, "INFO")Architecture Patterns
Repository-Service-Handler Layer
- Repository: Data access, database queries
- Service: Business logic, validation, orchestration
- Handler: HTTP transport, request binding, response formatting
Directory Structure
internal/
├── auth/ # Authentication domain
├── user/ # User management domain
├── social/ # OAuth2 providers
├── twofa/ # Two-factor authentication
├── log/ # Activity logging
├── middleware/ # HTTP middleware (auth, CORS, etc.)
├── database/ # Database connection & migrations
├── redis/ # Redis session management
└── config/ # Configuration loading
pkg/
├── models/ # GORM database models
├── dto/ # API request/response DTOs
├── jwt/ # JWT utilities
└── errors/ # Custom error typesSecurity Patterns (from SECURITY.md)
Authentication
- JWT tokens: 15-minute access, 720-hour refresh (configurable)
- Token blacklisting: Use Redis for logout functionality
- Password hashing: Use bcrypt (never store plaintext)
- 2FA/TOTP: Support for authenticator apps + recovery codes
Authorization
- JWT validation middleware in
internal/middleware/auth.go - Extract user context:
c.Get("user_id")after auth middleware - Admin routes prepared for role-based access control
Input Validation
- Always validate DTOs with struct tags + validator
- Sanitize database inputs (GORM prevents SQL injection)
- Use parameterized queries only (GORM handles automatically)
Security Scanning
Before committing:
make security # Run gosec + nancyConfiguration in .gosec.json with Medium severity threshold.
Commit Message Guidelines (from Cursor Rules)
Format
<type>(<scope>): <description>
[optional body]
[optional footer]Types
feat: New featurefix: Bug fixsecurity: Security patch (especially important for auth APIs)docs: Documentationrefactor: Code restructuring (no logic changes)test: Adding/updating testschore: Build, dependencies
Scopes
auth, user, social, twofa, email, middleware, database, redis, log, api, models, dto, jwt
Examples
feat(auth): add JWT token blacklisting for logout
fix(middleware): prevent auth bypass with malformed tokens
security(password): enforce bcrypt cost >= 12
test(user): add registration validation tests
docs(api): update Swagger for 2FA endpointsDevelopment Workflow
Before Starting
- Check
.envis configured with database/Redis credentials - Run
make docker-devto start PostgreSQL and Redis (optional, or use local services) - Run
make setupto install Air and dependencies
During Development
- Use
make devfor hot-reload server - Write tests alongside implementation
- Run
make fmtandmake lintbefore committing - Document HTTP endpoints with Swagger tags
Before Committing
make test- Ensure all tests passmake fmt- Format codemake lint- Check linting rulesmake security- Run security scansmake swag-init- Update Swagger docs (if API changed)- Follow commit message format above
Key Files Reference
- Entry point:
cmd/api/main.go(dependency injection, route setup) - Database models:
pkg/models/ - DTOs:
pkg/dto/ - Error types:
pkg/errors/errors.go - JWT utilities:
pkg/jwt/jwt.go - Configuration: Viper-based in
cmd/api/main.gowith.envfile - Swagger docs: Auto-generated in
docs/directory
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.
Of the AGENTS.md files we read, the 28,693 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository7 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/api-development.mdcCursor rule
- .cursor/rules/code-patterns.mdcCursor rule
- .cursor/rules/commit-messages.mdcCursor rule
- .cursor/rules/development-workflow.mdcCursor rule
- .cursor/rules/project-structure.mdcCursor rule
- .cursor/rules/security-patterns.mdcCursor rule
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

