CFWheels DotEnvSettings Plugin published

June 06, 2022

Posted in CLI, Contributions, Plugin

Posted By: Peter Amiri

<!-- wp:paragraph --> <p>A new plugin was published to provide support for .env settings files in a production environment. This plugin is based on Eric Peterson's CommandBox module and allows the use of .env or similarly named files to store your application secrets so they can be kept out of source control.</p> <!-- /wp:paragraph --> <!-- wp:heading --> <h2>LICENSE</h2> <!-- /wp:heading --> <!-- wp:paragraph --> <p>Apache License, Version 2.0.</p> <!-- /wp:paragraph --> <!-- wp:heading --> <h2>SYSTEM REQUIREMENTS</h2> <!-- /wp:heading --> <!-- wp:list --> <ul><li>Lucee 5+</li><li>ColdFusion 9+</li><li>CFWheels 2+</li></ul> <!-- /wp:list --> <!-- wp:heading {"level":1} --> <h1>Instructions</h1> <!-- /wp:heading --> <!-- wp:paragraph --> <p>Just drop the zip file into your <code>plugins</code> folder and restart your application or use CommandBox cli to install. Simply type the following at the root of your project:</p> <!-- /wp:paragraph --> <!-- wp:code --> <pre class="wp-block-code"><code>box install cfwheels-dotenvsettings</code></pre> <!-- /wp:code --> <!-- wp:heading --> <h2>Usage</h2> <!-- /wp:heading --> <!-- wp:paragraph --> <p>Create a .env file in your project root and add to .gitignore or your version control's equivalent (don't commit secrets to your repo!) The file can contain JSON or Java properties style key value pairs:</p> <!-- /wp:paragraph --> <!-- wp:code --> <pre class="wp-block-code"><code>// property style MY_SECRET_KEY=somevalue MY_OTHER_SECRET=shh // json style { "MY_SECRET_KEY"="somevalue" ,"MY_OTHER_SECRET"="shh" };</code></pre> <!-- /wp:code --> <!-- wp:paragraph --> <p>The default file name supported is .env but you can use any file name you want. You could even have multiple files for various environment.</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>In your CFWheels app you can read in your secrets by adding <code>readDotEnvSettings()</code> to your application. The most logical point to do this would be in your <code>events\onApplicationStart.cfm</code> file, but you could do it in your environment specific <code>settings.cfm</code> files as well. You can then access the secrets contained in the file processed using <code>get("MY_SECRET_KEY")</code>.</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>If you want to customize the secrets file to use, you can specify the file name by passing in the file name like so <code>readDotEnvSettings(".env-second")</code>.</p> <!-- /wp:paragraph -->

Latest Blog Posts