Skip to content

Commit d501acf

Browse files
committed
FC-3005 allow DSN and configs to be set by environment variables
1 parent 91f09a9 commit d501acf

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Application.cfc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,66 @@
3434
<cfinclude template="#this.projectConstructorLocation#" />
3535

3636

37+
<!--- set up the farcry dsn from the environment --->
38+
<cfif structKeyExists(THIS, "bUseEnv") AND THIS.bUseEnv eq "true">
39+
<cfset system = createObject("java", "java.lang.System")>
40+
<cfset FARCRY_DSN = "" & system.getEnv("FARCRY_DSN")>
41+
42+
<cfif len(FARCRY_DSN)>
43+
44+
<!--- set the farcry dsn, dbtype and dbowner --->
45+
<cfset THIS.dsn = FARCRY_DSN />
46+
<cfset FARCRY_DBTYPE = system.getEnv("FARCRY_DBTYPE")>
47+
<cfif NOT isNull(FARCRY_DBTYPE)>
48+
<cfset THIS.dbType = FARCRY_DBTYPE>
49+
</cfif>
50+
<cfset FARCRY_DBOWNER = system.getEnv("FARCRY_DBOWNER")>
51+
<cfif NOT isNull(FARCRY_DBOWNER)>
52+
<cfset THIS.dbOwner = FARCRY_DBOWNER>
53+
</cfif>
54+
55+
<!--- set up the datasource settings --->
56+
<cfset this.datasources[FARCRY_DSN] = {
57+
"class" = system.getEnv("FARCRY_DSN_CLASS"),
58+
"connectionString" = system.getEnv("FARCRY_DSN_CONNECTIONSTRING"),
59+
"database" = system.getEnv("FARCRY_DSN_DATABASE"),
60+
"driver" = system.getEnv("FARCRY_DSN_DRIVER"),
61+
"host" = system.getEnv("FARCRY_DSN_HOST"),
62+
"port" = system.getEnv("FARCRY_DSN_PORT"),
63+
"type" = system.getEnv("FARCRY_DSN_TYPE"),
64+
"url" = system.getEnv("FARCRY_DSN_URL"),
65+
"username" = system.getEnv("FARCRY_DSN_USERNAME"),
66+
"password" = system.getEnv("FARCRY_DSN_PASSWORD")
67+
}>
68+
69+
<!--- set custom options when not using a connection string --->
70+
<cfset FARCRY_DSN_CUSTOM = system.getEnv("FARCRY_DSN_CUSTOM")>
71+
<cfif NOT isNull(FARCRY_DSN_CUSTOM) AND len(FARCRY_DSN_CUSTOM)>
72+
<cfif isJSON(FARCRY_DSN_CUSTOM)>
73+
<cfset this.datasources[FARCRY_DSN].custom = deserializeJSON(FARCRY_DSN_CUSTOM)>
74+
<cfelse>
75+
<cfset stCustom = {}>
76+
<cfloop list="#FARCRY_DSN_CUSTOM#" index="item" delimiters="&">
77+
<cfset stCustom[listFirst(item, "=")] = listRest(item, "=")>
78+
</cfloop>
79+
<cfset this.datasources[FARCRY_DSN].custom = stCustom>
80+
</cfif>
81+
</cfif>
82+
83+
<!--- set linked db hostname/port using the provided alias --->
84+
<cfset FARCRY_DB_LINK_ALIAS = system.getEnv("FARCRY_DB_LINK_ALIAS")>
85+
<cfif NOT isNull(FARCRY_DB_LINK_ALIAS) AND len(FARCRY_DB_LINK_ALIAS)>
86+
<cfset DB_PORT = system.getEnv(ucase(FARCRY_DB_LINK_ALIAS) & "_PORT")>
87+
<cfif NOT isNull(DB_PORT) AND len(DB_PORT)>
88+
<cfset this.datasources[FARCRY_DSN].host = listFirst(listLast(DB_PORT, "/"), ":")>
89+
<cfset this.datasources[FARCRY_DSN].port = listLast(listLast(DB_PORT, "/"), ":")>
90+
</cfif>
91+
</cfif>
92+
93+
</cfif>
94+
</cfif>
95+
96+
3797
<cfparam name="this.botDetection" default="true" />
3898
<cfif this.botDetection>
3999
<!--- Bot detection values --->
@@ -745,6 +805,9 @@
745805
<cfparam name="this.bUseMobileWebskins" default="false" /><!--- Mobile webskins are enabled when this flag is set to true --->
746806
<cfparam name="this.bUseTabletWebskins" default="false" /><!--- Tablet webskins are enabled when this flag is set to true --->
747807

808+
<!--- environment configuration --->
809+
<cfparam name="this.bUseEnv" default="false" />
810+
748811
<!--- Option to archive --->
749812
<cfparam name="this.bUseMediaArchive" default="false" />
750813

tags/farcry/_farcryApplicationInit.cfm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ test for the existance of each and act accordingly
143143
</cfloop>
144144
<cfset application.fc.lib.db.setLogChangeFlags(application.config.general.logDBChanges) />
145145

146+
<!--- set up the configs from the environment --->
147+
<cfif structKeyExists(THIS, "bUseEnv") AND THIS.bUseEnv eq "true">
148+
<cfset system = createobject("java", "java.lang.System")>
149+
<cfset ENV = system.getEnv()>
150+
<cfset ENV_ARRAY = ENV.keySet().toArray()>
151+
<cfloop from="1" to="#arrayLen(ENV_ARRAY)#" index="i">
152+
<cfif findNoCase("FARCRY_CONFIG_", ENV_ARRAY[i]) AND listLen(ENV_ARRAY[i], "_") gte 4>
153+
<cfset configKey = listGetAt(ENV_ARRAY[i], 3, "_")>
154+
<cfset configProperty = replaceNoCase(ENV_ARRAY[i], "FARCRY_CONFIG_#configKey#_" , "")>
155+
<cfset configValue = ENV.get(ENV_ARRAY[i])>
156+
<cfset application.fapi.setConfig(configKey, configProperty, configValue, true)>
157+
</cfif>
158+
</cfloop>
159+
</cfif>
160+
161+
146162
<!--- wrap this in a cftry catch in case the policystore isn't initialised yet --->
147163
<!--- <cfif StructKeyExists(request,"init") AND request.init eq 0> --->
148164

0 commit comments

Comments
 (0)