refactor: handler signature an middlewares for an idiomatic solution #24

Merged
xdrm-brackets merged 6 commits from refactor/api-context into 0.4.0 2021-06-20 07:51:42 +00:00
1 changed files with 7 additions and 2 deletions
Showing only changes of commit 97941da901 - Show all commits

View File

@ -107,12 +107,17 @@ func main() {
log.Fatalf("invalid config: %s", err) log.Fatalf("invalid config: %s", err)
} }
// add http middlewares (logger)
builder.With(func(next http.Handler) http.Handler{ /* ... */ })
// add contextual middlewares (authentication)
builder.WithContext(func(next http.Handler) http.Handler{ /* ... */ })
// bind handlers // bind handlers
err = builder.Bind(http.MethodGet, "/user/{id}", getUserById) err = builder.Bind(http.MethodGet, "/user/{id}", getUserById)
if err != nil { if err != nil {
log.Fatalf("cannog bind GET /user/{id}: %s", err) log.Fatalf("cannog bind GET /user/{id}: %s", err)
} }
// ...
// build your services // build your services
handler, err := builder.Build() handler, err := builder.Build()
@ -261,7 +266,7 @@ type res struct{
Output2 bool Output2 bool
} }
func myHandler(ctx *api.Context, r req) (*res, api.Err) { func myHandler(ctx context.Context, r req) (*res, api.Err) {
err := doSomething() err := doSomething()
if err != nil { if err != nil {
return nil, api.ErrFailure return nil, api.ErrFailure