-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2308b67
commit 77d18a7
Showing
2 changed files
with
79 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/go-chi/chi/middleware" | ||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
"github.com/go-ozzo/ozzo-validation/v4/is" | ||
|
||
"github.com/expectedsh/kcd" | ||
) | ||
|
||
const idContext = "id" | ||
|
||
func main() { | ||
r := chi.NewRouter() | ||
r.Use(middleware.RequestID) | ||
|
||
// You can configure kcd with kcd.Config.{ ErrorHook, | ||
// You can configure kcd with kcd.Config. ErrorHook, | ||
// RenderHook, | ||
// BindHook, | ||
// ValidateHook, | ||
// LogHook, | ||
// StringsExtractors, | ||
// ValueExtractors } | ||
|
||
r.Use(func(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
ctx = context.WithValue(ctx, idContext, 12345) | ||
handler.ServeHTTP(w, r) | ||
}) | ||
}) | ||
// ValueExtractors | ||
|
||
r.Get("/{name}", kcd.Handler(SuperShinyHandler, http.StatusOK)) | ||
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:","` | ||
ContextualID *struct { | ||
ID int `ctx:"id" default:"12345"` | ||
} | ||
Name string `path:"name"` | ||
Emails []string `query:"emails" exploder:","` | ||
} | ||
|
||
// Validate is the function that will be called before calling your shiny handler. | ||
func (c CreateCustomerInput) Validate() error { | ||
return validation.ValidateStruct(&c, | ||
validation.Field(&c.Name, validation.Required, validation.Length(5, 20)), | ||
validation.Field(&c.Emails, validation.Each(is.Email)), | ||
validation.Field(&c.ContextualID, validation.Required), | ||
) | ||
// CustomerOutput is the output type of your handler it contain the input for simplicity. | ||
type CustomerOutput struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
// Customer is the output type of your handler it contain the input for simplicity. | ||
type Customer struct { | ||
CreateCustomerInput | ||
} | ||
|
||
// SuperShinyHandler is your http handler but in a shiny version. | ||
func SuperShinyHandler(in *CreateCustomerInput) (Customer, error) { | ||
// 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 | ||
|
||
return Customer{*in}, nil | ||
fmt.Printf("%+v", in) | ||
|
||
return CustomerOutput{Name: in.Name}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters