CF Mappings through command box

Hello everyone.

Is there a way to tell command box to automatically create CF mappings if they don’t exist already? I am aware of web server aliases setup in server.json file. But I can’t see if there’s an option to setup cf mappings.

I’m currently writing something to automatically create cf mapping with an init script through CF ADMIN API.

I am using Adobe ColdFusion 11 enterprise edition.

Thanks in advance!

You can by using the cfconfig CommandBox module. Here are the docs on it.

https://cfconfig.ortusbooks.com/using-the-cli/command-overview/manage-cf-mappings

Yes, assuming you’re referring to a server you started via CommandBox, CFConfig is the answer.

Thank you Jason and Brad!

I have 2 questions:

  1. Is there a way cfconfig be automatically be installed when box starts and cfconfig in turn imports the settings from a json file?

  2. Is there a global variable to get the current root (where server.json and box.son files are) and map accordingly?

For example, this:
/Users/fusionkanna/Documents/projects/bindi/faww
is specific to my own computer. Is there a way it can be something like:
[commandbox_web_root]/faww

This is how far I got so far:

I installed and exported current config. I created a box.json file with this:

{
“dependencies”:{
“commandbox-cfconfig”:“1.01”
}
}

And I have this exported from cfconfig export option (i deleted the other settings just to focus on mappings):

{
“CFMappings”:{
“/faww”:{
“PHYSICAL”:"/Users/fusionkanna/Documents/projects/bindi/faww"
},
“/daroot”:{
“PHYSICAL”:"/Users/fusionkanna/Documents/projects/bindi/DA"
}
}
}

Thank you again!
Raj

Right, so a couple things-- people don’t normally put system modules that install into CommandBox in their box.json of a specific project. Technically, when you install CFConfig, it does get stored in a box.json somewhere, but it’s an internal one to commandbox. That said-- if you want other people to be able to clone your repo, run “box install” and have everything they need, that will work. However, I’d recommend putting it as a dev dependency.

CFConfig will automatically load the file if it’s called exactly “.cfconfig.json” and it’s in your webroot. Or you can customize the name in your server.json file. Check the docs for all the specifics on this:
https://cfconfig.ortusbooks.com/using-the-cli/commandbox-server-interceptors/server-start

So the webroot question is a bit trickier. To be honest, I would recommend you not store these mappings at all in the server level and just use your Application.cfc’s this.mappings since that gives you a lot of control over dynamically setting up your mappings.
CF mappings in your cfconfig JSON are just used as-is and not made absolute or anything. You can use placeholders in your JSON such as ${myEnvVar} but CommandBox doesn’t currently set anything automatically when starting a server. I’ve had it in mind to do something like that for a while but never quite decided on what it should set and when. What you can do is add in the commandbox-dotenv module to your mix and introduce a .env file with a variable that you set to whatever your web root will be. So your .env might look like

mywebroot=/Users/fusionkanna/Documents/projects/bindi/

Then your config json would have something like
“PHYSICAL”:"${mywebroot}faww"

But that’s not really a super good solution because it still requires each dev to create and maintain the .env file. If you want to get fancier (and you’re on the latest version of CommandBox) you could add some magic sauce of your own. If you put something like this in your box.json

“scripts”:{
“preServerStart” : “env set myWebRoot=pwd
}

Then that would set a dynamic environment variable that only lives for the life of the start command called “mywebroot” equal to the output of pwd (print working directory). Then you could pick that up with ${mywebroot} in your cfconfig JSON file. Note, the above assumes you actually run the start command from the web root. Running “start myservername” from elsewhere would have an incorrect web root since the working directory of the shell would be elsewhere which you could work around by swapping “pwd” with something like

server info name=myServerName property=webroot

Lots of ways to skin the cat. Hopefully that gives you some idea, but seriously, i would avoid putting this in CFConfig specifically just because application.cfc makes it so easy. You can still use CFConfig for other stuff though :slight_smile:

This worked:

“scripts”:{
“preServerStart” : “env set myWebRoot=pwd
}

Thanks!