Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexisvisco/kcd
KCD lets you focus on what matters: coding.
https://github.com/alexisvisco/kcd
golang http-handler library
Last synced: 3 months ago
JSON representation
KCD lets you focus on what matters: coding.
- Host: GitHub
- URL: https://github.com/alexisvisco/kcd
- Owner: alexisvisco
- License: apache-2.0
- Created: 2020-07-17T20:16:31.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-09-15T10:04:47.000Z (about 2 years ago)
- Last Synced: 2024-06-28T06:36:46.652Z (4 months ago)
- Topics: golang, http-handler, library
- Language: Go
- Homepage: https://alexisvisco.gitbook.io/kcd
- Size: 1.23 MB
- Stars: 45
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
------
## :stars: KCD
KCD is a grandiose REST helper that wrap your shiny handler into a classic http handler. It manages all you want for
building REST services.This library is **opinionated** by default but **customizable** which mean it uses some other libraries like Chi,
Logrus... KCD is modular so each pieces of the code that rely on a specific library can be changed.## :muscle: Example
- [*Demo simple*](examples/demo/main.go)
- [*Demo standard http handler*](examples/demo-standard-http/main.go)
- [*Bind query parameters*](examples/input-from-query-parameter/main.go)
- [*Bind path parameters*](examples/input-from-path-parameter/main.go)
- [*Bind json body*](examples/input-from-json-body/main.go)
- [*Bind context value*](examples/input-from-ctx/main.go)
- [*Set default value*](examples/input-with-default-value/main.go)
- [*Split string with exploder tag*](examples/input-with-exploder/main.go)
- [*Nested struct*](examples/input-with-nested-struct/main.go)
- [*Validation*](examples/demo-validation)
- [*Errors*](examples/demo-errors)
- [*Real world example: pagination*](examples/demo-ordered-pagination)## :rocket: QuickStart
```go
package mainimport (
"fmt"
"net/http""github.com/go-chi/chi"
"github.com/go-chi/chi/middleware""github.com/alexisvisco/kcd"
)func main() {
r := chi.NewRouter()
r.Use(middleware.RequestID)// You can configure kcd with kcd.Config
r.Get("/{name}", kcd.Handler(YourHttpHandler, http.StatusOK))
// ^ Here the magic happen this is the only thing you need
// to do. Adding kcd.Handler(your handler)
_ = http.ListenAndServe(":3000", r)
}// CreateCustomerInput is an example of input for an http request.
type CreateCustomerInput struct {
Name string `path:"name"` // you can extract value from: 'path', 'query', 'header', 'ctx'
Emails []string `query:"emails" exploder:","` // exploder split value with the characters specified
Subject string `json:"body"` // it also works with json body
}// CreateCustomerOutput is the output type of the http request.
type CreateCustomerOutput struct {
Name string `json:"name"`
}// YourHttpHandler is your http handler but in a shiny version.
// You can add *http.ResponseWriter or http.Request in params if you want.
func YourHttpHandler(in *CreateCustomerInput) (CreateCustomerOutput, error) {
// do some stuff here
fmt.Printf("%+v", in)return CreateCustomerOutput{Name: in.Name}, nil
}
```## Install
```shell
go get github.com/alexisvisco/[email protected]
```## Compatibility with framework
- chi (by default)
- [gorilla/mux](https://github.com/alexisvisco/kcd-mux)
- [gin](https://github.com/alexisvisco/kcd-gin)
- [echo](https://github.com/alexisvisco/kcd-echo)## :coffee: Benefits
- 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[ 📖 Read more...](https://alexisvisco.gitbook.io/kcd)