Firebase Web应用程序上的不同环境 [英] Different environments on Firebase web application

查看:129
本文介绍了Firebase Web应用程序上的不同环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase构建Web应用程序.目前,我可以说我确实有两个阶段-开发,运行本地主机的firebase serve和将Web应用程序上传到Firebase托管的firebase deploy --only hosting.

I am building a web application with Firebase. Currently I can say that I do have two stages - development, the firebase serve which runs the localhost and firebase deploy --only hosting which uploads the web application on Firebase hosting.

一切都很好,但是我不认为这是一种专业的解决方案.我看到的问题是,我的本地环境和实时Web应用程序共享同一个数据库.我对该主题进行了很多研究,并且我知道在Firebase上每个项目都不可能拥有两个数据库.提供的解决方案是在Firebase上创建两个项目,一个用于开发,另一个用于生产.或者,即使您愿意,也可以分期进行.

Everything is fine with that, but I do not see this as a professional solution. The problem that I see is that, my local environment and the live web application share the same database. I did quite some research on the topic and I understood that there is no way to have two databases per one project on Firebase. The solution that is offered out there, is to create two projects on Firebase, one for development and one for production. Or even if you want to, one for staging.

这个解决方案对我来说似乎完全可以.当然,这是个好主意.几个项目,几个环境,单独的数据库,非常完美.然后,在实施此解决方案之前,另一个问题浮现在我的脑海.如果我说,让我们创建一个暂存项目,以便将我用作暂存环境,然后决定部署我的Web应用程序,该暂存Web应用程序将公开可用,因此它也将被Google索引.

This solution seemed completely fine with me. It's a good idea for sure. Couple of projects, for couple of environments, separate databases, just perfect. Then just before implementing this solution another problem bumped in my head. If I say, let's create a staging project, in order to serve me as a staging environment, and I decide to deploy my web application, the staging web application will be publicly available, so it will also get indexed by Google and so on.

那么,在这种情况下,您能给我什么建议?如何确保我的暂存Web应用程序(在暂存Firebase应用程序上托管)将不可供其他用户使用,并且不会被搜索引擎索引.我曾考虑过将IP或VPC列入白名单,但是我不知道如何以免费可靠的方式进行操作.

So, what could you advice me in this situation? How can I make sure that my staging web application (hosted on the staging Firebase app) will not be available for others and will not be indexed by search engines. I thought about white-listing IPs or VPC, but I have no clue how to proceed in a way that is free and reliable.

推荐答案

以下解决方案适用于Firebase实时数据库".它不适用于"Firestore".在此处.

The following solution is for Firebase "Realtime Database". It does not apply to "Firestore". Read the difference here.

现在(2018年3月),Firebase Realtime Database允许您创建多个实例.

Now (2018 March), Firebase Realtime Database allows you to create multiple instance.

正式文件:通过多个数据库进行扩展

  1. 转到您的Firebase项目

  1. Go to your Firebase Project

在Firebase控制台中,转到Develop > Database部分中的数据"选项卡. 从数据库"部分(右上角)的菜单中选择Create new database.

In the Firebase console, go to the Data tab in the Develop > Database section. Select Create new database from the menu in the Databases section (upper right corner).

自定义您的数据库参考和安全规则,然后单击获取".

Customize your Database reference and Security rules, then click Got it.

(可选)修改新实例的Security ruleBackup option.

(Optional) Modify the Security rule and Backup option of the new instance.

2.用法

// Get the default database instance for an app
var database = firebase.database();

// Get a secondary database instance by URL
var database = firebase.database('https://testapp-1234.firebaseio.com');

3.用法示例:不同的环境

firebase-config.js

const BUILD_LEVEL = "dev";
// const BUILD_LEVEL = 'stage'
// const BUILD_LEVEL = 'prod'

let config = {
  apiKey: "your_apiKey",
  authDomain: "your_authDomain",
  projectId: "your_projectId",
  storageBucket: "your_storageBucket",
  messagingSenderId: "your_messagingSenderId"
};

if (BUILD_LEVEL === "dev") {
  config.databaseURL = "https://your-project-dev.firebaseio.com/";
} else if (BUILD_LEVEL === "stage") {
  config.databaseURL = "https://your-project-stage.firebaseio.com";
} else if (BUILD_LEVEL === "prod") {
  config.databaseURL = "https://your-project-dev.firebaseio.com";
}

firebase.initializeApp(config);

现在要更改Firebase数据库实例,只需更改BUILD_LEVEL变量.

Now to change the Firebase Database instance, you only need to change the BUILD_LEVEL variable.

将此功能与Git/Github/Gitlab workflowGit hookwebpackCI/CD tool组合在一起,您将获得一个非常灵活的解决方案.

Combine this feature with Git/Github/Gitlab workflow, Git hook, webpack, CI/CD tool, and you have a very flexible solution.

这篇关于Firebase Web应用程序上的不同环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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