Responses
All route handlers use a pointer to a ResponseWriter (i.e. ResponseWriterPtr) as part of their output (plus an error) which require the following methods to be defined:
WriteBody(write func(body []byte) (int, error)) error
WriteHead(head *chimera.ResponseHead) error
OpenAPIResponsesSpec() chimera.Responses
All of the provided request types in chimera include sensible implementations for these methods so in general you only need to concern yourself with this if you are making your own ResponseWriter type. The internal usage however is defined by:
- The
OpenAPIRequestSpec()is run when the route is registered (viaGet,Post, etc.) so any pre-valdation should happen there - When handlers return a
ResponseWriter, the write does NOT happen immediately and can be intercepted by middleware - After all middleware is done
WriteHead()andWriteResponse()are used to write the response body/head to the underlyinghttp.ResponseWriter WriteHead()recieves aResponseHeadobject with the default status code already set and an emptyhttp.HeadermapWriteResponse()andWriteHead()should be kept very simple since errors returned by them can not be easily caught. In general the only issues that should occur here are errors involving serialization (i.e.json.Marshal) or writing (i.e.http.ResponseWriter.Write)- handlers can return an
error, which if non-nil will ignore the response value and instead return a generic500or a custom response if it is achimera.APIError.
Simple response types
chimera provides a few response types that implement ResponseWriter which are:
Responsewhich is just a set of predefined headers, body, response codeNoBodyResponse[Params any]which is a response that has no body but returns headersEmptyResponsewhich is a response that has no body or paramsLazybodyResponsewhich is a response with predefined headers/status code and a lazy body (written after middleware)