简体   繁体   English

将NodeJS应用程序变量绑定到Pivotal Cloud Foundry Service

[英]Bind NodeJS app variables to Pivotal Cloud Foundry Service

I am looking to bind a PCF (Pivotal Cloud Foundry) Service to allow us to set certain api endpoints used by our UI within PCF environment. 我希望绑定PCF(Pivotal Cloud Foundry)服务,以允许我们设置PCF环境中UI所使用的某些api端点。 I want to use the values in this service to overwrite the values in the root directory file, 'config.json'. 我想使用此服务中的值来覆盖根目录文件“ config.json”中的值。 Are there any examples out there that accomplish this sort of thing? 有没有可以完成这种事情的例子?

The primary way to tackle this is to have your application do this parsing. 解决此问题的主要方法是让您的应用程序执行此解析。 Most (all?) programming languages give you the ability to load environment variables and to parse JSON. 大多数(全部?)编程语言都使您能够加载环境变量和解析JSON。 Using these capabilities, what you'd want to do is to read the VCAP_SERVICES environment variable and parse the JSON. 使用这些功能,您要做的就是读取VCAP_SERVICES环境变量并解析JSON。 This is where the platform will insert the information from your bound services. 平台将在此处插入绑定服务中的信息。 From there you, you have the configuration information so you can configure your app using the values from your bound service. 从那里,您可以获得配置信息,因此可以使用绑定服务中的值配置应用程序。

Manual Ex: 手动执行:

var vcap_services = JSON.parse(process.env.VCAP_SERVICES)

or you can use a library. 或者您可以使用图书馆。 There's a handy Node.js library called cfenv . 有一个方便的Node.js库,名为cfenv You can read more about both of these options in the docs. 您可以在文档中详细了解这两个选项。

https://docs.cloudfoundry.org/buildpacks/node/node-service-bindings.html https://docs.cloudfoundry.org/buildpacks/node/node-service-bindings.html

If you cannot read the configuration inside of your application, perhaps there's a timing problem and you need the information before your app starts, you can use the platform's pre-runtime hooks. 如果您无法阅读应用程序内部的配置,则可能是时序问题,并且在应用程序启动之前需要信息,则可以使用平台的运行前挂钩。

https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#profile https://docs.cloudfoundry.org/devguide/deploy-apps/deploy-app.html#profile

The runtime hooks allow your application to include a file called .profile which will execute before your application. 运行时挂钩允许您的应用程序包含一个名为.profile的文件,该文件将在您的应用程序之前执行。 The .profile file is a simple bash script which can do anything needed to ready your application to be run. .profile文件是一个简单的bash脚本,可以执行准备运行应用程序所需的任何操作。 The only catch is that this needs to happen very quickly because it must complete before your application is able to start up and your application has a finite amount of time to start (usually 60s). 唯一需要注意的是,这需要很快发生,因为它必须在应用程序能够启动且应用程序具有有限的启动时间(通常为60秒)之前完成。

In your case, you could use jq to parse you values and insert them info your config file, perhaps using sed to overwrite a template value. 在您的情况下,您可以使用jq解析您的值,然后将其插入配置文件中,或者使用sed覆盖模板值。 Another option would be to run a small Node.js script, since your app is using Node.js it should be available on the path when this script runs, to read the environment variables and generate your config file. 另一个选择是运行一个小的Node.js脚本,因为您的应用程序正在使用Node.js,因此该脚本在运行时应该在路径上可用,以读取环境变量并生成您的配置文件。

Hope that helps! 希望有帮助!

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

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