KCD is a grandiose REST helper that wrap your shiny handler into a classic http handler. It manage all you want for building REST services.
This library is opinionated by default but fully customizable which mean it uses some other libraries like Chi for instance. KCD is modular so each pieces of the code that rely on a specific library can be changed.
Okay so KCD will wrap your cool handler into a http handler. The magic happen with this function:
kcd.Handler(YourShinyHandler, http.StatusOK)
(which returns a http.HandlerFunc)
Your handler is the YourShinyHandler
parameter, it accepts:
func([response http.ResponseWriter], [request *http.Request], [input object ptr]) ([output object], error)
The only parameter in your shiny handler that is required is the returned error.
If there are any errors at some point KCD will call the error hook to provide a REST generic error.
- If there is a custom input parameter (a pointer to a structure) it will:
- Run all extractors to extract values from the request into the input (query parameters, path, header, default value ...)
- Run the JSON body bind hook
- Validate the input through the validate hook
- If all is good it will then call your shiny handler with all required arguments
- Then if there is an output parameter it will call the render hook
That's all. Well that's it, that's all you should have done if you didn't have KCD.
- More readable code
- Focus on what it matters: business code
- No more code duplication with unmarshalling, verifying, validating, marshalling ...
- You could have one interface for the client and server implementation