<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[eSteve's Blog]]></title>
  <link href="http://blog.earaya.com/atom.xml" rel="self"/>
  <link href="http://blog.earaya.com/"/>
  <updated>2014-01-24T21:37:15-07:00</updated>
  <id>http://blog.earaya.com/</id>
  <author>
    <name><![CDATA[Esteban Araya]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Compilers Should Be Slower]]></title>
    <link href="http://blog.earaya.com/blog/2014/01/24/compilers-should-be-slower/"/>
    <updated>2014-01-24T16:30:00-07:00</updated>
    <id>http://blog.earaya.com/blog/2014/01/24/compilers-should-be-slower</id>
    <content type="html"><![CDATA[<p>Sometimes I think compilers (and interpreters, I suppose) should be slower. This would force us to slow down and reason about the code we write.</p>

<p>In <a href="http://www.amazon.com/gp/product/0374533555/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0374533555&amp;linkCode=as2&amp;tag=earaya-20">Thinking, Fast and Slow</a>, Daniel Kahneman breaks down thinking into 2 modes. The slow mode is a deliberate and conscious mode; it&rsquo;s the kind of thinking you do when you&rsquo;re in control and are accutely aware of the choices you&rsquo;re making. The fast mode, is a &ldquo;reactive&rdquo; mode driven by our nature and our insticts; it&rsquo;s the kind of thinking you do when you need to quickly asses a situation and react to it immediately.</p>

<p>Slow thinking, then, is the kind of thinking you do when deciding what vehicle to purchase. Fast thinking, is the kind of thinking you do when you wake up at night and need to go to the bathroom.</p>

<p>And here, again, is the reason why I half-wish compilers were slower. Writing code is not the kind of task you can do in fast thinking mode. Even with years of prior experience writing software, you can&rsquo;t code thinking fast.</p>

<p>Writing software is a tough beast: it takes a lot of research, discovery, and prototyping. Clearly, writing code is slow thinking.</p>

<p>It&rsquo;s easy, however, to feel like systems you&rsquo;ve developed before are similar to the software you&rsquo;re currently writing. It&rsquo;s tempting to think you can just go with prior knowledge, make some assumptions, do things just like you&rsquo;ve done them before. It&rsquo;s easy to think you can use some heuristics and think fast.</p>

<p>But you can&rsquo;t. There are too many details and too many dependencies that you need track. There are too many unknowns and too many side effects that you need to consider. In other words, you&rsquo;ll never achieve quality software by thinking fast.</p>

<p>So, just take a minute and slow down. Think. Design. Prototype. Test. Then throw away all the code you&rsquo;ve written and start all over again. It&rsquo;s surprising how much even just a little design helps.</p>

<p>Even if all you do is grab some paper and draw a couple of diagrams; even if all you do is just talk to a few people about some of your ideas, you&rsquo;ll be better off.</p>

<p>We need to reason about the code we write; we need to think slowly and carefully about the changes we make.</p>

<p>And if compilers were slower, we might just be forced to think slow. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The Right Way To Do REST Updates]]></title>
    <link href="http://blog.earaya.com/blog/2013/05/30/the-right-way-to-do-rest-updates/"/>
    <updated>2013-05-30T22:56:00-06:00</updated>
    <id>http://blog.earaya.com/blog/2013/05/30/the-right-way-to-do-rest-updates</id>
    <content type="html"><![CDATA[<p>There&rsquo;s lots of good advice on the internet about how to design good REST APIs. The folks at <a href="http://apigee.com/about/api-best-practices">apigee</a> have tons of great articles and really know their stuff, so if you are looking for some general advice on REST, you should start there.</p>

<p>Most of the guidance you&rsquo;ll find on the internet regarding REST, however, focuses on GETs (reads) and POSTs (inserts). Today, therefore, I want to offer advice on how to do updates.</p>

<h2>Gimme The Skinny</h2>

<p>Let the consumers of your API update resources by appylying deltas instead of forcing them to replace the entire resource.</p>

<p>I know this isn&rsquo;t strictly related to REST APIs; it&rsquo;s more of a data API recommendation, but partial updates really do work better than full resource updates.</p>

<p>Why? Well, there are several reasons of course, but most of them revolve around:</p>

<ul>
<li><p>Partial updates ease update concurrency problems.</p></li>
<li><p>Partial updates let you more accurately express the changes you want to make and this simplifies your code.</p></li>
</ul>


<p>One final recommendation regarding REST: use the HTTP PATCH method instead of PUT to do partial updates. PATCH was specifically introduced in March 2010 to allow <a href="http://tools.ietf.org/html/rfc5789">partial resource modification</a>.</p>

<h2>Gimme More Detail</h2>

<p>OK, you&rsquo;ve decided to keep reading (thanks!). So here&rsquo;s the deal: Full resource updates (replacements) have the following problem:</p>

<p><img src="http://blog.earaya.com/images/posts/dotnet-concurrency-control.jpg" title="'A common problem with full updates'" ></p>

<p>Pretty ugly, huh?</p>

<p>There&rsquo;s a good chance the user wasn&rsquo;t even trying to update the &ldquo;conflicting&rdquo; fields. And if he was updating the conflicting fields, does the knowledge that the data changed matter to him? Why are we showing him an error? What is he supposed to do about it? Really, there&rsquo;s not a lot of good solutions here.</p>

<p>And then there&rsquo;s even more problems. As soon as your user can make changes you&rsquo;re going to get requirements like the following:</p>

<ol>
<li>When the customer address is updated, notify accounting.</li>
<li>Make sure only fields x, y and z are updatable on Foo.</li>
<li>Keep a history of changes on fields a, b, and c on Bar.</li>
</ol>


<p>How are you going to do that if all you get from you client is an entirely &ldquo;new&rdquo; resource? Are you going to diff it and derive what changed? That&rsquo;s hard and usually ends up being really messy.</p>

<p>Again, just have your API consumers be explicit about the fields they&rsquo;re changing and all your problems will go away, your life will be better, and your co-workers will be super impressed by your simple and intelligent code. :) Just kidding, you&rsquo;ll never impress another programmer, so don&rsquo;t even bother. :P</p>

<h2>Gimme Me Some Code</h2>

<p>You should check out the samples and the tests in <a href="https://github.com/earaya/voodoo">Voodoo</a>. <a href="https://github.com/brazilbrown">Scott Brown</a> and I recently added some cool code to support partial enitity with automatic validation in Jersey.</p>

<p>We decided to take a rather informal representation of the data by accepting a dictionary where only the fields to be updated are present. Our Jersey endpoint therefore looks something like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/person&quot;</span><span class="o">)</span>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">PersonResource</span> <span class="o">{</span>
</span><span class='line'>    <span class="c1">// Other endpoints omitted.</span>
</span><span class='line'>
</span><span class='line'>    <span class="nd">@PATCH</span>
</span><span class='line'>    <span class="nd">@Path</span><span class="o">(</span><span class="s">&quot;/{id}&quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="kd">public</span> <span class="n">Response</span> <span class="nf">updatePerson</span><span class="o">(</span><span class="nd">@PathParam</span><span class="o">(</span><span class="s">&quot;id&quot;</span><span class="o">)</span> <span class="n">String</span> <span class="n">id</span><span class="o">,</span> <span class="nd">@Editable</span><span class="o">(</span><span class="n">type</span> <span class="o">=</span> <span class="n">Person</span><span class="o">.</span><span class="na">class</span><span class="o">,</span>
</span><span class='line'>        <span class="n">fields</span> <span class="o">=</span> <span class="o">{</span><span class="s">&quot;name&quot;</span><span class="o">,</span> <span class="s">&quot;age&quot;</span><span class="o">})</span> <span class="n">Map</span> <span class="n">personUpdates</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="c1">// Note: In Voodoo @Editable makes sure only the editable</span>
</span><span class='line'>        <span class="c1">// fields defined here are being updated.</span>
</span><span class='line'>        <span class="c1">// Voodoo also validates the values passed according to the</span>
</span><span class='line'>        <span class="c1">// &#39;javax.validation` annotations on the Person class.</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// Build your query to update person here.</span>
</span><span class='line'>        <span class="c1">// You can also raise events, based on the properties being updated here.</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// It&#39;s a good idea to return the latest representation of the entity.</span>
</span><span class='line'>        <span class="n">Person</span> <span class="n">updatedPerson</span> <span class="o">=</span> <span class="n">personStore</span><span class="o">.</span><span class="na">getById</span><span class="o">(</span><span class="n">personUpdates</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">id</span><span class="o">));</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">Response</span><span class="o">.</span><span class="na">ok</span><span class="o">(</span><span class="n">updatePerson</span><span class="o">).</span><span class="na">build</span><span class="o">();</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see, the approach is rather straightforward. I think the <code>@Editable</code> annotation actually turned out pretty well: it abstracts away all the validation code from your endpoint.</p>

<h2>A few parting thoughts</h2>

<p>Of course not everything is perfect with this approach; there are a few minor drawbacks. Chief among these is the fact that there&rsquo;s a good chunk of tooling that doesn&rsquo;t quite support PATCH yet.</p>

<p>Backbone for example, has recently added support for PATCH (in IE) but hasn&rsquo;t put it into any of their releases yet. We&rsquo;ve had similar problems with <a href="https://github.com/wordnik/swagger-core">Swagger</a>; the method isn&rsquo;t really supported there either.</p>

<p>One other minor consideration is that if you don&rsquo;t really need any intelligence around what&rsquo;s changing, the code to do a full resource update is usually simpler.</p>

<p>So there you go: if you want to be awesome, start using PATCH and supporting partial resource updates. Otherwise, you can keep doing the same old thing you&rsquo;ve been doing. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Inside the .git Directory]]></title>
    <link href="http://blog.earaya.com/blog/2012/12/15/inside-the-git-directory/"/>
    <updated>2012-12-15T16:33:00-07:00</updated>
    <id>http://blog.earaya.com/blog/2012/12/15/inside-the-git-directory</id>
    <content type="html"><![CDATA[<p>Have you ever wondered how git works? Have you tried to figure out how git stores stuff?</p>

<p>Well, I have. This is what I discovered about the git object store.</p>

<h2>In the begining there was nothing.</h2>

<p>Not really. In fact, there&rsquo;s quite a bit of stuff in an empty repo. On your command line, create a new repo and list its contents.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># Create the repository</span>
</span><span class='line'><span class="nv">$ </span>mkdir mygitrepo <span class="o">&amp;&amp;</span> <span class="nb">cd </span>mygitrepo
</span><span class='line'><span class="nv">$ </span>git init
</span><span class='line'>Initialized empty Git repository in /Users/earaya/Projects/gitrepo/.git/
</span><span class='line'>
</span><span class='line'><span class="c"># Show all files</span>
</span><span class='line'><span class="nv">$ </span>find .
</span><span class='line'>.
</span><span class='line'>./.git
</span><span class='line'>./.git/config
</span><span class='line'>./.git/description
</span><span class='line'>./.git/HEAD
</span><span class='line'>./.git/hooks
</span><span class='line'>./.git/hooks/applypatch-msg.sample
</span><span class='line'>./.git/hooks/commit-msg.sample
</span><span class='line'>./.git/hooks/post-update.sample
</span><span class='line'>./.git/hooks/pre-applypatch.sample
</span><span class='line'>./.git/hooks/pre-commit.sample
</span><span class='line'>./.git/hooks/pre-rebase.sample
</span><span class='line'>./.git/hooks/prepare-commit-msg.sample
</span><span class='line'>./.git/hooks/update.sample
</span><span class='line'>./.git/info
</span><span class='line'>./.git/info/exclude
</span><span class='line'>./.git/objects
</span><span class='line'>./.git/objects/info
</span><span class='line'>./.git/objects/pack
</span><span class='line'>./.git/refs
</span><span class='line'>./.git/refs/heads
</span><span class='line'>./.git/refs/tags
</span></code></pre></td></tr></table></div></figure>


<p>As you can see, there&rsquo;s quite a bit of stuff there to begin with.</p>

<p>Something we learn right off the bat is that Git supports <a href="http://git-scm.com/book/en/Customizing-Git-Git-Hooks">hooks</a>. Take a look at some of the samples. At work, we use a pre-commit hook to run our linting before letting you commit. But I digress, I want to talk about the object store.</p>

<p>You&rsquo;ll see that initially, the <code>objects</code> directory is empty.</p>

<p>So, let&rsquo;s create a <a href="http://git-scm.com/book/en/Git-Internals-Git-Objects">git object</a>. In order for this to work, you have to type the commands as shown:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span><span class="nb">echo</span> <span class="s2">&quot;git rocks&quot;</span> &gt; test.txt
</span><span class='line'><span class="nv">$ </span>git add test.txt
</span></code></pre></td></tr></table></div></figure>


<p>As a result of adding the above object you should now see:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>find .git/objects
</span><span class='line'>.git/objects
</span><span class='line'>.git/objects/f6
</span><span class='line'>.git/objects/f6/8ce0a31a54e37649ee417d60e90911258f1043
</span><span class='line'>.git/objects/info
</span><span class='line'>.git/objects/pack
</span></code></pre></td></tr></table></div></figure>


<h2>And then there was a (SHA1) hash</h2>

<p>You might now be wondering how I was so confident you&rsquo;d get the same output I got on my machine.</p>

<p>The answer is simple: Git is, at its core, just a key-value store. Git doesn&rsquo;t care what you call your objects; Git only cares about the content of those objects.</p>

<p>To store an object, Git first peforms a few operations on the data. One of these operations is to calculate the SHA1 hash of the data and then store the data in the object store with a filename representing the hash.</p>

<p>At this point we have to make a brief but important aside. SHA1 has two properties that make it really useful for Git&rsquo;s use:</p>

<ol>
<li>It&rsquo;s extremely unlikely that 2 different objects will have the same hash value.</li>
<li>Identical objects will always the same hash representation.</li>
</ol>


<p>And so, <code>8ce0a31a54e37649ee417d60e90911258f1043</code> represents the SHA1 hash of &ldquo;git rocks&rdquo;. That&rsquo;s how I knew you&rsquo;d get exactly the same output I got.</p>

<h2>And finally, there was a tree.</h2>

<p>So now that our file is tucked away in the object store, we have to wonder: What happened to its filename? After all, Git wouldn&rsquo;t be that useful if it didn&rsquo;t preserve folder structure and if it didn&rsquo;t let us find our files by name.</p>

<p>Git tracks pathnames through a <a href="http://git-scm.com/book/en/Git-Internals-Git-Objects#Tree-Objects">tree</a>.</p>

<p>Go back to your command line and type:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>git write-tree
</span><span class='line'>81cbaf28bc31ce9218d51b685e35a08bfea99599
</span><span class='line'><span class="nv">$ </span>find .git/objects
</span><span class='line'>.git/objects
</span><span class='line'>.git/objects/81
</span><span class='line'>.git/objects/81/cbaf28bc31ce9218d51b685e35a08bfea99599
</span><span class='line'>.git/objects/f6
</span><span class='line'>.git/objects/f6/8ce0a31a54e37649ee417d60e90911258f1043
</span><span class='line'>.git/objects/info
</span><span class='line'>.git/objects/pack
</span></code></pre></td></tr></table></div></figure>


<p><code>git write-tree</code> (a low level command) saves the state of the index (your staged files) to the object store. Thus, we now see a new object in the store: <code>.git/objects/81/cbaf28bc31ce9218d51b685e35a08bfea99599</code>. This new object is our tree.</p>

<p>Once you peek into the tree object, it&rsquo;ll immeidately make sense. You&rsquo;ll be able to see the file contents by typing the following git command:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>git cat-file -p 81cbaf
</span><span class='line'>100644 blob f68ce0a31a54e37649ee417d60e90911258f1043    test.txt
</span></code></pre></td></tr></table></div></figure>


<p>You might already have guessed it, but the first section of the file, the <code>100644</code> represents the file permissions (in octal). <code>f68ce0</code> represents the filename of the blob in the store, and <code>test.txt</code> is the filename.</p>

<p>Directory hierarchies are represented in a similar manner, of course.</p>

<h2>Conclusion</h2>

<p>And there you have it folks. That&rsquo;s pretty much all there is to how Git stores objects. Pretty simple, huh?</p>

<p>I think it&rsquo;s amazing how Linus built such a powerful and useful system by elegantly using a simple hashmap. I wish my software was more like Git.</p>

<p>I hope you too gain an appreciation for using simple constructs in your own code.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Javascript AMD: Asynchrounous Module Definition]]></title>
    <link href="http://blog.earaya.com/blog/2012/12/01/javascript-amd-asynchrounous-module-definition/"/>
    <updated>2012-12-01T14:54:00-07:00</updated>
    <id>http://blog.earaya.com/blog/2012/12/01/javascript-amd-asynchrounous-module-definition</id>
    <content type="html"><![CDATA[<p>Before you read any further, I should warn you: I have limited experience with JS. However, please hear me out; I&rsquo;ve found that the concepts I&rsquo;m going to talk about, although imporant, are still not widely adopted by Javascript developers.</p>

<p>In the last few years we&rsquo;ve seen a shift to the &ldquo;cloud&rdquo;. With that, there has been a resurgence of the web. We&rsquo;re now doing things with HTML and HTTP that no one would have thought possible just 5 years ago. We now render data on the client, we push data from the server to the browser to have real time interactions like a native application would. Heck, we can even do 3D rendering on the browser&hellip; and if you have a modern browser, it works well!</p>

<p>In a few words, we&rsquo;re doing what Java Applets promised to do but never accomplished.</p>

<p>This re-birth of the web, however, has been bumpy. Developing for the browser is plagued with problems. You have to target multiple (old) browsers, on multiple OSes, with multiples displays; you have to deal with disparate hardware, internet connections, etc. I think you get the point: you really don&rsquo;t know where and how your app is going to run. And to make all of this worse, the tooling for writing browser applications is still maturing; there&rsquo;s not a lot of help out there.</p>

<p>Imagine, for example, if you had to write a Java server application and you didn&rsquo;t have a good compiler; or if you had to add a bunch of conditionals to detect what OS you&rsquo;re running on &ndash; it&rsquo;d be crazy, right?</p>

<p>But that&rsquo;s not even the worse of it; imagine Java the language provided no mechanism for modularizing your code: not JARs, no classes, no nothing. And of top of that everything was globally scoped. Scary, huh?</p>

<p>Well, to some extent that&rsquo;s the situation we find ourselves in when we write Javascript applications. Javascript has no supports for modules. And of top of that, everything in the browser is globally scoped. Now, I&rsquo;m aware that we developers have been playing games for years to diminish the global scoping problem, but we really haven&rsquo;t had a good solution. Unitl now, at least. Now we have AMD&hellip; and it changes everything.</p>

<p>AMD stands for asynchronous module definition. The goal of the AMD format is to provide a solution for modular Javascript that we can use right now (while we wait for <a href="http://wiki.ecmascript.org/doku.php?id=harmony:modules">harmony</a>).</p>

<p>The genius in AMD is that it proposes a format where both the module and its dependencies are asynchronously loaded. If you&rsquo;re working on the browser, the async nature of AMD provides advantages over other module systems (such as CommonJS) that really make it enjoyable to code in Javascript.</p>

<p>But enough talk, let&rsquo;s get to some code. I don&rsquo;t want to write a full tutorial on AMD, so the code samples will be brief. I&rsquo;m just hoping that when you&rsquo;re done reading this, you won&rsquo;t write a single line of code withouth using <a href="http://requirejs.org">RequireJS</a>.</p>

<p>Here&rsquo;s how you define a module:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">define</span><span class="p">([</span><span class="s1">&#39;jquery&#39;</span><span class="p">,</span> <span class="s1">&#39;backbone&#39;</span><span class="p">],</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">$</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">// At this point your dependencies are loaded (jquery and backbone).</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">// Let&#39;s do some setup. Anything here is private to the module.</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">privateVariable</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">// This what consumers of your module will get when they require your module.</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">View</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
</span><span class='line'>    <span class="nx">id</span><span class="o">:</span> <span class="nx">privateVariable</span><span class="p">,</span>
</span><span class='line'>    <span class="nx">initialize</span><span class="o">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span><span class='line'>      <span class="c1">// Some view init code.</span>
</span><span class='line'>    <span class="p">},</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// The rest of your public methods.</span>
</span><span class='line'>  <span class="p">});</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>I don&rsquo;t know about you, but the first time I saw an AMD module I immediately fell in love. Finally I saw an easy way to have private members. Finally there was a cleaner way to scope modules and their dependencies. I could go on and on, but I think the benefits I&rsquo;ve listed, should be enough to convince you.</p>

<p>Or at least, I hope this has at least made you aware of AMD and piqued your interest. Writing Javascript apps on the browser doesn&rsquo;t have to be a pain anymore. In fact, I rather enjoy.</p>

<p>I know, however, I haven&rsquo;t done this topic justice. Please go look at the RequireJS site and look at the samples and the documentation &ndash; espeically the the optmizer section. Go look at <a href="http://tagneto.blogspot.com/2011/04/on-inventing-js-module-formats-and.html">this post</a> by James Burke where he talks about the many reasons developers are now using AMD. And then when you&rsquo;re done reading that, come join the fun.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Scala Really Scales]]></title>
    <link href="http://blog.earaya.com/blog/2012/11/09/scala-really-scales/"/>
    <updated>2012-11-09T15:13:00-07:00</updated>
    <id>http://blog.earaya.com/blog/2012/11/09/scala-really-scales</id>
    <content type="html"><![CDATA[<p>About 3 months ago <a href="http://twitter.com/iammerrick">@iammerrick</a> started talking to me about Scala. After talking a little bit about it, he said: &ldquo;It&rsquo;s called Scala because the language scales with you&rdquo;.</p>

<p>My initial reaction was to dismiss Merrick&rsquo;s statement as &ldquo;just marketing fluff&rdquo;. However, after playing with the language a bit, I&rsquo;m convinced Scala is really the way forward on the JVM.</p>

<p>As a side note &ndash; if you can take the <a href="https://www.coursera.org/course/progfun">Functional Programming Principles In Scala</a> course by <a href="http://en.wikipedia.org/wiki/Martin_Odersky">Martin Odersky</a> on <a href="http://coursera.org">Coursera</a>, I&rsquo;d highly recommend it. I just finished the class and really enjoyed it. The course is a little on the tough side (specially the last 3 assignments), but it&rsquo;s a great introduction to Scala and to functional programming.</p>

<h2>Why Scala Matters</h2>

<p>The JVM is a superb platform. It runs everywhere and it&rsquo;s just solid.</p>

<p>Furthermore, the Java ecosystem is fantastic. Everything from the IDEs, to the build tools, to the Servlet containers, to the OSS projects that run on Java are first class, well documented and well supported by a fantastic community of excellent developers.</p>

<p>Java the language, however, has failed to keep with the times. The lack of anonymous functions and closures, the lack of type inference, the lack of object and array literals, and the lack of many other constructs make the language feel archaic. Unfortunately it&rsquo;s not just that the language is arcane, it&rsquo;s verbose and it gets in the way; it&rsquo;s not that all of this is annoying, it gets in the way of productivity. If often wish C# would run on the JVM.</p>

<p>In summary, the JVM is a great platform, but it just needs a better statically typed language.</p>

<h2>How Scala Truly Scales</h2>

<p>The thing about Scala is that it has a low barrier to entry. If you&rsquo;re not used to functional languages, you can write imperative code and get started anyhow. Classes are also first class citizens, so if you&rsquo;re coming from Java, you&rsquo;ll feel right at home.</p>

<p>If you&rsquo;ve written C# and used some of the newer features such as LINQ, anonymous functions, etc., the transition will be even easier.</p>

<p>And then, when you&rsquo;re starting to get comfortable, you can write truly idiomatic Scala. It&rsquo;s simple, uncluttered, and succinct. Here&rsquo;s a short sample:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'>  <span class="cm">/**</span>
</span><span class='line'><span class="cm">   * This function decodes the bit sequence `bits` using the code tree `tree` and returns</span>
</span><span class='line'><span class="cm">   * the resulting list of characters.</span>
</span><span class='line'><span class="cm">   */</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="n">decode</span><span class="o">(</span><span class="n">tree</span><span class="k">:</span> <span class="kt">CodeTree</span><span class="o">,</span> <span class="n">bits</span><span class="k">:</span> <span class="kt">List</span><span class="o">[</span><span class="kt">Bit</span><span class="o">])</span><span class="k">:</span> <span class="kt">List</span><span class="o">[</span><span class="kt">Char</span><span class="o">]</span> <span class="k">=</span> <span class="o">{</span>
</span><span class='line'>    <span class="k">def</span> <span class="n">decode0</span><span class="o">(</span><span class="n">tree0</span><span class="k">:</span> <span class="kt">CodeTree</span><span class="o">,</span> <span class="n">bits</span><span class="k">:</span> <span class="kt">List</span><span class="o">[</span><span class="kt">Bit</span><span class="o">],</span> <span class="n">acc</span><span class="k">:</span> <span class="kt">List</span><span class="o">[</span><span class="kt">Char</span><span class="o">])</span><span class="k">:</span> <span class="kt">List</span><span class="o">[</span><span class="kt">Char</span><span class="o">]</span> <span class="k">=</span> <span class="o">{</span>
</span><span class='line'>      <span class="n">tree0</span> <span class="k">match</span> <span class="o">{</span>
</span><span class='line'>        <span class="k">case</span> <span class="nc">Leaf</span><span class="o">(</span><span class="n">c</span><span class="o">,</span> <span class="n">w</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="n">decode0</span><span class="o">(</span><span class="n">tree</span><span class="o">,</span> <span class="n">bits</span><span class="o">,</span> <span class="n">acc</span> <span class="o">:+</span> <span class="n">c</span><span class="o">)</span>
</span><span class='line'>        <span class="k">case</span> <span class="nc">Fork</span><span class="o">(</span><span class="n">l</span><span class="o">,</span> <span class="n">r</span><span class="o">,</span> <span class="n">cs</span><span class="o">,</span> <span class="n">w</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="o">{</span>
</span><span class='line'>          <span class="k">if</span> <span class="o">(</span><span class="n">bits</span><span class="o">.</span><span class="n">isEmpty</span><span class="o">)</span> <span class="n">acc</span>
</span><span class='line'>          <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">bits</span><span class="o">.</span><span class="n">head</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="n">decode0</span><span class="o">(</span><span class="n">l</span><span class="o">,</span> <span class="n">bits</span><span class="o">.</span><span class="n">tail</span><span class="o">,</span> <span class="n">acc</span><span class="o">)</span>
</span><span class='line'>          <span class="k">else</span> <span class="n">decode0</span><span class="o">(</span><span class="n">r</span><span class="o">,</span> <span class="n">bits</span><span class="o">.</span><span class="n">tail</span><span class="o">,</span> <span class="n">acc</span><span class="o">)</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>      <span class="o">}</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>    <span class="n">decode0</span><span class="o">(</span><span class="n">tree</span><span class="o">,</span> <span class="n">bits</span><span class="o">,</span> <span class="nc">List</span><span class="o">())</span>
</span><span class='line'>  <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>(Note, I&rsquo;m not claiming to write idiomatic Scala. In fact, as mentioned before, I&rsquo;ve just barely started using the language. This snippet, however, demonstrates some good features).</p>

<p>If you&rsquo;re like me, it may take you a bit to get used to they syntax. In fact, I thought I&rsquo;d never get used to the implicit returns and the optional semicolons &ndash; but now I wish I&rsquo;d never have to type those extra characters ever again.</p>

<p><a href="http://www.scala-lang.org/node/107">Pattern matching and case classes</a> are a fantastic features. I wish more languages (I&rsquo;m looking at you C#) would offer such capabilities. In the snippet above you can also see functions are first class citizens, and how to type variables.</p>

<p>Things you can&rsquo;t see in the sample, but that are just as important:</p>

<ol>
<li><p>The <code>List</code> class you see above, is immutable. This means most operations on it actually return a new list instead of mutating it.</p></li>
<li><p>Types are optional; if the compiler can figure out the type, you can omit it.</p></li>
</ol>


<p>It&rsquo;s hard to explain, but I felt like Scala was really forgiving and let me grow into the language. For example, as I learned, I realized more and more that I really didn&rsquo;t need mutable types. This of course, has a nice side effect of making parallelization of your code much easier.</p>

<p>In summary Scala scales with you. As you learn the language, you can express more interesting thoughts in simpler ways. Furthermore, the language pushes to clean, elegant solutions.</p>

<h2>Final Thoughts</h2>

<p>If Scala required it&rsquo;s own runtime, I&rsquo;d have to say it&rsquo;d be just another functional language. But because Scala runs on the JVM, and because it can use all the Java code you currently depend on, Scala really does have a bright future.</p>

<p>I hope in 5 or so years we&rsquo;re all writing Scala instead of Java.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[HMAC User Authentication]]></title>
    <link href="http://blog.earaya.com/blog/2012/08/14/hmac-user-authentication/"/>
    <updated>2012-08-14T22:07:00-06:00</updated>
    <id>http://blog.earaya.com/blog/2012/08/14/hmac-user-authentication</id>
    <content type="html"><![CDATA[<p><a href="http://twitter.com/derrickisaacson">@derrickisaacson</a> recently showed me a really slick way to do user authentication with HMACs. Here&rsquo;s a real quick overview on how to accomplish it:</p>

<h3>User Sign In</h3>

<p>One of the cool things about using HMACs for authentication is that you can keep your current sign in process. Validate user credentials however you see fit, and then, upon credential validation, hand your user something like the following token:</p>

<p><code>&lt;userId, timestamp, hmac(userId + timestamp)&gt;</code></p>

<p>The idea behind this is that once a user has proved his identity, we can hand them the above token as an authentication code that will be able to prove their identity going forward. The client, of course, will have to submit the token on every subsequent request to the server so, if your client is a browser, you may want to consider sending the token down in a cookie.</p>

<p><em>A note of caution:</em> If an attacker is able to intercept or sniff the token when sent to the client, all bets are off and this, and just about any other authentication scheme, provides no protection. Please make sure you encrypt your client connection when using the ideas describe here.</p>

<p>While you have to worry about safely delivering the token, you don&rsquo;t have to worry about legitimate users trying to modify the token. This is because even though clients will have access to the token, they can&rsquo;t recalculate the HMAC of the data handed to them; they don&rsquo;t have the key that your server used to calculate the hash.</p>

<h3>Request Authentication</h3>

<p>Now that your client has the necessary token to make authenticated requests, all you have to do is verify the token. This is accomplished by recalculating the HMAC of the token they&rsquo;ve sent you and comparing it to the one produced on your server for authentication purposes. Here&rsquo;s some pseudocode to make the point clearer:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">HmacToken</span> <span class="n">clientToken</span> <span class="o">=</span> <span class="n">HmacToken</span><span class="o">.</span><span class="na">parse</span><span class="o">(</span><span class="n">stringWithTokenFromClient</span><span class="o">);</span>
</span><span class='line'><span class="n">HmacToken</span> <span class="n">serverToken</span> <span class="o">=</span> <span class="n">HmacTokenGenerator</span><span class="o">.</span><span class="na">generateToken</span><span class="o">(</span><span class="n">clientToken</span><span class="o">.</span><span class="na">getUserId</span><span class="o">(),</span> <span class="n">clientToken</span><span class="o">.</span><span class="na">getTimestamp</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'><span class="k">return</span> <span class="n">clientToken</span><span class="o">.</span><span class="na">getSignature</span><span class="o">()</span> <span class="o">==</span> <span class="n">serverToken</span><span class="o">.</span><span class="na">getSignature</span><span class="o">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can adjust the above code to also validate the timestamp and reject the token if it&rsquo;s too old (and require the user to sign in again), but you get the point.</p>

<p>One of the best things of using this idea for authentication is that you can now validate requests without keeping any sort of state: every request contains all the information you need to validate it. No need to keep any sort of session tokens around, and no need to do any kind of lookups; you&rsquo;re completely stateless. Pretty awesome, huh?</p>

<h3>Some Recommendations</h3>

<p>Don&rsquo;t write your own cryptography code. Use a well known (preferably open source) library to calculate your HMACs. It&rsquo;s hard to get cryptography right, so this is an area were relying on the experience of experts is well worth it.</p>

<p>Also, make sure you have an effective key management and rotation strategy. If your HMAC key is compromised, you&rsquo;re toast: people will be able to create tokens illegitimately. I&rsquo;d recommend you generate a new random key every time you deploy so that no one has knowledge of it and it is changed often. If you can&rsquo;t do that, at least make sure the key is never checked in anywhere in code and just very few individuals have access to it.</p>

<p>I really like this authentication scheme: the HMAC key is never sent to the client, so there&rsquo;s no key sharing and negotiation to get wrong. Furthermore, with very few changes to any authentication process, you can get a stateless, distributed authentication mechanism that is simple to write and easy to understand.</p>

<p>Give this a try and let me know what you think. Or if you see any big gaping holes, please let me know.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Char Arrays For Passwords]]></title>
    <link href="http://blog.earaya.com/blog/2012/07/25/using-char-arrays-for-passwords/"/>
    <updated>2012-07-25T14:52:00-06:00</updated>
    <id>http://blog.earaya.com/blog/2012/07/25/using-char-arrays-for-passwords</id>
    <content type="html"><![CDATA[<p>Just a few days ago a co-worker asked me about mongodb&rsquo;s choice of <code>char[]</code> for the password in their <a href="http://api.mongodb.org/java/current/com/mongodb/DB.html#authenticate(java.lang.String,%20char[])">authenticate</a> call. He was wondering why they chose <code>char[]</code> over <code>String</code>.</p>

<p>The reason is simple: some people feel that using a <code>char[]</code> is safer since you can &ldquo;wipe out&rdquo; the array after you&rsquo;re done with the data (eg. the password). <code>String</code> being an immutable type does not allow this precaution; once you&rsquo;re done with the <code>String</code> it&rsquo;ll live in memory until garbage collection decides to claim it at an undetermined time in the future.</p>

<p>The implication, of course, is that an attacker may read the contents of the memory before the <code>String</code> is garbage collected and steal the password.</p>

<p>This, however, seems a moot to me. Switching to a <code>char[]</code> does not really solve the problem; it only reduces the time window in which the memory contents are available to an attacker. This is even worse than <a href="http://en.wikipedia.org/wiki/Security_through_obscurity">security through obscurity</a>; really, this is just giving up and pretending to do something to address the problem. I&rsquo;ll also say that if someone is able to read the contents of your memory space, you have bigger problems than using a <code>String</code> for your password.</p>

<p>Nonetheless, there is one slight advantage in using character arrays for sensitive data: you&rsquo;re less likely to accidentally print the contents of an <code>Array</code> (to a log, or console, etc.) than the contents of a <code>String</code>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kt">char</span><span class="o">[]</span> <span class="n">secret</span> <span class="o">=</span> <span class="s">&quot;Secret&quot;</span><span class="o">.</span><span class="na">toCharArray</span><span class="o">();</span>
</span><span class='line'><span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Value: &quot;</span> <span class="o">+</span> <span class="n">secret</span><span class="o">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>The above code, for example, does not print &ldquo;Secret&rdquo;. Rather, it prints the <code>@</code> character followed by the unsigned hexadecimal representation of the hash code of the object. This is because <code>Array</code> does not override <code>Object</code>&rsquo;s implementation of <code>toString</code>, so you just get the default behavior (see <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#toString()">documentation</a>). Still, I don&rsquo;t think this is enough &ldquo;security&rdquo; to warrant using a character array.</p>

<p>The real solution, would be to implement something like .NET&rsquo;s <a href="http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx"><code>SecureString</code></a>. A <code>SecureString</code> object behaves a lot like a regular <code>String</code> object except that its memory contents are automatically encrypted. Furthermore, a <code>SecureString</code> instance is immediately discarded when no longer in use. This approach offers real security and it gives developers are more appropriate API when handling strings.</p>

<p>If it wasn&rsquo;t for the fact that it sucks to handle strings as character arrays, I&rsquo;d say that the marginal security benefits of using a <code>char[]</code> are worth it. But really, there&rsquo;s no way to rationally justify this. Just keep using a <code>String</code>, or if you really care, find a good <code>SecureString</code> implementation.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Octopress Review]]></title>
    <link href="http://blog.earaya.com/blog/2012/07/17/octopress-review/"/>
    <updated>2012-07-17T21:55:00-06:00</updated>
    <id>http://blog.earaya.com/blog/2012/07/17/octopress-review</id>
    <content type="html"><![CDATA[<p>Now that I&rsquo;ve been using Octopress for a few weeks, I thought I&rsquo;d just share some of my initial thoughts on it.</p>

<h3>It Rocks!</h3>

<ol>
<li><p>You can have your blog up and running in under 10 minutes. Furthermore, if you use GitHub pages to host your blog, you get a deployment process out of the box with very little effort.</p></li>
<li><p>The HTML template that Brandon Mathis has setup is fantastic. My favorite part about the template is how responsive it is, and how well it works in mobile devices. Go ahead, give this site a try on your phone; the template works really well there.</p></li>
<li><p>The product really makes it a joy for hackers to blog. I&rsquo;ve never had so much fun setting up my blog and writing posts.</p></li>
</ol>


<h3>Yet</h3>

<p>Howerver, there are a few things I wish were a bit different.</p>

<ol>
<li><p>On version 2.0, it seems like JS is a second class citizen. Scripts are not compiled, minified, and stitched into a single file.</p>

<p> I know there&rsquo;s an effort to change in this in version 2.1, but I disagree with the direction Brandon has taken. He really should be using <a href="http://requirejs.org">RequireJS</a>.</p></li>
<li><p>I wish there was tighter integration with S3. There have been several pull requests to add this feature, but they have yet to be merged.</p>

<p> It&rsquo;d be nice, for example, to get a deployment process that also uploads gzipped files (sets the headers on S3 for them) and also sets reasonable cache headers for different file types.</p>

<p> Little side note here: <a href="http://s3tools.org/">s3cmd</a> is awesome. The sync command will compare the files you&rsquo;re trying to upload and only upload changes. Can&rsquo;t recommend it enough.</p></li>
<li><p>A matter of preference, but I wish Mustache or Handlebars was the templating language. Now, to be fair, I think this is a Jekyll dependency, so it&rsquo;s not really an issue with Octopress directly.</p></li>
</ol>


<h3>The Veredict</h3>

<p>If you love tinkering, Octopress is for you. You&rsquo;ll have an awesome blog quickly and you&rsquo;ll enjoy tweaking it to your heart&rsquo;s content.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hosting a Static Website on Amazon S3 &amp; CloudFront]]></title>
    <link href="http://blog.earaya.com/blog/2012/07/13/hosting-a-static-website-on-amazon-s3-and-cloudfront/"/>
    <updated>2012-07-13T21:33:00-06:00</updated>
    <id>http://blog.earaya.com/blog/2012/07/13/hosting-a-static-website-on-amazon-s3-and-cloudfront</id>
    <content type="html"><![CDATA[<p>There are plenty of guides on how to host your static content on S3 and CloudFront, so in this tutorial I&rsquo;ll just focus on some of the likely problems you&rsquo;ll run into, and some good tips on making things better.</p>

<h3>S3</h3>

<p>The first thing you&rsquo;ll have to do to host your static content is create an S3 bucket to upload your content. This is straight forward, but there are three things you must make sure are setup correctly:</p>

<ol>
<li><p>Make sure the bucket&rsquo;s name matches the URL you want to use. For example, for this blog, the bucket&rsquo;s name is blog.earaya.com.</p></li>
<li><p>Enable website browsing.</p>

<p> This is done by clicking the &ldquo;Properties&rdquo; button for your bucket (up on the right hand corner) and then clicking on the &ldquo;Website&rdquo; tab. You&rsquo;ll then just have to check the &ldquo;Enabled&rdquo; checkbox, and choose your Index and Error Documents.</p>

<p> <img src="http://blog.earaya.com/images/posts/s3-web-settings.png" title="Enable Static Website on S3" alt="S3 Static Website Settings"></p>

<p> This step allows you to create an HTTP endpoint through which your assets can be accessed. It&rsquo;s important to note this endpoint as we&rsquo;ll be using it later to setup CloudFront.</p></li>
<li><p>Setup a bucket policy allowing the &ldquo;GetObject&rdquo; action to anonymous users. You&rsquo;ll want to make sure you do this at the bucket level so you don&rsquo;t have to set ACLs for every new file you upload.</p></li>
</ol>


<p>Again, this is done by clicking on the &ldquo;Properties&rdquo; button, then on the &ldquo;Permissions&rdquo; tab, and then adding the following bucket policy:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="nt">&quot;Version&quot;</span><span class="p">:</span> <span class="s2">&quot;2008-10-17&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;Id&quot;</span><span class="p">:</span> <span class="s2">&quot;Policy1342052196146&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;Statement&quot;</span><span class="p">:</span> <span class="p">[</span>
</span><span class='line'>      <span class="p">{</span>
</span><span class='line'>          <span class="nt">&quot;Sid&quot;</span><span class="p">:</span> <span class="s2">&quot;Stmt1342052192499&quot;</span><span class="p">,</span>
</span><span class='line'>          <span class="nt">&quot;Effect&quot;</span><span class="p">:</span> <span class="s2">&quot;Allow&quot;</span><span class="p">,</span>
</span><span class='line'>          <span class="nt">&quot;Principal&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'>              <span class="nt">&quot;AWS&quot;</span><span class="p">:</span> <span class="s2">&quot;*&quot;</span>
</span><span class='line'>          <span class="p">},</span>
</span><span class='line'>          <span class="nt">&quot;Action&quot;</span><span class="p">:</span> <span class="s2">&quot;s3:GetObject&quot;</span><span class="p">,</span>
</span><span class='line'>          <span class="nt">&quot;Resource&quot;</span><span class="p">:</span> <span class="s2">&quot;arn:aws:s3:::YOURBUCKETNAMEHERE/*&quot;</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>  <span class="p">]</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>It&rsquo;s important not to miss this step as you&rsquo;ll get an &ldquo;AccessDenied&rdquo; error on your documents if try to browse the website endpoint.</p>

<p>That&rsquo;s it as far as S3 goes. You&rsquo;re now ready to setup CloudFront.</p>

<h3>CloudFront</h3>

<p>The only thing worth noting here is that while you&rsquo;re going through the wizard that creates your distribution, you <strong>DO NOT</strong> want to use your S3 bucket as your &ldquo;Origin Domain Name&rdquo;. Rather, you want to use the &ldquo;Endpoint&rdquo; URL you got while setting up S3 as described in Step 2 above.</p>

<p><img src="http://blog.earaya.com/images/posts/cf-origin-settings.png" title="Create Website Origin on CF" alt="CF Origin Settings"></p>

<p>When you&rsquo;re done with the wizard, note the domain name for your CloudFront distribution. You&rsquo;ll need it to setup DNS.</p>

<h3>DNS (Route 53)</h3>

<p><img src="http://blog.earaya.com/images/posts/route53-cname-settings.png" width="400" title="Setup CNAME on Route 53" alt="DNS Settings"></p>

<p>Add a CNAME, and point it to the domain name of your CloudFront distribution. This blog, for example, points &ldquo;blog.earaya.com&rdquo; to <a href="http://d31e45oz3360lh.cloudfront.net">d31e45oz3360lh.cloudfront.net
</a> (which is what I got when I finished setting up my CF distribution as described above).</p>

<p>You don&rsquo;t necessarily have to setup DNS on AWS; your domain name registrar can likely add a CNAME record and point it to the CloudFront URL as well.</p>

<p>I mostly ended up using Route53 because I felt like trying it out. Your registrar probably has decent DNS service, so you could just use that instead.</p>

<h3>Performance Tips</h3>

<p>S3, being just a store, won&rsquo;t compress files for you. If you want to serve compressed content (which you should), you&rsquo;ll have to jump through quite a few hoops to get that working. Look at the &ldquo;Serving Compressed Files From Amazon S3&rdquo; in this <a href="http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html">guide here</a>.</p>

<p>Also, S3 won&rsquo;t set any caching headers by default. You&rsquo;ll have to make sure you set the appropriate headers manually on each file you want cached. The good news is that although setting this up is tedious, CloudFront will respect your caching headers and evict content appropirately.</p>

<h3>Summary</h3>

<p>Hopefully you&rsquo;ve gotten a good idea of how to setup your static site on S3 and CloudFront. It&rsquo;s really not hard to do at all.</p>

<p>Considering, however, that you can get a micro linux instance for free on EC2, I&rsquo;d say it&rsquo;s worth exploring using a good web server to serve your content instead of using S3. That way, you could get caching and compression for free.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hello World]]></title>
    <link href="http://blog.earaya.com/blog/2012/07/11/hello-world/"/>
    <updated>2012-07-11T15:38:00-06:00</updated>
    <id>http://blog.earaya.com/blog/2012/07/11/hello-world</id>
    <content type="html"><![CDATA[<p>I’ve started a new blog, and this is it.</p>

<p>I feel like my <a href="http://adotnetdude.blogspot.com">old blog</a> just didn’t fit me anymore; I certainly don’t feel like I’m “a dotnet dude” anymore.</p>

<p>I’ve also been itching to build a blog just out of static files, host them on <a href="http://aws.amazon.com/s3/">S3</a> and serve them through <a href="http://aws.amazon.com/cloudfront/">CloudFront</a>. So I finally bit the bullet and got it all done; I&rsquo;ll be writing a bit about how to host your own <a href="http://octopress.org">Octopress</a> blog on S3 later.</p>

<p>One last thing: I&rsquo;d like to thank <a href="http://www.erikzaadi.com/">Erik Zaadi</a> for his awesome <a href="https://github.com/erikzaadi/solarized-octopress-theme">Solarized Octopress Theme</a>; it&rsquo;s a great theme, don&rsquo;t you think?</p>
]]></content>
  </entry>
  
</feed>
