{"id":5047,"date":"2013-07-26T16:21:19","date_gmt":"2013-07-26T13:21:19","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=5047"},"modified":"2021-08-16T11:33:27","modified_gmt":"2021-08-16T08:33:27","slug":"globalize-your-configuration","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/globalize-your-configuration\/","title":{"rendered":"Globalize Your Configuration"},"content":{"rendered":"\n<p>Quite a lot of applications face the problem of storing configuration data, where configuration differs for development, test and production environments.<\/p>\n\n\n\n<p>Yaml files is a common way of organising application settings. They help make our configuration clean, as well as allow loading it somewhere to constant or global variable and then re-use in application.<\/p>\n\n\n\n<p>But it would be really cool to have some abstract interface for all our configuration files in an application and<br><a href=\"https:\/\/github.com\/railsware\/global\">global<\/a> gem resolves this issue by providing object oriented interface for our config files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installation<\/h2>\n\n\n\n<p>Extremely simple:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gem install global<\/pre>\n\n\n\n<p>or you can also insert the following line into Gemfile:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gem 'global'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Configuration<\/h2>\n\n\n\n<p>Before using the gem, we need to configure directory that is used as a source to load yaml files and our application environment:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Global.environment = \"ENV_HERE\"\nGlobal.config_directory = \"PATH_TO_DIRECTORY\"\n<\/pre>\n\n\n\n<p>For Rails, put initialisation into <code>config\/initializers\/global.rb<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Global.environment = Rails.env.to_s\nGlobal.config_directory = Rails.root.join('config\/global').to_s\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Usage<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">General<\/h3>\n\n\n\n<p>Let&#8217;s say our application has web and api parts divided by different hostnames. Also, each environment has its own configuration. We place all this stuff to <code>config\/global\/hosts.yml<\/code> file with the following structure:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">test:\n  web: localhost\n  api: api.localhost\n\ndevelopment:\n  web: localhost\n  api: api.localhost\n\nproduction:\n  web: myhost.com\n  api: api.myhost.com\n<\/pre>\n\n\n\n<p>Now, for the development environment we have the following data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt; Global.hosts\n=&gt; { \"api\" =&gt; \"api.localhost\", \"web\" =&gt; \"localhost\" }\n&gt; Global.hosts.api\n=&gt; \"api.localhost\"\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Namespacing<\/h3>\n\n\n\n<p>If there are different settings groups, we can split the files by directories in order to make configuration more organised. For example, let&#8217;s say web should have basic auth.<br>In this case we can have <code>config\/global\/web\/basic_auth.yml<\/code> with:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">test:\n  username: user\n  password: secret\n\ndevelopment:\n  username: user\n  password: secret\n\nproduction:\n  username: production_user\n  password: supersecret\n<\/pre>\n\n\n\n<p>Then in the development environment we will have:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt; Global.web.basic_auth\n=&gt; { \"username\" =&gt; \"development_user\", \"password\" =&gt; \"secret\" }\n&gt; Global.web.basic_auth.username\n=&gt; \"development_user\"\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Default section<\/h3>\n\n\n\n<p>In our previous examples, development and test sections were quite similar. To DRY our configs, use <code>default<\/code> section:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">default:\n  web: localhost\n  api: api.localhost\n\nproduction:\n  web: myhost.com\n  api: api.myhost.com\n<\/pre>\n\n\n\n<p>Data from the default section is used until it&#8217;s overridden in a specific environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ERB support<\/h3>\n\n\n\n<p>Global supports erb injection to make configuration dynamic. For this, place the following code into &#8216;global\/file_name.yml&#8217;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">test:\n  key: &lt;%=1+1%&gt;  \n\ndevelopment:\n  key: &lt;%=2+2%&gt;\n\nproduction: \n  key: &lt;%=3+3%&gt;\n<\/pre>\n\n\n\n<p>As a result, here&#8217;s what we have in the development environment:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt; Global.file_name.key\n=&gt; 4\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p>For more details, check out the source code and documentation on <a href=\"https:\/\/github.com\/railsware\/global\">github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quite a lot of applications face the problem of storing configuration data, where configuration differs for development, test and production environments. Yaml files is a common way of organising application settings. They help make our configuration clean, as well as allow loading it somewhere to constant or global variable and then re-use in application. But&#8230;<\/p>\n","protected":false},"author":44,"featured_media":5086,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Olexander Paladiy"],"class_list":["post-5047","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=5047"}],"version-history":[{"count":56,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5047\/revisions"}],"predecessor-version":[{"id":14083,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/5047\/revisions\/14083"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/5086"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=5047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=5047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=5047"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=5047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}