pythonfirebaseflaskgoogle-cloud-functions

Unable to get firebase app to deploy a simple request using python's Flask


I have the following main.py firebase function which I am trying to deploy using firebase deploy But when I curl the firebase endpoint I get a 404 error:

curl -v https://us-central1-<project-id>.cloudfunctions.net/test

Here is the function I deployed:

from firebase_admin import initialize_app, db
from firebase_functions import https_fn
import flask

initialize_app()
app = flask.Flask(__name__)

print("TEST APP")

@app.get('/test')
def hello_world():
    print('Hello World')
    return 'Hello, Firebase Cloud Functions with Python'

When I run the function locally by entering this command flask --app main run I am able to hit this endpoint, but not when I curl the firebase endpoint. Any help is appreciated.

Updated: When I go to the Google Cloud Console, no functions appear. I am thinking that there must be a problem with the deployment. When I type: firebase -P staging deploy --only functions I get the following output:

(node:25815) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

=== Deploying to '<progject-id>'...

i  deploying functions
i  functions: preparing codebase cancer-hacked for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  functions: required API cloudfunctions.googleapis.com is enabled
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled
i  functions: Loading and analyzing source code for codebase cancer-hacked to determine what to deploy
 * Serving Flask app 'serving'
 * Debug mode: off

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:8081
Press CTRL+C to quit

127.0.0.1 - - [15/Nov/2024 14:55:25] "GET /__/functions.yaml HTTP/1.1" 200 -

127.0.0.1 - - [15/Nov/2024 14:55:25] "GET /__/quitquitquit HTTP/1.1" 200 -

/bin/sh: line 1: 25820 Terminated: 15          python3.11 "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/firebase_functions/private/serving.py"

i  functions: cleaning up build files...

✔  Deploy complete!

Project Console: https://console.firebase.google.com/project/cancer-hacked/overview

Solution

  • I found what I was missing to get the firebase cloud function working using python's Flask library. I had to register the flask app to use https_fn wrapper. Here is the code which now works when doing a firebase deploy

    import firebase_admin
    from firebase_admin import credentials, firestore
    from flask import Flask, request
    from firebase_functions import https_fn
    from cancer import get_cancer
    
    # Initialize Firebase Admin SDK
    cred = credentials.Certificate("serviceAccountKey.json")
    firebase_admin.initialize_app(cred)
    db = firestore.client()
    
    app = Flask(__name__)
    
    @app.route('/test', methods=['GET'])
    def test():
        print("Hello World")
        return "Hello World"
    
    # Main entry point for Firebase Functions
    #firebase_function = functions.https.on_request(app)
    
    @https_fn.on_request()
    def functions(req: https_fn.Request) -> https_fn.Response:
        with app.request_context(req.environ):
            return app.full_dispatch_request()
    

    You can find more documentaiton in the following firebase docs:

    https://firebase.google.com/docs/functions/http-events?gen=2nd

    You can then access the test endpoint by going to curl -v https://us-central1-<project-id>.cloudfunctions.net/functions/test