-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
51 lines (40 loc) · 1.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"net/http"
"github.com/alexisvisco/kcd/pkg/errors"
"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. ErrorHook,
// RenderHook,
// BindHook,
// ValidateHook,
// LogHook,
// StringsExtractors,
// ValueExtractors
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"`
Emails []string `query:"emails" exploder:","`
}
// CustomerOutput is the output type of your handler it contain the input for simplicity.
type CustomerOutput 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) (CustomerOutput, error) {
// do some stuff here
fmt.Printf("%+v", in)
return CustomerOutput{}, errors.NewWithKind(errors.KindInternal, "c'est fini !")
}