Cursor rule
.cursor/rules/backend-golang.mdcGolang backend development patterns and conventions for POS System
Cursor rules
Quality
69/100
Scores the file, not the repository.Length
510 words
24 headings · 4 code blocksRepository
118
— · pushed 339 days agoLast changed
3 days ago
First indexed 3 days ago.123456# Backend Development Guidelines (Golang)78## Code Organization910### Package Structure11Follow the [backend/internal/](mdc:backend/internal/) package layout:12- `models/` - Data structures and DTOs13- `handlers/` - HTTP request handlers14- `middleware/` - HTTP middleware functions15- `database/` - Database connection and utilities16- `api/` - Route definitions and setup17- `utils/` - Shared utility functions1819### Handler Pattern20All handlers follow the pattern in [handlers/orders.go](mdc:backend/internal/handlers/orders.go):2122```go23type OrderHandler struct {24 db *sql.DB25}2627func NewOrderHandler(db *sql.DB) *OrderHandler {28 return &OrderHandler{db: db}29}3031func (h *OrderHandler) GetOrders(c *gin.Context) {32 // Implementation33}34```3536## Database Operations3738### Raw SQL Usage39- Use parameterized queries to prevent SQL injection40- Follow the patterns in [handlers/orders.go](mdc:backend/internal/handlers/orders.go) for database operations41- Always handle `sql.ErrNoRows` explicitly42- Use transactions for multi-table operations4344### Example Query Pattern:45```go46func (h *Handler) getRecord(id uuid.UUID) (*Model, error) {47 var record Model48 query := `SELECT id, field1, field2 FROM table WHERE id = $1`4950 err := h.db.QueryRow(query, id).Scan(&record.ID, &record.Field1, &record.Field2)51 if err == sql.ErrNoRows {52 return nil, fmt.Errorf("record not found")53 }54 if err != nil {55 return nil, fmt.Errorf("database error: %w", err)56 }5758 return &record, nil59}60```6162## Authentication & Security6364### JWT Middleware65Use the authentication middleware from [middleware/auth.go](mdc:backend/internal/middleware/auth.go):66- Protected routes must use `authMiddleware`67- Role-based access with `RequireRoles([]string{"admin", "manager"})`68- Extract user info with `GetUserFromContext(c)`6970### Error Handling71Follow the API response pattern from [models/models.go](mdc:backend/internal/models/models.go):7273```go74c.JSON(http.StatusBadRequest, models.APIResponse{75 Success: false,76 Message: "User-friendly error message",77 Error: stringPtr("error_code"),78})79```8081## API Endpoints8283### RESTful Design84Follow REST conventions as shown in [api/routes.go](mdc:backend/internal/api/routes.go):85- `GET /api/v1/orders` - List resources86- `POST /api/v1/orders` - Create resource87- `GET /api/v1/orders/:id` - Get single resource88- `PUT /api/v1/orders/:id` - Update entire resource89- `PATCH /api/v1/orders/:id/status` - Partial update90- `DELETE /api/v1/orders/:id` - Delete resource9192### Response Format93All API responses use the standard format from [models/models.go](mdc:backend/internal/models/models.go):9495```go96type APIResponse struct {97 Success bool `json:"success"`98 Message string `json:"message"`99 Data interface{} `json:"data,omitempty"`100 Error *string `json:"error,omitempty"`101}102```103104## Performance Best Practices105106### Database Connections107- Use connection pooling as configured in [database/connection.go](mdc:backend/internal/database/connection.go)108- Set appropriate connection limits and timeouts109- Always close rows and statements110111### Query Optimization112- Use indexes for frequently queried columns (see [database/init/01_schema.sql](mdc:database/init/01_schema.sql))113- Avoid N+1 queries by using JOINs or batch loading114- Implement pagination for large result sets115116## Error Handling117118### Database Errors119- Always wrap database errors with context120- Handle connection errors gracefully121- Use the `IsConnectionError` helper from [database/connection.go](mdc:backend/internal/database/connection.go)122123### HTTP Errors124- Return appropriate HTTP status codes125- Provide clear, actionable error messages126- Don't expose internal system details to clients127128## Testing Guidelines129130### Unit Tests131- Test handlers with mock database connections132- Test middleware functions independently133- Focus on business logic and edge cases134135### Integration Tests136- Test complete API endpoints137- Use test database with proper cleanup138- Test authentication and authorization flows139140## Logging141142### Structured Logging143- Use Gin's built-in logging middleware144- Log important business events (orders created, payments processed)145- Include request IDs for tracing146- Don't log sensitive information (passwords, tokens)
Also in madebyaris/poinf-of-sales
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| madebyaris/poinf-of-sales.cursor/rules/admin-interface-patterns.mdc · 118 | Cursor rules | stylearchsecurityapi+2 | 62/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/api-patterns.mdc · 118 | Cursor rules | lint-formatstylesecuritydatabase+3 | 62/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/authentication-and-security-patterns.mdc · 118 | Cursor rules | setupteststylesecurity+4 | 81/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/business-logic-patterns.mdc · 118 | Cursor rules | teststyledatabaseperformance+1 | 50/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/database-patterns.mdc · 118 | Cursor rules | stylearchtypessecurity+2 | 62/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/development-workflow.mdc · 118 | Cursor rules | setupbuildteststyle+3 | 86/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/docker-deployment.mdc · 118 | Cursor rules | setupbuildteststyle+8 | 77/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/frontend-react.mdc · 118 | Cursor rules | buildtestlint-formatstyle+4 | 69/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/makefile-scripting.mdc · 118 | Cursor rules | setuplint-formatstylearch+2 | 81/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/performance-optimization-patterns.mdc · 118 | Cursor rules | buildteststyledatabase+3 | 66/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/project-architecture.mdc · 118 | Cursor rules | setupteststylearch+6 | 78/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/react-native-mobile-patterns.mdc · 118 | Cursor rules | buildstylearchui+2 | 74/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/role-based-access-patterns.mdc · 118 | Cursor rules | styletypessecuritydatabase+2 | 58/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/tech-debt-prevention.mdc · 118 | Cursor rules | styletesting-strategyui | 50/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/testing-patterns.mdc · 118 | Cursor rules | setupteststylearch+4 | 74/100 | 3 days ago | |
| madebyaris/poinf-of-sales.cursor/rules/user-journey-optimization.mdc · 118 | Cursor rules | styleperformanceagent-behaviour | 50/100 | 3 days ago |
Diff against .cursor/rules/admin-interface-patterns.mdc Diff against .cursor/rules/api-patterns.mdc Diff against .cursor/rules/authentication-and-security-patterns.mdc Diff against .cursor/rules/business-logic-patterns.mdc Diff against .cursor/rules/database-patterns.mdc Diff against .cursor/rules/development-workflow.mdc Diff against .cursor/rules/docker-deployment.mdc Diff against .cursor/rules/frontend-react.mdc Diff against .cursor/rules/makefile-scripting.mdc Diff against .cursor/rules/performance-optimization-patterns.mdc Diff against .cursor/rules/project-architecture.mdc Diff against .cursor/rules/react-native-mobile-patterns.mdc Diff against .cursor/rules/role-based-access-patterns.mdc Diff against .cursor/rules/tech-debt-prevention.mdc Diff against .cursor/rules/testing-patterns.mdc Diff against .cursor/rules/user-journey-optimization.mdc
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| hiromaily/go-crypto-wallet.cursor/rules/typescript.mdc · 126 | Cursor rules | setupbuildtestlint-format+6 | 100/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/10-hermes-api.mdc · 45 | Cursor rules | testlint-formatstylearch+5 | 100/100 | 3 days ago | |
| markstev/mark-starter.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+6 | 99/100 | 3 days ago | |
| Allymahmoud/case-intake-platform.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| dodgecfr/combatfilms-webapp.cursor/rules/frontend.mdc · 0 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| deifos/clipmira-subtitles.cursor/rules/frontend.mdc · 1 | Cursor rules | setuptestlint-formatstyle+7 | 99/100 | 3 days ago | |
| langflow-ai/langflow.cursor/rules/docs_development.mdc · 153k | Cursor rules | setupbuildtestlint-format+7 | 97/100 | 3 days ago | |
| TechSquidTV/Hermes.cursor/rules/20-hermes-api-tests.mdc · 45 | Cursor rules | teststyletesting-strategysecurity+3 | 97/100 | 3 days ago |
