8000 Add example of developing multiple functions locally by di · Pull Request #99 · GoogleCloudPlatform/functions-framework-python · GitHub
[go: up one dir, main page]

Skip to content

Add example of developing multiple functions locally #99

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

Merged
merged 4 commits into from
Feb 17, 2021

Conversation

di
Copy link
Member
@di di commented Nov 25, 2020

Fixes #98.

@google-cla google-cla bot added the cla: yes label Nov 25, 2020
@di di requested a review from grant November 25, 2020 18:30
Copy link
Contributor
@grant grant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good skaffold example.

I'm going to approve but ask that we specifically don't suggest that this solves the multiple functions on a single port issue. This example is an opinionated way to achieve that but requires many dependencies that an existing Functions user is likely not going to want in their application.

@@ -0,0 +1,138 @@
# Multiple Cloud Functions, Same Host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The H1 here and example content are not exactly what I would imagine.

Similar to #98 (comment), I wouldn't expect that the minimum requirement to run multiple functions on the same host involves Kubernetes/minikube/skaffold. I'd expect a lighter weight setup, like how the FF is very lightweight – maybe require 1 dependency like Docker.


That said, can we change this example / PR title to something more aligned to the content, like "Developing Multiple Functions using Minikube and Skaffold"


## Introduction

This example shows you how to deploy multiple Cloud Functions to a single host.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example shows how to develop multiple functions on a Kubernetes cluster with a single host.

  • We're not using the Cloud Functions product anymore (FF's can be deployed to many environments).
  • This seems like an opinionated way for a multiple function, single host app. Let's label that, as we may want a simpler multiple function, single host app in a different example.

@martindufort
Copy link
martindufort commented Nov 26, 2020

Hi all,

Just want to provide some insights on our in-house solution to enable supporting multiple functions on the same host:port.
After investigation, we saw that the target function was always called regardless of the url path provided:

http://localhost:8080/go
http://localhost:8080/forward

Those two requests would both be executed by the target function. So we decided to take advantage of this and create a dispatcher function in our target file.

Now we are invoking the functions-framework like this:

functions-framework --target dispatcher --port 8081 --debug 

The dispatcher function within our main.py file is responsible for introspecting method names and calling the corresponding method if it exists.

def dispatcher(request):
    path = request.path
    
    path = path[1:] # Remove prefix / from path
    
    # Introspect all methods for potential execution
    potential_methods = globals().copy()
    potential_methods.update(locals())
    
    # Try to acquire corresponding method associated to the URL path
    method = potential_methods.get(path)
    if not method:
         raise NotImplementedError("URL Path %s not implemented" % path)
    return method(request)

@di di merged commit c247550 into master Feb 17, 2021
@di di deleted the multiple-function-example branch February 17, 2021 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to serve multiple routes on the same Url : Port
3 participants
0