From 43c5c8a0e6488e93140fa3f86716495ca7c7c5ae Mon Sep 17 00:00:00 2001 From: Adam Wiggins Date: Sat, 4 Jun 2011 18:30:31 -0700 Subject: [PATCH] In Practice html and styling, example content in config --- content/config-in-practice.md | 32 ++++++++++++++++++++++++++++++++ content/config.md | 1 - public/style.css | 24 +++++++++++------------- views/factor.erb | 22 ++++++++++++++++++---- views/home.erb | 19 +++++-------------- web.rb | 3 ++- 6 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 content/config-in-practice.md diff --git a/content/config-in-practice.md b/content/config-in-practice.md new file mode 100644 index 0000000..f978dc0 --- /dev/null +++ b/content/config-in-practice.md @@ -0,0 +1,32 @@ +## Example: Canonical host config + +[`rack-canonical-host`](https://github.com/tylerhunt/rack-canonical-host) is of Rack middleware for Ruby web apps which redirects the user to the canonical hostname for the app. For example, if your production deploy can be reached at both hostnames `www.example.com` and `example.com`, but you wish for `www.example.com` to be the canonical host, using `rack-canonical-host` will automatically redirect users there when they access it on `example.com`. + +## Doing it wrong #1: Hostname as a constant in the code + + CANONICAL_HOST = 'www.example.com' + use(Rack::CanonicalHost, CANONICAL_HOST) + +This violates twelve-factor because it stores config in the codebase. + +## Doing it wrong #2: Hostname from conditional on "environment" + + use Rack::CanonicalHost do + case ENV['RACK_ENV'].to_sym + when :staging then 'example.com' + when :production then 'staging.example.com' + end + end + +This violates twelve-factor because it uses a conditional on the RACK_ENV value which stores an deploy name like `staging` or `production`. This stores config in the codebase (the hardcoded hostnames), and uses a non-granular, non-orthogonal deploy name to choose the canonical host. Both of these things make it impossible to add new deploys without changing the code. (As a thought experiment, imagine what it would take to open-source an app that had the above block of code in it.) + +## Doing it right: Hostname from an env var + + use(Rack::CanonicalHost, ENV['CANONICAL_HOST']) if ENV['CANONICAL_HOST'] + +## Doing it really right: Library uses env var directly + +A well-written library of the nature of `rack-canonical-host` will use the enviroment variable directly, simplifying the code even further: + + use Rack::CanonicalHost + diff --git a/content/config.md b/content/config.md index 195e7b2..24f5cf6 100644 --- a/content/config.md +++ b/content/config.md @@ -18,4 +18,3 @@ One method of managing config is batching it up into named groups (often called In a twelve-factor app, env vars are each orthogonal values, not grouped together as "environments," but independently controllable for each deploy. This is a model that scales up smoothly as the app naturally grows more deploys over its lifetime. A common pattern with env vars is to fall back on sensible defaults when not set. For example, an app may use the `CANONICAL_HOST` env var for redirects, but the app will not attempt a redirect the the value is not set (which is usually desirable for development deploys). Or assuming a local memcached if the MEMCACHED_URL is not set. In this way, no env vars means the app is running as a vanilla development deploy. - diff --git a/public/style.css b/public/style.css index deccb43..daaab53 100644 --- a/public/style.css +++ b/public/style.css @@ -21,7 +21,6 @@ header { font-size: 22pt; text-align: center; box-shadow: 0px 2px 12px #888; - margin-bottom: 12pt; } header a { text-decoration: none; @@ -36,7 +35,7 @@ section h1 { font-size: 22pt; } -article { +article, nav { text-align: justify; width: 60em; } @@ -49,35 +48,34 @@ article p a:hover, article li a:hover { color: #337; } -section#toc { +section.concrete { background: #ccc; margin-top: 24pt; padding-top: 32pt; padding-bottom: 32pt; } -section#toc h1 { +section.concrete h1 { font-size: 32pt; margin-bottom: 30pt; text-decoration: underline; } -section#toc h2 { +section.concrete h2 { margin-top: 12pt; } -section#toc h2 a { +section.concrete h2 a { text-decoration: none; color: #000; } -section#toc h2 a:hover { +section.concrete h2 a:hover { color: #337; } -section#toc h3 { +section.concrete h3 { color: #555; font-weight: normal; } section#factor { margin-top: 24pt; - margin-bottom: 128pt; } section#factor h2 { font-size: 32pt; @@ -90,16 +88,16 @@ section#factor h3 { margin-bottom: 16pt; } -#next, #prev { +nav { margin-top: 32pt; } -#next { +nav #next { float: right; } -#prev { +nav #prev { float: left; } -#next a, #prev a { +nav #next a, nav #prev a { font-size: 22pt; text-decoration: none; color: #555; diff --git a/views/factor.erb b/views/factor.erb index b03e07c..0c68e49 100644 --- a/views/factor.erb +++ b/views/factor.erb @@ -1,8 +1,22 @@ -
+
<%= render_markdown(@factor) %> - - -
+ +
+
+ <% + in_practice = render_markdown(@factor + '-in-practice') + if in_practice + %> +

In Practice

+ <%= in_practice %> + <% end %> +
+ + +
diff --git a/views/home.erb b/views/home.erb index 476402e..82682a7 100644 --- a/views/home.erb +++ b/views/home.erb @@ -1,19 +1,10 @@ -
-
- <%= render_markdown('abstract') %> -
+
+
<%= render_markdown('abstract') %>
+
<%= render_markdown('who') %>
-
-
- <%= render_markdown('who') %> -
-
- -
-
- <%= render_markdown('toc') %> -
+
+
<%= render_markdown('toc') %>