go-handler

Guidelines for writing the HTTP handler layer in Go applications. A handler receives a web request, checks its input and authentication, passes the work to a service, and returns a response.

In plain words
What is it for?
Use it when adding or changing Go routes, request validation, authentication, service calls, JSON responses, or handler tests.
Why use it?
It keeps request-handling code small and consistent, while preventing business rules and direct database access from being mixed into it.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/golid-ai/golid/go-handler
Clone the repo
git clone --depth 1 https://github.com/golid-ai/golid

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 1,160 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.01160
Opus 5 $0.00000 $0.00580
Sonnet 5 $0.00000 $0.00232
Haiku 4.5 $0.00000 $0.00116

Measured 3d ago against content hash 09a97dd86628, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

go-handler scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/go-handler.mdc · 154 lines

How it starts

The opening of the file, as written. The whole thing — 154 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Go Handler Layer Patterns

Thesis: Handlers are thin translators between HTTP and services. They validate input, extract auth, delegate to services, and return JSON — nothing more.

Refrain: Extract → Validate → Delegate → Return

Reference files: auth.go, user.go, middleware/stack.go

Before modifying a module's handler (changes to validation, auth, or route registration), read docs/modules/{module}/spec.md for API surface and validation rules. For cross-module request chains, also check docs/flows.md.

Structure

type XHandler struct { xService xServicer }
func NewXHandler(xService *service.XService) *XHandler { ... }
  • Handlers are thin — validate input, delegate to service, return JSON.
  • No business logic in handlers. No direct DB access.
  • Struct field is the interface (xServicer), constructor takes the concrete type. This is the standard pattern — make new-module generates it automatically.

Service Interfaces

Handlers use unexported interfaces (authServicer, userServicer, sseHubber), not concrete service types. This enables mock-based testing. Constructor takes concrete types (callers unchanged), struct field is the interface. See handler/interfaces.go.

Scaffold Output

make new-module name=items generates:

  • handler/item.go — Handler with interface-typed service field
  • handler/item_test.go — Mock struct + 5 test stubs (Create success, Create validation, List, GetByID not found, Delete)
  • Interface entry appended to handler/interfaces.go
  • Fill in test bodies for your specific validation logic and error paths.

Auth Extraction

Every protected handler starts with:

userID, err := requireUserID(c)
if err != nil { return err }
userType, err := requireUserType(c)
if err != nil { return err }

Role guards (when needed) go after extraction:

if userType != "admin" {
    return apperror.Forbidden("Only admins can create items")
}

Resource-level authorization is handled by the service layer, not the handler.

Read the full file on GitHub · 154 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 154 lines · 0 tokens per session scan A 09a97dd86628

Subscribe to this mod's changes

go-handler is a cursor rule published in the GitHub repository golid-ai/golid (40 stars, last pushed 2mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,160 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.