跳到正文
cccskills
免费在 GitHub 公开

handlers-and-http

Implement HTTP handlers that parse requests, invoke use cases, and format responses following REST conventions.

中文简介正在准备中。当前显示原文说明。

Authula/authula星标 273(GitHub 273 + cccskills 0)

实测评分 未实测

查看安装方法

包含的文件(2)

  • SKILL.md3.6 KB
  • examples/todo_handlers.go3.3 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Handlers & HTTP Integration

When to use this skill

  • Create HTTP endpoint handlers for authentication flows
  • Parse HTTP requests into domain types
  • Handle HTTP-specific concerns (status codes, headers, serialization)
  • Return properly formatted JSON responses
  • Map domain errors returned from services to HTTP status codes
  • Keep handlers thin by delegating to use cases

Key principles

  1. Thin handlers: Business logic lives in services, not handlers. Always remember to only extract and validate request data, invoke the appropriate service or use case, and format the response. Avoid embedding business logic directly in the handler to maintain separation of concerns and improve testability.
  2. Service coordination: Handlers invoke services which are interfaces
  3. HTTP boundaries: Handle only HTTP serialization and status codes, as well as request parsing and validation.
  4. Error mapping: Map domain errors returned from services to HTTP status codes via constants/errors.go.
  5. Context propagation: Pass request context to services

Pattern

Handlers are the HTTP boundary:

  • Define handler struct with service dependency. If a handler needs multiple unrelated services, create a use case struct instead. For example, a RegisterHandler needing both UserService and EmailService should delegate to a RegisterUseCase that imports both interfaces, keeping the handler thin and focused on HTTP concerns.
  • Implement Handle method with http.HandlerFunc type.
  • Parse and validate request. Always include a Validate() method on request structs to clean up input but do not trim sensitive fields like passwords, tokens, etc. Before trimming fields on the request struct, check that pointer fields are not nil to avoid dereferencing nil pointers. For example, if you have a request struct with a pointer field like Email *string, you should check if Email is not nil before calling strings.TrimSpace() on it within the Validate() method. This ensures that you don't encounter a runtime panic due to dereferencing a nil pointer.
  • Invoke use case/service
  • Map response to HTTP (status code, headers, JSON) using reqCtx.SetJSONResponse.
  • Handle errors consistently
  • Always make use of the code within the internal/ folder as there are utilities for request parsing, response formatting, and error handling. Avoid reinventing the wheel by utilising these utilities to ensure consistency across handlers.

Example

See examples/todo_handlers.go for:

  • CreateTodoHandler parsing POST requests
  • MarkTodoCompleteHandler patterns
  • Error handling and response formatting

Common mistakes

  1. Putting business logic in handlers
  2. Handlers calling multiple services directly (use use cases instead)
  3. Not propagating request context to use cases
  4. Forgetting error handling
  5. Incorrect HTTP status codes and not using reqCtx.SetJSONResponse for consistent response formatting or not using it to return the response at all, which can lead to inconsistent responses and missing status codes. Always use reqCtx.SetJSONResponse to ensure that responses are consistently formatted and include the appropriate status codes.
  6. Logging sensitive data (passwords, tokens)

References

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Wire dependencies using constructor-based dependency injection throughout services, repositories, and handlers.

中文简介正在准备中。当前显示原文说明。

Authula/authula2732026年10月24日 更新

Use this skill when answering questions, explaining concepts, writing technical responses, reviewing code, or helping with development tasks. Communicate in clear, simple, standard English. Keep answers concise and focused, remove unnecessary filler and repetition, explain technical ideas in plain language, and structure information so it is easy to read and understand. When showing code, present it clearly with enough context for the developer to understand what it does and where it belongs.

中文简介正在准备中。当前显示原文说明。

Authula/authula2732026年10月24日 更新

grill-me

免费

A relentless interview to sharpen a plan or design.

中文简介正在准备中。当前显示原文说明。

Authula/authula2732026年10月24日 更新

grilling

免费

Grill the user relentlessly about a plan, decision, or idea. Use when the user wants to stress-test their thinking, or uses any 'grill' trigger phrases.

中文简介正在准备中。当前显示原文说明。

Authula/authula2732026年10月24日 更新

handoff

免费

Compact the current conversation into a handoff document for another agent to pick up.

中文简介正在准备中。当前显示原文说明。

Authula/authula2732026年10月24日 更新

Build pluggable authentication features using the plugin system with initialization, migrations, routes, and service registration.

中文简介正在准备中。当前显示原文说明。

Authula/authula2732026年10月24日 更新

查看 Authula 的全部技能

报告此技能的问题