xiaohongshu
xiaohongshu
controller-handlers
Writing controller handler functions
Full skill instructions
Controller handler functions follow a standardized pattern with consistent return types and error handling.
All handler functions use this signature:
func handlerName(_ http.ResponseWriter, req *http.Request) (*ModelType, int, error)
Parameters:
_ - ResponseWriter (unused, handled by wrapper)req - HTTP request with context, params, and bodyReturns:
*ModelType/ResponseDataType - The response data (nil on error)int - HTTP status code (200, 400, 404, etc.)error - Error object (nil on success)func adminGet(_ http.ResponseWriter, req *http.Request) (*account.AccountJoined, int, error) {
// Get the URL parameter
id := chi.URLParam(req, "id")
// Fetch the model using the repository pattern
accountObj, err := account.GetJoined(req.Context(), types.UUID(id))
if err != nil {
log.ErrorContext(err, req.Context())
return helpers.AdminBadRequestError[*account.AccountJoined](err)
}
// Return success with the model data
return helpers.Success(accountObj)
}
return helpers.Success(data)
Returns: (data, http.StatusOK, nil)
For admin endpoints - returns full error details:
// Bad request with error message
return helpers.AdminBadRequestError[*ModelType](err)
// Returns: (zeroValue, http.StatusBadRequest, err)
// Not found
return helpers.AdminNotFoundError[*ModelType]()
// Returns: (zeroValue, http.StatusNotFound, standardError)
// Forbidden
return helpers.AdminForbiddenError[*ModelType]()
// Returns: (zeroValue, http.StatusForbidden, standardError)
For public endpoints - returns sanitized error messages:
// Bad request (generic public error)
return helpers.PublicBadRequestError[*ModelType]()
// Returns: (zeroValue, http.StatusBadRequest, publicError)
// Not found
return helpers.PublicNotFoundError[*ModelType]()
// Returns: (zeroValue, http.StatusNotFound, publicError)
// Forbidden
return helpers.PublicForbiddenError[*ModelType]()
// Returns: (zeroValue, http.StatusForbidden, publicError)
Important: Public errors never expose internal error details to users.
// Get URL parameter from route like /account/{id}
id := chi.URLParam(req, "id")
name := chi.URLParam(req, "name")
// Get the current user session (available in all authenticated endpoints)
userSession := helpers.GetReqSession(req) // returns a session object
userObj := helpers.GetLoadedUser(req) // returns the actual user object, not a session wrap
// For POST/PUT endpoints that are not the standard crud, use a struct for the input
input := &MyPostData{}
err := router.GetJSONPostDataStruct(req,input)
doSomething(input.SomeDataHere)
// Access query string parameters
queryParams := req.URL.Query()
page := queryParams.Get("page")
limit := queryParams.Get("limit")
// embeeded route params
id := chi.URLParam(req, "id")
Always log errors with the context before returning up. The controller is the log point.
if err != nil {
log.ErrorContext(err, req.Context())
return helpers.AdminBadRequestError[*ModelType](err)
}
This ensures errors are captured in logs with full request context.
Handlers are wrapped in setup.go:
// Admin endpoint - shows full errors
helpers.RoleHandler(helpers.RoleHandlerMap{
constants.ROLE_ADMIN: helpers.StandardRequestWrapper(adminCreate),
})
// Public endpoint - sanitizes errors
helpers.RoleHandler(helpers.RoleHandlerMap{
constants.ROLE_ANY_AUTHORIZED: helpers.StandardPublicRequestWrapper(authGet),
})
For admin endpoints:
helpers.StandardRequestWrapper(adminHandler)
Features:
For public/authenticated user endpoints:
helpers.StandardPublicRequestWrapper(authHandler)
Features:
public:"view" tagspublic:"edit" tagsUse appropriate wrappers: Admin handlers with StandardRequestWrapper, public handlers with StandardPublicRequestWrapper
Verify ownership: In auth handlers, always verify the user owns the resource:
if accountObj.ID_.Get() != session.AccountID {
return helpers.PublicForbiddenError[*account.Account]()
}
Principle of least privilege: Use the lowest role required for each endpoint
Don't mix admin and public logic: Keep admin and auth handlers separate
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience