Does Firebase Hosting support rewrite rule to Python Cloud Function not deployed by Firebase?

我与影子孤独终老i 提交于 2019-12-24 23:36:55

问题


Firebase Hosting can be configured to direct requests to a Cloud Function:

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.


回答1:


I've verified that this use case is supported.

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.

Here is an example firebase.json file:

{
  "hosting": [
    {
      "target": "optional_target",
      "public": "public",
      "ignore": [
        "*"
      ],
      "rewrites": [
        {
          "source": "**",
          "function": "my_python_function"
        }
      ]
    }
  ]
}

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.

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

In this case, the subdirectory functions contains a main.py module with the my_python_function defined.

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.



来源:https://stackoverflow.com/questions/58068055/does-firebase-hosting-support-rewrite-rule-to-python-cloud-function-not-deployed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!