Go 1.18 or higher is required as this library relies on parametric polymorphism.
This library provides a set of functions to quickly create http handlers without the need to write any boilerplate code, And have the safety and help of the compiler by making use of all the power of go's latest updates to the typing system.
This library is very small and lightweight and is not meant to be a framework.
It's only meant as a set of utilities that reduce the mental load for the developers by removing the need to create repetitive code.
It can be used with any framework or library that implement http.handler
interface.
Check example folder:
func MakeAccountHandler() http.Handler {
mux := http.NewServeMux()
mux.Handle("/account", GET(GetAccount))
// ...
return mux
}
// ...
func GetAccount(ctx context.Context, byID *AccountByID) (*Account, error) {
//TODO implement me
panic("implement me")
}
GET
, POST
, DELETE
, PUT
and PATCH
are functions that receive a function as an argument.
As you can see we never defined a function with arguments *AccountByID
and *Account
. Because these parameters are generic!
The provided function has the following Arguments:
It receives a generic handler function which is a function with the following args and return args
- Arg 1:
context.Context
Mandatory for all http handlers - Arg 2: Any go type, this will be used for decoding with the provided decoder. By default, json decoder is used.
- Return Arg 3: Any go type, this type will be encoded using the provided encoder. By default, json encoder is used.
- Return Arg 3: error, this is needed in case a error has to be returned
By Default GET
, POST
, DELETE
, PUT
and PATCH
are using a json encoder and decoder as well as a generic error handler.
However, all of these can be easily changed and defined based on your program needs.
As you can see in this GET
implementation it's just a shortcut for calling the underlying Handle function:
func GET[
Req any,
Req2 interface{ *Req },
Res any,
](handler Handler[Req, Req2, Res]) http.Handler {
return Handle(json.NewEncoder, json.NewDecoder, BasicJsonErrorHandler, handler, http.MethodGet)
}
By calling Handle
directly you can customize the handler to encode/decode messages or handle errors as you please!
In your program you will most likely want to define GET
, POST
, DELETE
, PUT
and PATCH
functions with your own error handler or even your specific encoder/decoder.