-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support backend middleware #635
Comments
Another thought is maybe we have two intercepting methods, one for handling all incoming requests before API routes handle them, and one for handling all outgoing responses right before sending them back to the client. As one example, maybe users might want to add headers to all outgoing responses after a feature change.
So in a chain, targeting a route called
(In the chart above, responses created by I will try to think of an actual use-case for this before deciding. However, it could be worth figuring this out because it would probably affect the naming of the feature (e.g. |
Interesting idea... This reminds me of the way AWS API gateway and elastic load balancing works. In those services, you can insert an authorization handler between the proxy and your backend. Your backend can then query the request for that auth information. |
Oh, and Ktor already has the interceptor concept. Not sure if it would be useful in this context though. |
@bitspittle seems to do the job. This doesn't seem to handle several interceptors (interceptors are often modeled as an ordered chain). But I wouldn't see it as too big an issue, given that you can insert several steps within the declared interceptor, so it doesn't seem like there are cases that can't be handled. |
Thanks! Yeah, that's how NextJS works -- one middlelayer declaration but you can of course break the logic up however you want. |
So waking up this morning and having slept on it, here's an interesting wrinkle... A Kobweb project is allowed to be split up -- you can definitely define your project's API endpoints across separate modules in your codebase. Forcing a single global interceptor rubs a bit up against this philosophy. So I'm staring down a few choices that I can think of:
At the moment, I'm leaning to 1, but 3 would be pretty cool (where 3 is really just 1 + extra steps now that I am thinking through it). If there would be a way to make 2 work, I would probably love that for the flexibility. This is how For now, I can't see a way to do 2 fairly -- it is too easy to imagine a case where two interceptors want to create mutually exclusive responses. I think the biggest danger is I push for 1 now and then realize I could have done 2 in a smart way later. To break down approach 3 further, here are various cases a user can end up in:
Note that detecting which interceptors were already called in a codebase might be really hard to detect, actually, as I don't think KSP parses deep enough to give me that information. 3 may be dead in the water because of KSP limitations. |
In backend parlance, middleware is a method that intercepts all requests.
Note
The design for this concept is inspired by NextJS. See also: https://nextjs.org/docs/app/building-your-application/routing/middleware
(For a first pass at least, it should be an error to define more than one middleware method)
So imagine something like this:
MiddlewareContext
here is similar toApiContext
except it won't contain any response object.If the method returns a response, then no followup API route will be triggered.
The text was updated successfully, but these errors were encountered: