This sample demonstrates how to expose a Flask app as a single Cloud Function. The Flask app provides a simple CRUD interface for "widgets" stored in the Realtime Database.
See file functions/main.py for the code.
The sample creates a Flask app and defines two routes:
GET /widgetsandGET /widgets/<id>: These routes retrieve a list of all widgets or a single widget by its ID from the Realtime Database.POST /widgets: This route adds a new widget to the Realtime Database.
The entire Flask app is then exposed as a single Cloud Function called httpsflaskexample using @https_fn.on_request().
The httpsflaskexample function is triggered by an HTTP request.
To deploy and test the sample:
- Create a Firebase project on the Firebase Console.
- Get the code, for instance using
git clone https://github.com/firebase/functions-samples - Enter the correct directory
cd functions-samples/Python/http-flask - Set up the CLI to use your Firebase project using
firebase use --addand select your Firebase project. - Deploy your project's code using
firebase deploy. - Use a tool like
curlto test the function:- Add a widget:
curl -X POST -d "My new widget" https://us-central1-YOUR_PROJECT_ID.cloudfunctions.net/httpsflaskexample/widgets - Get all widgets:
curl https://us-central1-YOUR_PROJECT_ID.cloudfunctions.net/httpsflaskexample/widgets - Get a specific widget:
curl https://us-central1-YOUR_PROJECT_ID.cloudfunctions.net/httpsflaskexample/widgets/WIDGET_ID
- Add a widget: