Skip to content
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

Proposal: Custom route support and middleware plugins #782

Closed
andrew-s opened this issue Jul 21, 2018 · 3 comments
Closed

Proposal: Custom route support and middleware plugins #782

andrew-s opened this issue Jul 21, 2018 · 3 comments
Labels

Comments

@andrew-s
Copy link

There's a few elements to this so I'll try and break this down - the main use case here is to be able to build full REST based applications.

1. Routing

This may already exist but couldn't really find it, when defining your functions in yml - you would ideally want to specify a path and method exclusively for that function. There would need to be some validation somewhere so that 2 or more functions can't specify the same combination. An example of what this may look like;

functions:
  http-authentication-login:
    lang: php7.1
    handler: ./http/authentication/login
    image: project-http-authentication-login
    path:
        url: /login
        method: POST

If the user tries to GET /login then the gateway should return a 405 Method Not Allowed (unless the relevant method is allowed).

2. Parameters

Named parameters make searches or model lookups a lot more friendly (than say, using query strings) - it's easy to see exactly what's happening within the URL, for example;

GET /api/categories/my-category/posts

It's obvious that we're getting all the posts from my-category but, we'd ideally want that to be named going into the function. An example yml may look like;

functions:
  category-posts:
    lang: php7.1
    handler: ./http/posts
    image: project-http-posts
    path:
        url: /api/categories/:catname/posts
        method: GET

I'm not sure how the gateway would pass that into the function, perhaps query string it into the function? Then each function/language can use it's relevant input library to obtain that information (if that were a solution, it would have to merge with any query strings being passed through too).

I'm not sure how close or far away this is to this issue; #716

3. Middleware

Middleware definitions would allow for generic application code to be run on any other function - AWS's Gateway supports this to authentication only but, I think we could easily expand this in a generic way.

I'll give an example of what this may look like in the yml;

functions:
  category-posts:
    lang: php7.1
    handler: ./http/posts
    image: project-http-posts
   middleware:
       - authentication-middleware
       - access-middleware
    path:
        url: /api/categories/:catname/posts
        method: GET

That way, we'll always run the authentication-middleware and access-middleware functions whenever we call the category-posts, we should forward the headers to the middleware functions so that they can action however they need to, and if they return anything other than a 200 we should forward the response to the user and stop processing the chain.

Final Thoughts

All of this could be achieved in the application level, I've been looking at experimenting with nginx + a function that would proxy the other functions (then OpenFaaS would at least be scaling the application gateway) but, maybe it would be nice to actually implement this into OpenFaaS itself natively.

This would give enough flexibility to be a full REST platform and be more flexible than the alternatives (especially with multi-middleware capabilities). Unfortunately I don't have enough Go (if any) knowledge to be able to PR this but happy to advise.

@alexellis alexellis changed the title Allow functions to be REST driven Proposal: Custom route support and middleware plugins Jul 21, 2018
@ivanayov
Copy link
Contributor

Derek add label: proposal

@derek derek bot added the proposal label Aug 10, 2018
@alexellis
Copy link
Member

The first middleware is available for authentication:

https://github.com/openfaas/faas/tree/master/auth

@alexellis
Copy link
Member

/lock: inactivity

@derek derek bot locked and limited conversation to collaborators Feb 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants