简体   繁体   English

Firebase Cloud Functions版本控制

[英]Firebase Cloud Functions versioning

I am working with Firebase Cloud Functions and I am trying to versioning my api. 我正在使用Firebase Cloud Functions,并且正在尝试对api进行版本控制。 I am using express as all the tutorials suggest . 我正在按照所有教程的建议使用Express。 However, with this solution, we use Firebase Hosting instead of Cloud Functions. 但是,通过此解决方案,我们使用Firebase Hosting代替Cloud Functions。

Hosting: https://xxxx.firebaseapp.com 托管: https //xxxx.firebaseapp.com

Cloud Functions: https://xxxx.cloudfunctions.net 云功能: https : //xxxx.cloudfunctions.net

The solution which comes closest to what I am looking for is this . 最接近我正在寻找的解决方案是这个

const app1 = express();
app1.use(cors({ origin: true }));
app1.get("*", (request, response) => {
  if(request.url.includes("/v1/")){
    response.send("V1: "+request.path);
  }
  else{
    response.send("V2: "+request.path);
  }

});

const stats = functions.https.onRequest(app1);

module.exports = {
  stats
};

However, you only can see only one function in Firebase Cloud Functions: 但是,您只能在Firebase Cloud Functions中看到一个功能:

https://xxxx.cloudfunctions.net/stats

Also, you only can handle one kind of HTTP request (GET, POST, etc). 另外,您只能处理一种HTTP请求(GET,POST等)。

What I am looking for is to have in Firebase Cloud Functions: 我正在寻找的是Firebase Cloud Functions中具有的功能:

https://xxxx.cloudfunctions.net/stats/v1/  (including GET, POST, PUT or in another case separate functions with “/:userId” “/save”, etc)
https://xxxx.cloudfunctions.net/stats/v2/  
https://xxxx.cloudfunctions.net/item/v1/

Is it possible to do it with Cloud Functions? 使用Cloud Functions可以做到吗?

It's expected that you'll see only one function with the name you've exported from your index.js. 预计您只会看到一个具有从index.js导出的名称的函数。 All URLs must be anchored through that one function. 所有URL必须通过该功能锚定。 It looks like you're expecting that both /stats/* and /items/* will route through your function, but that's not the way it works. 您似乎希望/ stats / *和/ items / *都可以路由您的函数,但这并不是它的工作方式。

If you want different path prefixes, you will either need different functions to handle them, or you will need to have a single common prefix for them all. 如果要使用不同的路径前缀,则可能需要使用不同的函数来处理它们,或者都需要为它们使用单​​个公共前缀。 For example, you could export a function called "api", then use it to handle all the different types of queries: 例如,您可以导出一个名为“ api”的函数,然后使用它来处理所有不同类型的查询:

/api/stats/...
/api/items/...

You may also want to consider using different functions (Express apps) for each different version of your api, and have Firebase Hosting route the different versions to the different app, instead of having a single app check the version at runtime. 您可能还需要考虑为API的每个不同版本使用不同的功能(Express应用程序),并让Firebase Hosting将不同版本路由到其他应用程序,而不是让单个应用程序在运行时检查版本。

Each path can point to a distinct function (or Express app). 每个路径可以指向不同的功能(或Express应用程序)。 You configure this in your firebase.json file like below. 您可以在firebase.json文件中进行配置,如下所示。 You don't need a separate domain at all. 您根本不需要单独的域。 The below firebase.json results in 以下firebase.json结果

https://example.com is handled by firebase hosting https://example.com/v1 is handled by firebase function https://example.com/v2 is handled by firebase function https://example.com/v3 is handled by firebase function

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "hosting": {
    "public": "public",
    "cleanUrls": true,
    "trailingSlash": false,
    "rewrites": [
      {
        "source": "/v1",
        "function": "v1"
      },
      {
        "source": "/v2",
        "function": "v2"
      },
      {
        "source": "/v3",
        "function": "v3"
      }
    ],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  },
  "storage": {
    "rules": "storage.rules"
  },
  "functions": {
    "source": "functions"
  }
}

Finally, I solved it in this way: 最后,我以这种方式解决了它:

const express = require("express")

const getUserFunctions = express();
getUserFunctions.get("/stats", (request, response) => {
  ...
  ...

});

const v1 = functions.https.onRequest(getUserFunctions);

module.exports = {
  v1,
  v2
};

So the endpoint would be like this: 因此,端点将是这样的:

https://xxxx.cloudfunctions.net/v1/stats

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM