Skip to main content

Controller API

Set HTTP Response Code in Runtime

When a controller function returns with a nil error, the operation is considered successful.

If the function has no response payload, the status code will be 204. If it contains a payload, the status code will be 200, as per HTTP specifications.

If the function returns an error, the default error code will be 500.

However, it is possible to set a custom response code using the SetStatus API.

// @Method(GET)
// @Route(/my-route)
func (mc *MyController) MyRoute() (string, error) {
mc.SetStatus(runtime.StatusPartialContent)
return "", nil
}

Set HTTP Response Header in Runtime

To set HTTP response headers, use the SetHeader API.

// @Method(GET)
// @Route(/my-route)
func (mc *MyController) MyRoute() (string, error) {
mc.SetHeader("X-my-Header", "some string")
mc.SetHeader("X-my-Header-2", "some string 2")
return "", nil
}

Access HTTP Request Context

Sometimes, especially in edge cases, there is a need to access the full HTTP request context.

This might be necessary to perform extra dynamic operations on the request, support features not yet implemented and integrated into Gleece, or other specific needs.

Whatever the reason, you can access the router context via the GetContext API. The type is any, and you can cast it to your router's specific context type.

For example:

// @Method(GET)
// @Route(/my-route)
func (mc *MyController) MyRoute() (string, error) {
context := mc.GetContext()
// For Gin
ginContext := context.(*gin.Context)
// For Echo
echoContext := context.(echo.Context)
// For Fiber
fiberContext := context.(*fiber.Ctx)
// For Gorilla Mux & Chi
httpRequest := context.(*http.Request)

// Do the advanced logic....
return "", nil
}