Firebase托管是否支持将规则重写为Firebase未部署的Python Cloud Function? [英] Does Firebase Hosting support rewrite rule to Python Cloud Function not deployed by Firebase?
问题描述
Firebase托管可以配置为对云直接请求功能:
Firebase Hosting can be configured to direct requests to a Cloud Function:
对直接部署到Cloud Functions(即不使用firebase deploy
)的Python Cloud Function是否有效?这对于Python函数特别重要,因为Firebase只能部署Node.js函数.
Will that work for a Python Cloud Function deployed directly to Cloud Functions, i.e., not using firebase deploy
? That is particularly relevant for Python Functions, as Firebase can only deploy Node.js Functions.
推荐答案
我已验证了此用例的支持.
I've verified that this use case is supported.
您可以使用gcloud
cli部署python云函数,然后将URL部署到您的Firebase托管目标,并重写为该python函数.
You can deploy a python cloud function using the gcloud
cli, and then deploy a url to your firebase hosting target that rewrites to this python function.
这是一个firebase.json
文件示例:
{
"hosting": [
{
"target": "optional_target",
"public": "public",
"ignore": [
"*"
],
"rewrites": [
{
"source": "**",
"function": "my_python_function"
}
]
}
]
}
Firebase需要一个public
目录,即使在这种情况下它未使用.因此,您可以使用.gitkeep
文件创建一个简单的public
目录,以确保将其保存在源代码存储库中. firebase.json
文件中的ignore
属性可确保未将.gitkeep
文件上传到Firebase托管.
Firebase requires a public
directory even though it's unused in this case. So you can create a simple public
directory with a .gitkeep
file to make sure it's preserved in your source code repository. The ignore
attribute in the firebase.json
file makes sure that the .gitkeep
file is not uploaded to firebase hosting.
为方便起见,这是一个简单的bash脚本,可在一个命令中同时部署功能和托管配置:
For convenience, here is a simple bash script to deploy both the function and the hosting configuration in one command:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ORGINAL_PWD=$PWD
PROJECT_ID=$1
cd $DIR/../functions
gcloud functions deploy my_python_function \
--project "$PROJECT_ID" \
--runtime python37 \
--trigger-http
firebase use $PROJECT
firebase deploy
cd $ORGINAL_PWD
在这种情况下,子目录functions
包含定义了my_python_function
的main.py
模块.
In this case, the subdirectory functions
contains a main.py
module with the my_python_function
defined.
还要清楚,firebase use $PROJECT
的使用以及在firebase.json中定义target
,都需要为.firebaserc
文件配置项目/目标映射.
Also to be clear, the use of firebase use $PROJECT
, as well as defining a target
in firebase.json, requires that a .firebaserc
file be configured with the project/target mappings.
这篇关于Firebase托管是否支持将规则重写为Firebase未部署的Python Cloud Function?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!