agentleFS
gjovanovicstgjovanovicst/golang-auth-api/.cursor/rules/api-development.mdc

golang-auth-api / rules

Cursor rule130 starsChanged 16 months agoMITView on GitHub

API development

We split this api-development.mdc into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Add to appropriate group in cmd/api/main.go:

    line 61
  2. Very specific:

    Use go-playground/validator tags in DTOs

    line 107
  3. Distinctive:

    Use structured logging format

    line 114
  4. Very specific:

    Use GORM models from pkg/models/

    line 118

≈ “Common” is an estimate: at least 1 in 5 of the 6,350 Cursor rule 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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
api-development.mdc
---
description: API development
globs: 
alwaysApply: false
---
# API Development Patterns

## Route Organization in Main
Routes are defined in [cmd/api/main.go](mdc:cmd/api/main.go) with clear groupings:

### Public Routes
- No authentication required
- Registration, login, password reset
- Social OAuth callbacks

### Protected Routes  
- Require JWT authentication via `middleware.AuthMiddleware()`
- User profile, logout, 2FA management
- Activity logs

### Admin Routes
- Protected with auth middleware
- Future role-based access control

## Adding New API Endpoints

### 1. Create DTOs
Define request/response structures in `pkg/dto/`:
```go
type CreateSomethingRequest struct {
    Name        string `json:"name" validate:"required" example:"Sample Name"`
    Description string `json:"description,omitempty" example:"Sample description"`
}

type SomethingResponse struct {
    ID          uint   `json:"id" example:"1"`
    Name        string `json:"name" example:"Sample Name"`
    CreatedAt   string `json:"created_at" example:"2023-01-01T00:00:00Z"`
}
```

### 2. Add Handler with Swagger Annotations
```go
// CreateSomething creates a new something
// @Summary Create something
// @Description Create a new something with the provided data
// @Tags something
// @Accept json
// @Produce json
// @Param request body dto.CreateSomethingRequest true "Something data"
// @Success 201 {object} dto.APIResponse{data=dto.SomethingResponse}
// @Failure 400 {object} dto.APIResponse
// @Security ApiKeyAuth
// @Router /something [post]
func (h *Handler) CreateSomething(c *gin.Context) {
    // Implementation
}
```

### 3. Register Route
Add to appropriate group in [cmd/api/main.go](mdc:cmd/api/main.go):
```go
protected.POST("/something", handler.CreateSomething)
```

### 4. Regenerate Documentation
```bash
make swag-init
```

## Authentication Patterns

### JWT Middleware
- Applied to protected routes in [cmd/api/main.go](mdc:cmd/api/main.go)
- Validates JWT tokens and extracts user information
- Sets user context for handlers

### 2FA Flow
- Temporary tokens for 2FA verification
- Separate verification endpoint
- Recovery code support

### Social Authentication
- OAuth2 flow with state verification
- Provider-specific callbacks
- User linking/creation logic

## Response Format Standards

### Success Response
```go
{
  "success": true,
  "data": {...}
}
```

### Error Response
```go
{
  "success": false,
  "error": "descriptive error message"
}
```

## Input Validation
- Use `go-playground/validator` tags in DTOs
- Validate in handlers before service calls
- Return appropriate HTTP status codes

## Activity Logging
- Log security-relevant events
- Include user ID, IP address, user agent
- Use structured logging format
- Examples: login attempts, password changes, 2FA events

## Database Operations
- Use GORM models from `pkg/models/`
- Repository pattern for data access
- Transaction support for multi-step operations
- Proper error handling and logging

Download now (ZIP)

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.