Skip to content

Add global middleware support #32

@aosasona

Description

@aosasona

This is going to be a bit tricky because I ideally do not want middlewares to apply to all procedures, which means there needs to be a way to opt out or in. Currently, I have two options in mind:

Option 1

The simplest one probably, applying any middleware set with r.Use to any procedure set after it. An example of this:

r.Add(proc1).Add(proc2)

r.Use(someMiddleware)

r.Add(proc3).Add(proc4)

r.Build()

In the pseudo-code above, someMiddleware will only apply to proc3 and proc4 because of where it is positioned. The obvious problem with this is that it becomes less clear what is going on and now, the line position matters and makes it significantly easier to screw up

Option 2

Implementing named middleware with an opt-out mechanism available, For example:

r.Use("mid1", middleware1)

r.Add(q(...).ExcludeMiddleware("mid1")).
  Add(q(...)).
  Add(m(...).ExcludeMiddleware("mid2")).
  Add(q(...).ExcludeMiddleware("*"))

r.Use("mid2", middleware2)

r.Build()

In the example above, the order of your Use calls won't matter anymore, middleware will be applied to all procedures unless they explicitly opt out of it. In this case:

  • the first procedure has opted out of mid1
  • the second procedure will have both mid1 and mid2 applied
  • the third procedure will have only mid1 applied since it has opted out of mid2
  • the fourth procedure will have NONE of the middleware applied (* means all here)

I am inclined to go with the second option but for future references, I have documented both here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions