Skip to main content
This topic has been closed for replies.
Correct answer BKBK
 

Variables de ambiente/entorno:

Hola, Alguien podría ayudarme, busco una forma de aplicar variables de ambiente PROD/DEV, y poder desde ahí tomar los valores como URL o credenciales de servicios Web o APIs , etc.

============================

Environment variables/environment:

Hi, Could someone help me, I'm looking for a way to apply PROD/DEV environment variables, and from there take values ??such as URLs or Web service credentials or APIs, etc.


By @Cristian23334364163h

 

Yes, your ColdFusion application can have access to and use environment variables. As you weigh this option, there are some ideas I wish to share with you. They are from personal experience.

 

Major advantages of environment variables:

1. The Operating System's environment variables comprise essentially a datastore. The variables are globally available to any application running on the Operating System.
PROD and DEV usually run on separate Operating Systems. So, you should have PROD environment variables on PROD's Operating System and DEV environment variables on DEV's Operating System.
Which is best-practice because it fosters "Separation of Concerns".

2.  Obtaining variables from the Operating System usually incurs less network latency than obtaining them from, say, a database such as SQL Server, Oracle or MySQL.

 

Major disadvantages of environment variables:

1. Environment variables are globally available to any application running on the Operating System. So other applications will have access to the variables that are specific to your ColdFusion application. That may be a security or privacy concern.

2. Environment variables are read-only, immutable. You cannot modify them at the level of the ColdFusion application.

 

Access and use:

Suppose you wish to access and use the following environment variables in your ColdFusion code:

 

on PROD:

DB_USERNAME_PROD
PAYMENT_WS_URL_PROD

 

on DEV

DB_USERNAME_DEV
PAYMENT_WS_URL_DEV

 

Then you could do it in one of two ways. Either way, you would store the value in application scope, because an environment varible is a global constant.

 

(1) using native CFML
Which is Charlie's suggestion (mentioned again because I want to add a qualification to it) 

 

<!--- on PROD --->
<cfset application.DB_USERNAME_PROD = server.system.environment["DB_USERNAME_PROD"]>
<cfset application.PAYMENT_WS_URL_PROD = server.system.environment["PAYMENT_WS_URL_PROD"]>

<!--- on DEV --->
<cfset application.DB_USERNAME_DEV = server.system.environment["DB_USERNAME_DEV"]>
<cfset application.PAYMENT_WS_URL_DEV = server.system.environment["PAYMENT_WS_URL_DEV"]>

 

The struct keys in this method are case-insensitive by default. So the following variations will also work:

<cfset application.DB_USERNAME_PROD = server.system.environment["DB_username_prod"]>
<cfset application.PAYMENT_WS_URL_PROD = server.system.environment["payment_WS_URL_prod"]>

 

(2) using Java

 

<!--- on PROD --->
<cfset systemObject = createObject("java", "java.lang.System")>
<cfset application.DB_USERNAME_PROD = systemObject.getenv()["DB_USERNAME_PROD"]>
<cfset application.PAYMENT_WS_URL_PROD = systemObject.getenv()["PAYMENT_WS_URL_PROD"]>

<!--- on DEV --->
<cfset systemObject = createObject("java", "java.lang.System")>
<cfset application.DB_USERNAME_DEV = systemObject.getenv()["DB_USERNAME_DEV"]>
<cfset application.PAYMENT_WS_URL_DEV = systemObject.getenv()["PAYMENT_WS_URL_DEV"]>

 

The keys in this method are case-sensitive by default. So the following variations will fail:

<cfset application.DB_USERNAME_PROD = server.system.environment["DB_username_prod"]>
<cfset application.PAYMENT_WS_URL_PROD = server.system.environment["payment_WS_URL_prod"]>

2 replies

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
February 27, 2022
 

Variables de ambiente/entorno:

Hola, Alguien podría ayudarme, busco una forma de aplicar variables de ambiente PROD/DEV, y poder desde ahí tomar los valores como URL o credenciales de servicios Web o APIs , etc.

============================

Environment variables/environment:

Hi, Could someone help me, I'm looking for a way to apply PROD/DEV environment variables, and from there take values ??such as URLs or Web service credentials or APIs, etc.


By @Cristian23334364163h

 

Yes, your ColdFusion application can have access to and use environment variables. As you weigh this option, there are some ideas I wish to share with you. They are from personal experience.

 

Major advantages of environment variables:

1. The Operating System's environment variables comprise essentially a datastore. The variables are globally available to any application running on the Operating System.
PROD and DEV usually run on separate Operating Systems. So, you should have PROD environment variables on PROD's Operating System and DEV environment variables on DEV's Operating System.
Which is best-practice because it fosters "Separation of Concerns".

2.  Obtaining variables from the Operating System usually incurs less network latency than obtaining them from, say, a database such as SQL Server, Oracle or MySQL.

 

Major disadvantages of environment variables:

1. Environment variables are globally available to any application running on the Operating System. So other applications will have access to the variables that are specific to your ColdFusion application. That may be a security or privacy concern.

2. Environment variables are read-only, immutable. You cannot modify them at the level of the ColdFusion application.

 

Access and use:

Suppose you wish to access and use the following environment variables in your ColdFusion code:

 

on PROD:

DB_USERNAME_PROD
PAYMENT_WS_URL_PROD

 

on DEV

DB_USERNAME_DEV
PAYMENT_WS_URL_DEV

 

Then you could do it in one of two ways. Either way, you would store the value in application scope, because an environment varible is a global constant.

 

(1) using native CFML
Which is Charlie's suggestion (mentioned again because I want to add a qualification to it) 

 

<!--- on PROD --->
<cfset application.DB_USERNAME_PROD = server.system.environment["DB_USERNAME_PROD"]>
<cfset application.PAYMENT_WS_URL_PROD = server.system.environment["PAYMENT_WS_URL_PROD"]>

<!--- on DEV --->
<cfset application.DB_USERNAME_DEV = server.system.environment["DB_USERNAME_DEV"]>
<cfset application.PAYMENT_WS_URL_DEV = server.system.environment["PAYMENT_WS_URL_DEV"]>

 

The struct keys in this method are case-insensitive by default. So the following variations will also work:

<cfset application.DB_USERNAME_PROD = server.system.environment["DB_username_prod"]>
<cfset application.PAYMENT_WS_URL_PROD = server.system.environment["payment_WS_URL_prod"]>

 

(2) using Java

 

<!--- on PROD --->
<cfset systemObject = createObject("java", "java.lang.System")>
<cfset application.DB_USERNAME_PROD = systemObject.getenv()["DB_USERNAME_PROD"]>
<cfset application.PAYMENT_WS_URL_PROD = systemObject.getenv()["PAYMENT_WS_URL_PROD"]>

<!--- on DEV --->
<cfset systemObject = createObject("java", "java.lang.System")>
<cfset application.DB_USERNAME_DEV = systemObject.getenv()["DB_USERNAME_DEV"]>
<cfset application.PAYMENT_WS_URL_DEV = systemObject.getenv()["PAYMENT_WS_URL_DEV"]>

 

The keys in this method are case-sensitive by default. So the following variations will fail:

<cfset application.DB_USERNAME_PROD = server.system.environment["DB_username_prod"]>
<cfset application.PAYMENT_WS_URL_PROD = server.system.environment["payment_WS_URL_prod"]>

Participant
February 28, 2022

Hola,
Entonces, Para evitar definir una variable global y que solo sea accesible dentro de un aplicacion en especifico, cual seria la forma correcta de hacerlo? A traves del Application.cfc o se puede acceder a un archivo (Ejemplo: .env) en donde se tengan declaradas dichas variables?

BKBK
Community Expert
Community Expert
February 28, 2022

Entonces, Para evitar definir una variable global y que solo sea accesible dentro de un aplicacion en especifico, cual seria la forma correcta de hacerlo? A traves del Application.cfc o se puede acceder a un archivo (Ejemplo: .env) en donde se tengan declaradas dichas variables?
=============================================
So, to avoid defining a global variable that is only accessible within a specific application, what would be the correct way to do it? Through the Application.cfc or can you access a file (Example: .env) where these variables are declared?

By @Cristian23334364163h

Like with most matters in programming, there is not one correct way to do it. The usual and commonest way is to store global, application-specific data in a relational database, such as MySQL, SQL Server, Oracle, and so on.

 

It is also usual and common to declare such variables in the application scope. That is what I did in the code example. You would usually initialize such variables in the onApplicationStart method in Application.cfc.

Charlie Arehart
Community Expert
Community Expert
February 25, 2022

Even translating that to english (via google) I'm not 100% sure I understand what you're asking. If you mean, perhaps, "how can I access environment variables from within CFML?", the answer is that you can do that easily since CF2018, as CF pulls them into the server scope as a struct:

server.system.environment

If you cfdump or writedump that, you should see what I mean.

 

And FWIW, I have a blog post with more on this:

https://www.carehart.org/blog/2020/2/28/cf2018_env_vars_in_server_scope

 

If you may feel you need to elaborate on your question, feel free. If instead, this is the info you needed, please do mark this comment as the "answer". 

/Charlie (troubleshooter, carehart. org)