refactor: handler signature an middlewares for an idiomatic solution #24
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue