-
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.
feature(ctx): ctx as handler parameter
- Loading branch information
1 parent
1fa1fc3
commit dd54428
Showing
2 changed files
with
55 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/go-chi/chi/middleware" | ||
|
||
"github.com/expectedsh/kcd" | ||
) | ||
|
||
func main() { | ||
r := chi.NewRouter() | ||
r.Use(middleware.RequestID) | ||
|
||
r.Get("/{name}", kcd.Handler(YourHttpHandler, http.StatusOK)) | ||
_ = 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 here using the ctx of the request. | ||
func YourHttpHandler(ctx context.Context, _ *CreateCustomerInput) (CustomerOutput, error) { | ||
// get the id of the request from the context | ||
id := middleware.GetReqID(ctx) | ||
|
||
return CustomerOutput{Name: id}, 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