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

use of a web framework #25

Closed
bcollard opened this issue Apr 29, 2020 · 8 comments
Closed

use of a web framework #25

bcollard opened this issue Apr 29, 2020 · 8 comments
Assignees

Comments

@bcollard
Copy link

bcollard commented Apr 29, 2020

Hello,

Is it possible to use a web framework (for routing & middleware) like gorilla/mux or gofiber/fiber when using the functions-framework for go?
I can't figure out how to configure the main.go in such case, so maybe one just can't do that on local development machine?

(I've first given a try to functions-framework-nodejs and I could take advantage of Express.js both on local machine and on GCP Functions)

@bcollard
Copy link
Author

For now, I'm using this hack which creates a new http.ServeMux in the target function.

@tbpg
Copy link

tbpg commented Apr 30, 2020

Hi there. Can you give an example of what you would want the API to do?

Right now, when you register functions with RegisterHTTPFunction and RegisterEventFunction, they get wrapped with some helper logic and registered with http.DefaultServeMux.

So, if you wanted to handle the routing yourself, we could either expose the wrappers (basically middleware) or we could let you provide a custom mux. At first glance, I think it would be better to expose the wrappers.

@bcollard
Copy link
Author

For instance, I would like to create multiple sub-paths for a given "API" / function.
For some of these sub-paths, I would like to apply a unique CORS policy and another unique rate-limiting policy (backed by Memorystore). They are kind of 'middleware'.
BTW, I'm sure these kinds of policies would be better located in an API Manager, but I'm just exploring the capabilities of Google Functions.

Yes, exposing the wrappers sounds to be a nice option. But as I'm new to Go lang, feel free to decide on your own :)

@tbpg
Copy link

tbpg commented May 6, 2020

If you're using Cloud Functions directly, you won't need to use this Functions Framework.

The Functions Framework would be helpful in case you want to convert existing functions to run on another platform, like App Engine or Cloud Run.

That said, I think exporting the functionality to convert a handler to a "event" handler is a reasonable feature request. Then, you would be completely responsible for your own routing and starting a server.

@juliehockett @grant, thoughts?

@grant
Copy link
Contributor

grant commented May 6, 2020

I'm not sure of the specific issue. It's not clear from the GitHub issue. What's the desired behavior? Is this the question?:

I would like to create multiple sub-paths for a given "API" / function.

For Node, it's easy to use a custom URL router atop the Functions Framework. I show an example in this blogpost:
https://medium.com/google-cloud/express-routing-with-google-cloud-functions-36fb55885c68

I don't know if Go has such a package, but I would imagine using a lightweight router would answer this question.

@bcollard
Copy link
Author

bcollard commented May 6, 2020

Hello, yes that's definitely the point. I also built a NodeJS function with a set of endpoints routed thanks to the Express lib. But I'm looking for a similar way of working with Go.

My nodeJS version with express: https://github.com/bcollard/gcloud-functions-nodejs-claps
My Go version with a secondary http multiplexer: https://github.com/bcollard/gcloud-functions-go-claps
(both work)

@tbpg
Copy link

tbpg commented May 7, 2020

@grant, for Go, instead of picking a particular framework for routing, etc., the change here would be to export a version of this function that users can use to wrap an http.HandlerFunc to get this extra functionality:

func handleEventFunction(w http.ResponseWriter, r *http.Request, fn interface{}) {

@grant
Copy link
Contributor

grant commented Oct 7, 2020

Further think with this, URL routing and middleware is really not a feature of functions. With a serverless function, we really expect one route, and not a whole web framework / API.

If you're interested in that, we recommend Cloud Run, which has more flexibility and is recommended for building with a web framework and features like routing and middleware.

The Node framework may support Express middleware as an artifact, but that's not really a purposeful design.


Going to close this issue, but feel free to comment or suggest re-opening and I can re-open.

@grant grant closed this as completed Oct 7, 2020
@grant grant self-assigned this Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants