Firebase托管是否支持将规则重写为Firebase未部署的Python Cloud Function? [英] Does Firebase Hosting support rewrite rule to Python Cloud Function not deployed by Firebase?

查看:86
本文介绍了Firebase托管是否支持将规则重写为Firebase未部署的Python Cloud Function?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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_functionmain.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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆