cfenv for Easier NodeJS on Cloud Foundry
Cloud Foundry Environment Variable Structure
In one of my apps, which has both a Cloudant (CouchDB) database, and a RabbitMQ queue, the configured environment looks something like this (use the cf env
command to see the environment values available to your app):
What the above is telling me is that inside a variable called VCAP_SERVICES, I’ll find the following information relating to my RabbitMQ service that this app uses. It’s entirely possible to parse the JSON and pull out the values the app needs, but it’s clunky especially with hyphenated key names! Instead, the cfenv library makes this super simple.
The code for this is on GitHub if you want to see it in context but essentially it boils down to:
Bring in the dependency:
require("vendor/autoload.php"); $queue = new Pheanstalk_Pheanstalk($config['beanstalkd']['host'] . ":" . $config['beanstalkd']['port']); $worker = new Worker($config); // Set which queues to bind to $queue->watch("mytube"); // pick a job and process it while($job = $queue->reserve()) { $received = json_decode($job->getData(), true); $action = $received['action']; if(isset($received['data'])) { $data = $received['data']; } else { $data = array(); } echo "Received a $action (" . current($data) . ") ..."; if(method_exists($worker, $action)) { $outcome = $worker->$action($data); // how did it go? if($outcome) { echo "done \n"; $queue->delete($job); } else { echo "failed \n"; $queue->bury($job); } } else { echo "action not found\n"; $queue->bury($job); } }
Next, parse the environment variable into something we can use:
class Worker { protected $config; protected $db; protected $client; public function __construct($config) { $this->config = $config; // connect to mysql $dsn = 'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['database']; $username = $config['db']['username']; $password = $config['db']['password']; $this->db = new \PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $this->client = new \Guzzle\Http\Client($config['api']['url']); } public function comment_added($data) { $comment_sql = "select * from comments where comment_id = :comment_id"; $comment_stmt = $this->db->prepare($comment_sql); $comment_stmt->execute(array("comment_id" => $data['comment_id'])); $comment = $comment_stmt->fetch(PDO::FETCH_ASSOC); if($comment) { // more SQL to update various counts } return true; }
Finally, go ahead and use those variables that are now in the appEnv
(you might like to make this a constant rather than a variable since updating it will do nobody any good):
rabbitmq_url = appEnv.getService('guestbook-messages').credentials.uri;
Hopefully this shows how the cfenv library makes it easy to work with the Cloud Foundry environment variables and will remind me how to solve this next time I run into problems parsing a complicated data structure! There are equivalents for other languages too, for example if you use PHP try installing cf-helper-php via composer.