-1

I have an angular app which is hosted on cloud .I'm Using Cloud foundry for deployment purpose. 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.

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. Need your Help. Thanks in Advance.

Aastick
  • 91
  • 1
  • 1
  • 7

1 Answers1

0

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. 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. The Javascript just has no access to that code.

That said, you could make a really hacky version of server-side rendering by using the .profile script. The trick is that CF allows you to include a .profile script with your application. 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.
  2. You can also write the .profile script in Python/Ruby/Perl as those are present in the CF Linux stack by default. You just need to include the #!/usr/bin/env python or whatever you need at the top of the .profile file.
  3. You could include a static binary (Go, Rust, etc..) and have that process your files.

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 (i.e. view source/browser dev tools).

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.

Daniel Mikusa
  • 13,716
  • 1
  • 22
  • 28
  • Do you have any jsfiddle or something which i can refer. Don't have much idea about **.profile** – Aastick May 20 '21 at 04:55
  • I don't. It's just a script though, so you can put whatever code you require in it. You just want some code that will take the environment variables you need, and inject them into your Javascript. That way when your users download the Javascript, it has access to the env variable values. – Daniel Mikusa May 20 '21 at 12:47