Skip to content

diamondburned/hrt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

23c7ab3 · Jun 19, 2024

History

21 Commits
Jun 19, 2024
Feb 28, 2023
Jun 19, 2024
Jun 19, 2024
Jun 19, 2024
Jun 19, 2024
Jun 19, 2024
May 7, 2023
Jun 19, 2024
Feb 28, 2023
Jun 19, 2024
Jun 19, 2024
Jun 19, 2024
Jun 19, 2024

Repository files navigation

Package hrt implements a type-safe HTTP router. It aids in creating a uniform API interface while making it easier to create API handlers.

HRT stands for (H)TTP (r)outer with (t)ypes.

Example

Below is a trimmed down version of the Get example in the GoDoc.

type EchoRequest struct {
	What string `query:"what"`
}

type EchoResponse struct {
	What string `json:"what"`
}

func handleEcho(ctx context.Context, req EchoRequest) (EchoResponse, error) {
	return EchoResponse{What: req.What}, nil
}
r := chi.NewRouter()
r.Use(hrt.Use(hrt.Opts{
    Encoder:     hrt.JSONEncoder,
    ErrorWriter: hrt.JSONErrorWriter("error"),
}))

r.Get("/echo", hrt.Wrap(handleEcho))

Documentation

For documentation and examples, see GoDoc.

Dependencies

HRT depends on chi v5 for URL parameters when routing. Apps that use HRT should also use chi for routing.

Note that it is still possible to make a custom URL parameter decoder that would replace chi's, but it is not recommended.

About

HTTP routing with types

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages