我有以下 main.py firebase 函数,我正在尝试使用它进行部署
firebase deploy
但是当我卷曲 firebase 端点时,我收到 404 错误:
curl -v https://us-central1-<project-id>.cloudfunctions.net/test
这是我部署的功能:
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'
当我通过输入此命令在本地运行该函数时
flask --app main run
我能够到达此端点,但当我卷曲 firebase 端点时却无法到达。如有任何帮助,我们将不胜感激。
更新:当我进入 Google Cloud Console 时,没有显示任何功能。我想肯定是部署有问题。当我输入:
firebase -P staging deploy --only functions
我得到以下输出:
(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
我发现我缺少使用 python 的 Flask 库让 firebase 云功能工作的功能。我必须注册 Flask 应用程序才能使用 https_fn 包装器。这是现在在执行
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()
您可以在以下 Firebase 文档中找到更多文档:
https://firebase.google.com/docs/functions/http-events?gen=2nd
然后您可以通过访问
curl -v https://us-central1-<project-id>.cloudfunctions.net/functions/test
来访问测试端点