refactor/idiomatic-handlers-middlewares #25

Merged
xdrm-brackets merged 7 commits from refactor/idiomatic-handlers-middlewares into 0.4.0 2021-06-20 08:26:28 +00:00

Context

The api.Ctx type has been dropped in favor of context.Context, we just provide a helpers to fetch values from the context:

  • api.GetRequest(context.Context)
  • api.GetResponseWriter(context.Context)
  • api.GetAuth()

Note that the GetAuth() returns a pointer, allowing for direct manipulation by middlewares (c.f. WithContext()).

Handlers

Handler signature used to be unclear thus non-idiomatic.

The complete signature has an optional first input argument that is can be provided or omitted at will.

func(api.Ctx, in) (*out, api.Err)
// equivalent to
func(in) (*out, api.Err)

The new signature is uses a mandatory context.Context that is from the http.Request:

func(context.Context, in) (*out, apiErr)

Middlewares

The api.Adapater type has been removed in favor of the idiomatic func(http.Handler) http.Handler.

Auth middlewares have been replaced by contextual middlewares: WithAuth() becomes WithContext() there is no additional argument all is in the request's context.

  • With(): standard middlewares wrap the entire http connection these are for logging, recovering, etc.
  • WithContext(): contextual middlewares only wrap the service handler for the request and has a request's context filled with useful data : api.GetRequest(), api.GetResponseWriter(), api.GetAuth()
## Context The `api.Ctx` type has been dropped in favor of `context.Context`, we just provide a helpers to fetch values from the context: - `api.GetRequest(context.Context)` - `api.GetResponseWriter(context.Context)` - `api.GetAuth()` Note that the `GetAuth()` returns a pointer, allowing for direct manipulation by middlewares (c.f. `WithContext()`). ## Handlers Handler signature used to be unclear thus non-idiomatic. The complete signature has an optional first input argument that is can be provided or omitted at will. ```go func(api.Ctx, in) (*out, api.Err) // equivalent to func(in) (*out, api.Err) ``` The new signature is uses a mandatory `context.Context` that is from the http.Request: ```go func(context.Context, in) (*out, apiErr) ``` ## Middlewares The `api.Adapater` type has been removed in favor of the idiomatic `func(http.Handler) http.Handler`. Auth middlewares have been replaced by contextual middlewares: `WithAuth()` becomes `WithContext()` there is no additional argument all is in the request's context. - `With()`: standard middlewares wrap the entire http connection these are for logging, recovering, etc. - `WithContext()`: contextual middlewares only wrap the service handler for the request and has a request's context filled with useful data : `api.GetRequest()`, `api.GetResponseWriter()`, `api.GetAuth()`
xdrm-brackets added 7 commits 2021-06-20 08:26:13 +00:00
continuous-integration/drone/push Build is passing Details
53dfc8f679
feat: *api.Context is required as first handler argument
continuous-integration/drone/push Build is passing Details
af63c4514b
refactor: idiomatic remove of api.Context for context.Context, custom middlewares for standard http middlewares
- remove api.Context as using context.Context is more idiomatic
 - remove api.Adapter as it is redundant with func(http.Handler) http.Handler
 - remove authentication middlewares as they be achieved as normal middlewares but launched around the handler (after the service has been found and validated)
 - builder.With() adds an standard Middleware that runs before any aicra code
 - builder.WithContext() adds an http middleware that runs just before the service handler is called. The http.Request provided contains a context with useful values such as the required permissions (from the service configuration).
 - handlers take a context.Context variable as first argument instead of api.Context
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
97941da901
docs: update README for context.Context and middlewares
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
fd1ced5a8b
fix: restore request denied on invalid auth after contextual middlwares
xdrm-brackets merged commit cc25995659 into 0.4.0 2021-06-20 08:26:28 +00:00
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: go/aicra#25
No description provided.