简体   繁体   English

如何使用 Angular 以编程方式访问系统特定(Cloud Foundry)环境变量?

[英]How to access System Specific (Cloud Foundry) environment Variable Programmatically using Angular?

I have an angular app which is hosted on cloud.I'm Using Cloud foundry for deployment purpose.我有一个托管在云上的 angular 应用程序。我正在使用 Cloud Foundry 进行部署。 I need to access cloud foundry environment variables in my angular app at runtime without using any third party library like Angular Universal or any backend api.我需要在运行时访问我的 angular 应用程序中的 Cloud Foundry 环境变量,而不使用任何第三方库,如 Angular Universal 或任何后端 api。

I have tried several links ( how to make angular application read environment variables from private cloud foundry (Runtime) ) but neither it's working nor matching my parameters.我已经尝试了几个链接( 如何使 angular 应用程序从私有云代工厂(运行时)读取环境变量),但它既不工作也不匹配我的参数。 Need your Help.需要你的帮助。 Thanks in Advance.提前致谢。

The typical way to do this would be something like in the post you referenced, or to run a very basic REST API that you could query to fetch environment variables.执行此操作的典型方法类似于您引用的帖子,或者运行非常基本的 REST API ,您可以查询以获取环境变量。 This is because the environment variables live in the container where your app runs on Cloud Foundry & your Javascript code executes on a completely different computer.这是因为环境变量存在于您的应用在 Cloud Foundry 上运行的容器中,并且您的 Javascript 代码在完全不同的计算机上执行。 The Javascript just has no access to that code. Javascript 无法访问该代码。

That said, you could make a really hacky version of server-side rendering by using the .profile script.也就是说,您可以使用.profile脚本制作一个非常 hacky 版本的服务器端渲染。 The trick is that CF allows you to include a .profile script with your application.诀窍是 CF 允许您在应用程序中包含一个.profile脚本。 This script is executed once prior to your application starting and it can modify your application files.此脚本在您的应用程序启动之前执行一次,它可以修改您的应用程序文件。 Thus, the script just needs to inject the environment variables somewhere into your application code.因此,脚本只需要将环境变量注入应用程序代码的某个位置。

There's a number of ways you could do this:有多种方法可以做到这一点:

  1. There are some pure Bash solutions here and here . 这里这里有一些纯 Bash 解决方案。
  2. You can also write the .profile script in Python/Ruby/Perl as those are present in the CF Linux stack by default.您还可以在 Python/Ruby/Perl 中编写.profile脚本,因为它们默认存在于 CF Linux 堆栈中。 You just need to include the #!/usr/bin/env python or whatever you need at the top of the .profile file.您只需在.profile文件的顶部包含#!/usr/bin/env python或您需要的任何内容。
  3. You could include a static binary (Go, Rust, etc..) and have that process your files.您可以包含 static 二进制文件(Go、Rust 等)并让其处理您的文件。

Whatever solution you end up with, you should think about what information is present in the env variables and if that is leaking anything sensitive because if you inject the values into your Javascript, it will be visible to your users (ie view source/browser dev tools).无论您最终采用哪种解决方案,您都应该考虑环境变量中存在哪些信息,以及是否泄漏任何敏感信息,因为如果您将这些值注入 Javascript,您的用户可以看到它(即查看源代码/浏览器开发工具)。

IMHO, it would probably be best to not inject the full set of env variables & skip things like VCAP_SERVICES and VCAP_APPLICATION which will almost certainly have sensitive information.恕我直言,最好不要注入全套环境变量并跳过像VCAP_SERVICESVCAP_APPLICATION这样几乎肯定会有敏感信息的东西。

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

相关问题 如何使用节点js buildpack在云代工厂上部署angular 6 app? - How to deploy angular 6 app on cloud foundry using node js buildpack? 如何从私有云代工厂创建角度应用程序读取环境变量(运行时) - how to make angular application read environment variables from private cloud foundry (Runtime) 在构建 Angular 2 应用程序时访问系统变量(环境变量) - Access system variable (Environment Variable) while building Angular 2 application 如何使用路径如 http://myapp/mycontextpath 的路由在 Cloud Foundry 上部署 Angular 应用程序? - How to deploy Angular application on Cloud Foundry using route with path like http://myapp/mycontextpath? 在Cloud Foundry中部署Angular应用程序 - Deploy Angular Application in Cloud Foundry 如何在由gulp,bower组成的Cloud Foundry上运行Angular2项目? - How to run Angular2 Project on Cloud Foundry consisting of gulp,bower? 如何从Angular 4中的JSON文件访问环境变量? - How to access environment variable from JSON file in Angular 4? 如何使用Angular访问JSON中具体的object? - How to access specific object in JSON using Angular? 如何在angular 2中使用* ngfor访问特定元素 - How to access a specific element using *ngfor in angular 2 如何隐藏环境变量angular 2 - How to hide environment variable angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM