<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Michael Crosby</title><link href="http://crosbymichael.com/" rel="alternate"></link><link href="http://crosbymichael.com/feeds/all.atom.xml" rel="self"></link><id>http://crosbymichael.com/</id><updated>2013-04-28T00:00:00Z</updated><entry><title>Favorite Tech</title><link href="http://crosbymichael.com/favorite-tech.html" rel="alternate"></link><updated>2013-04-28T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-04-28:favorite-tech.html</id><summary type="html">&lt;p&gt;I am always working with different projects and technologies so here are some of my favorite projects and tools so far this year.&lt;/p&gt;
&lt;h2&gt;Vagrant and Puppet&lt;/h2&gt;
&lt;p&gt;If you are not using virtual machines for development and testing then you are a fool.  There is no reason to run code on your local machine when you can reproduce your production configurations in a virtual machine.  Vagrant allows you to quickly spin up new boxes and Puppet manages provisioning your boxes and production servers easy and consistently.&lt;/p&gt;
&lt;p&gt;When I want to test a new database or mess around with a new package I always spin up a new box with Vagrant and do my testing in a contained environment so that I don't kill my server or development machines.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.vagrantup.com/"&gt;Vagrant&lt;/a&gt;
&lt;a href="https://puppetlabs.com/"&gt;Puppet&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Editors&lt;/h2&gt;
&lt;p&gt;I have been using Sublime Text 2 a lot over the past few months.  However I have noticed that when working with a few large projects and a few different windows open with these projects it really slows down.  Not just switching back and forth between windows and files but typing; that really bothers me.  So I am back in Vim for my editing needs.  I don't use gVim but Vim from the terminal because that is where I spend most of my time.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;If you take the time to learn and configure Vim then it will be your best friend.  You just have to invest in the editor, something that others do not have the time or patience for.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.vim.org/"&gt;Vim&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Redis&lt;/h2&gt;
&lt;p&gt;Redis is an awesome key value store that keeps the entire dataset in memory.  I have been using redis as a work queue because of the awesome PUSH and POP commands for working with lists.  You can see how powerful redis is just by viewing the &lt;a href="http://redis.io/commands"&gt;commands&lt;/a&gt;.  Redis is like a developers swiss army knife, it has many different tools to solve many different problems.  It is not only efficient as a data store but also efficient to program for.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;You can checkout my redis queue package for Go on &lt;a href="https://github.com/crosbymichael/rqgo"&gt;github&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://redis.io/"&gt;Redis&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Backbone.js and Bootstrap&lt;/h2&gt;
&lt;p&gt;Even though I would rather be writing low level code or building apis I still have to write html and js for web projects.  Backbone.js makes this simple and structured.  Backbone.js is a client side javascript mvc framework for interfacing with a backend RESTful api that adds structure to javascript.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Twitter Bootstrap is a html, js, css framework for building web apps.  It is so awesome and helps you produce great looking pages with minimal design skills.  It includes many javascript and html components to use that make working on a web project simple while keeping a high level of quality.&lt;/p&gt;
&lt;p&gt;This takes me to an aside, I believe the way to build web applications now is with static html and a client side mvc framework with a RESTful api on the backend.  Using a client side mvc framework creates a clear separation between a view layer and your backend code.  You cannot hack anything into view data, session, or whatever you crappy developers do.  On the client side you only have the data and interactions provided by the backend api.  Backbone.js allows you to split development between frontend developers and the backend developers working on the domain model and services.&lt;/p&gt;
&lt;p&gt;This also makes sites very fast.  Instead of rendering templates on the server side, all the templates are rendered client side with simple api calls to the server for CRUD operations.  This separation also helps you decouple your backend implementation from your frontend view layer.  Because the frontend connects to the backend via a JSON api you could change your entire backend's framework, implementing language, or scale horizontally without the tight coupling that you get when rendering templates server side.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;This also makes deployments easy.  If you have client side changes you can just push the new html and javascript to the server and you are done.  No need to compile your backend code for frontend changes.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://documentcloud.github.io/backbone/"&gt;Backbone.js&lt;/a&gt;
&lt;a href="http://twitter.github.io/bootstrap/"&gt;Bootstrap&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;CoffeeScript&lt;/h2&gt;
&lt;p&gt;CoffeeScript is a simple and elegant language that is compiled into javascript.  We all know that javascript sucks and CoffeeScript aims to make it better.  Here is some simple CoffeeScript with Backbone.js.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;FeedList&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Backbone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt;
    &lt;span class="nv"&gt;el: &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;.feedlist&amp;#39;&lt;/span&gt;

    &lt;span class="nv"&gt;render: &lt;/span&gt;&lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="nv"&gt;view = &lt;/span&gt;&lt;span class="nx"&gt;@&lt;/span&gt;
        &lt;span class="nv"&gt;feeds = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Feeds&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="nx"&gt;feeds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nv"&gt;success: &lt;/span&gt;&lt;span class="nf"&gt;(feeds) -&amp;gt;&lt;/span&gt;
                &lt;span class="nv"&gt;template = &lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;#feedlist-template&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;feeds&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;feeds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;models&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
                &lt;span class="nx"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now lets see what this compiles down to:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="nx"&gt;FeedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_super&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;__extends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;FeedList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_super&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;FeedList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;_ref7&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;FeedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__super__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;_ref7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;FeedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;.feedlist&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;FeedList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;feeds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;view&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;feeds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Feeds&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;feeds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;feeds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;#feedlist-template&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;feeds&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;feeds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;models&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;FeedList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;Backbone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is a huge difference, which one would you rather write?  When you find a part of your job that is a pain, look for tools that will help.  Web development is changing a lot and you (or the people in charge) are the only ones prohibiting you from performing your job efficiently and actually having fun.  There tools are out to help you write better code, with fewer bugs, and enjoy what you are doing.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://coffeescript.org/"&gt;CoffeeScript&lt;/a&gt;;&lt;/p&gt;
&lt;h2&gt;Microsoft land&lt;/h2&gt;
&lt;p&gt;In the Microsoft world I have been using F#.  It is really nice to look at problems from the functional perspective.  I am super lazy so instead of writing documentation for an assembly I wrote a small class that will iterate over an entire assembly and create instances of any type (no matter if they have complex constructors) and extract documentation and other data from the types.  I initially wrote it in C# and it was around 370+ lines of code.  I decided to rewrite it in F# and dropped the line count to 85.  It is very fast and efficient to code in.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://fsharpforfunandprofit.com/why-use-fsharp/"&gt;F#&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Rant&lt;/strong&gt;
I think that is everything for now.  Oh ya, everything on this list is open source and free to use.  Fuck vendor lock and drinking the koolaid of using shitty tools because that is just what we use.  Use the best tool for the job, shit tools produce shit product and is a pain to use. &lt;/p&gt;</summary></entry><entry><title>Golang JSON RPC</title><link href="http://crosbymichael.com/golang-json-rpc.html" rel="alternate"></link><updated>2013-04-23T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-04-23:golang-json-rpc.html</id><summary type="html">&lt;p&gt;Go's rpc package has an example for the default rpc implementation but nothing for the jsonrpc package.  Here is how to get it work.  The rpc example uses some helpers built into the rpc package, I just looked at that code in Go's stdlib to figure out the proper way to get the jsonrpc package to work.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="nf"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;fmt&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;log&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net/rpc&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;net/rpc/jsonrpc&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;test/server&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;startServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;arith&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Arith&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;rpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arith&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandleHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DefaultRPCPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DefaultDebugPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;tcp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;:8222&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;listen error:&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;go&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ServeCodec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonrpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewServerCodec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;go&lt;/span&gt; &lt;span class="n"&gt;startServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Dial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;tcp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;localhost:8222&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;

    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;jsonrpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Arith.Multiply&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;arith error:&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Arith: %d*%d=%d&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</summary></entry><entry><title>Go Lang</title><link href="http://crosbymichael.com/go-lang.html" rel="alternate"></link><updated>2013-04-12T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-04-12:go-lang.html</id><summary type="html">&lt;p&gt;If you don't know about Go it's a programming language for now.  Most of the languages that we use everyday are old, 10+ years old.  After so many years languages need to start to think about news ways to solve the same problems.  Go is a new language that takes a different stance on how you write and deploy code.  Go is a dynamically typed but statically compiled language.  I have "played" with Go pre 1.0 a few times and at the 1.0 release but it never really stuck.  I felt like I was writing to much code for what I wanted to accomplish.  However, this last time working with Go 1.1beta I have started to do a few projects and packages with Go and I am really impressed with the language.&lt;/p&gt;
&lt;h2&gt;Abstractions&lt;/h2&gt;
&lt;p&gt;I write a lot of Python code and I still enjoy writing Python more than any other language so I am used to a fairly high level of abstraction from the language.  Even working in C# with tools like LINQ you get trained to expect abstractions for working with collections.  Also when used correctly, I really like having generics available in my language.&lt;/p&gt;
&lt;p&gt;You miss these things with Go.  Go is at a lower level of abstraction in many areas.  Looping and working with arrays and slices ( variable sized collections ) is often back to the for i loop.  Going from LINQ to anything less hurts.  I find my self working with []byte more in Go than I have in any other language.  The Go stdlib does provide you with some abstractions in the io packages but you still are working with byte arrays often.&lt;/p&gt;
&lt;p&gt;Lets look at reading a file line by line in both Python and Go.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;names.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Python is pretty straight forward.  Very simple to remember and can be done with no imports from the stdlib.  Now lets look at the Go example.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="nf"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;bufio&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;fmt&amp;quot;&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;os&amp;quot;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;names.txt&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;scanner&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;bufio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewScanner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Scan&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scanner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So the go example is a little more than just 3 lines of code.  We have the package declaration, 3 imports from the stdlib, and the go error handling code in the main func.  Even though Go has more code it is still simple and elegant.  You do have to learn the stdlib to do simple tasks but once you do, you have a very powerful set of tools.  The go example is every more amazing once you learn about implicit interface implementation.&lt;/p&gt;
&lt;h2&gt;No Classes&lt;/h2&gt;
&lt;p&gt;I like OOP and modeling classes and their interaction with each other to create a fluent and straight forward API.  However, I also feel like OOP is taken too far.  People will insist that you have to design a polymorphic interface, provide factories, and strategies when all you really need to a simple function.  I believe in design patterns for solving problems in software but I also believe in simplicity, clarity, and elegance in the code that I write.  For me, simple wins over "over engineering."  Knowing when to design and when to bang out a function comes with experience.&lt;/p&gt;
&lt;p&gt;With Go you have types and interfaces but no classes and inheritance.  At the beginning this was hard for me.  However, after working with packages, types, and interfaces in Go I do not miss classes.  I feel like my code is simple and straight forward with only types and packages.  It is OOP at it's purest, data with actions.  You can still attach methods to your types and you have public and internal access via upper and lower case names.  If a type and/or function begins with an upper case it is exported to be used outside of that package, if it is lower case it is internal to that package.&lt;/p&gt;
&lt;p&gt;Interfaces in Go are also very modern.  You can have a type and implement an interface without explicitly stating that you implement that interface.  If we have an interface that has a&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;then in Go, any type that has a method Write that takes a byte array and returns an int implements that interface, which is exactly what you want.  With interfaces you are basically saying that you don't care about the type or other methods, all I care about is that whatever I have passed to me has a Write method that takes bytes and returns an int.&lt;/p&gt;
&lt;h2&gt;Why Go&lt;/h2&gt;
&lt;p&gt;So why am I writing a post on a language with no classes and missing certain abstractions that we are used to?  Because their some home run features that make Go an amazing language to work with.&lt;/p&gt;
&lt;h3&gt;Concurrency&lt;/h3&gt;
&lt;p&gt;There are many articles about concurrency in Go with gorutines and channels so I am not going to say much here except that they are awesome.&lt;/p&gt;
&lt;h3&gt;Compiled binary&lt;/h3&gt;
&lt;p&gt;Deploying a python application takes automation.  You need to to create a virtual environment, have a requirements file with all of your dependencies, and then finally copy your application code up to the server.  There are many tools for deploying python code and it really is not that bad but it is also not simple and there are many places that things can go wrong.&lt;/p&gt;
&lt;p&gt;With Go you have a compiled binary with all the dependencies compiled into the binary.  You do not even have to have the Go tool chain installed on the computer that you are deploying your go application to run the code.  You just drop the executable on the machine and your good to go, nothing else to do.  This is amazing.&lt;/p&gt;
&lt;p&gt;Also being compiled into native machine code makes go very fast.&lt;/p&gt;
&lt;h3&gt;Testing and Packages&lt;/h3&gt;
&lt;p&gt;Testing is simple and easy.  Automating testing is simple and easy.  Have you tried to automate C# unit tests?  Oh, you don't own VS Pro with unit testing?&lt;/p&gt;
&lt;p&gt;Installing dependencies is very easy with "go get".  You install dependencies exactly the same way as you have your own packages.&lt;/p&gt;
&lt;h3&gt;Go Doc&lt;/h3&gt;
&lt;p&gt;All of Go's documentation is auto generated by the commends in the code.  This includes packages in the stdlib and your own packages in your GOPATH.  I have been doing a lot of work in an area with no Internet access to I really appreciate being able to run the package documentation and other general Go documentation on my local machine.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;godoc&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;=:&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This provides me with all the documentation and examples available if I was connected online to the golang.org website.&lt;/p&gt;
&lt;h2&gt;Parting words&lt;/h2&gt;
&lt;p&gt;Hopefully this has peaked your interest in Go and maybe you can give it a try.  I am going to start using Go as my main language for backend services and processes.&lt;/p&gt;</summary></entry><entry><title>SecureString How To</title><link href="http://crosbymichael.com/securestring-how-to.html" rel="alternate"></link><updated>2013-03-09T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-03-09:securestring-how-to.html</id><summary type="html">&lt;p&gt;Working with the SecureString class in .NET is fairly easy.  This class allows you to keep string data encrypted in memory.  Maybe you load connection strings, passwords, or financial data into memory when your application starts or loads an object so the least you can do is use the SecureString class to keep this information encrypted while the data stays in memory.&lt;/p&gt;
&lt;h2&gt;Adding your data&lt;/h2&gt;
&lt;p&gt;Adding a string content to the class is very easy.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hushhush&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Something I need to keep on the down low&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;secureString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;SecureString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;chr&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;hushhush&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToCharArray&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;secureString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AppendChar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;secureString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MakeReadOnly&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;All you need to do is convert your string into a char array and append each char to the secure string.  After you have finished writing all data to the secure string you will want to call the MakeReadOnly method so that the instance becomes immutable.&lt;/p&gt;
&lt;h2&gt;Getting your data back out&lt;/h2&gt;
&lt;p&gt;Now this is where things get weird.  It's nice not to have a convenient method on the SecureString class to get to your data for security reasons but you actually have to work with pointers and unmanaged resources to get to the data.&lt;/p&gt;
&lt;p&gt;Because SecureString is a sealed class I made a little extension class that adds a GetString() method for reading the string data from the class.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SecureStringExtensions&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;SecureString&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;IntPtr&lt;/span&gt; &lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;IntPtr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;chars&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SecureStringToBSTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chars&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chars&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;finally&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;IntPtr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ZeroFreeBSTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The Marshal class actually has a BSTR method for use with the SecureString.  Just remember when working with unmanaged resources you will need to manually release and free the pointers that you used.&lt;/p&gt;
&lt;p&gt;Basically if you have not worked with Marshal and pointers in C# you will have to get the pointer to the char[] from the secure string.  Then we use Marshal.Copy to marshal the *char[] to our managed char[] using the length of the string.  A simple string.Join and we are home free.  Just remember to zero out our pointer to release the resources.&lt;/p&gt;</summary></entry><entry><title>F# Url Shortener</title><link href="http://crosbymichael.com/f-url-shortener.html" rel="alternate"></link><updated>2013-01-27T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2013-01-27:f-url-shortener.html</id><summary type="html">&lt;p&gt;Every year I try to learn a new language.  If you are a one trick pony you are not only limiting your self in opportunities but you are also limiting your thinking to the one paradigm that you know.&lt;/p&gt;
&lt;p&gt;So this year I wanted to learn something very different from what I am used to in the OO world ( Python, C#, Objective - C).  I decided to learn a functional language this year called F#.  I looked at a few different functional languages ( Scala, Erlang ) before choosing F#.  I felt that it would be the most practical language for what I do but still provide me with the paradigm shift I was seeking.  It does have OO parts being part of the .NET family but the functional paradigm still dominates it's landscape.&lt;/p&gt;
&lt;p&gt;Usually when I learn a new language I rewrite a program that I have already done before.  This helps me to focus on the language and not worry about the problem at hand because I already know the solution.  So this program is a url shortener that makes a request to the Google api via JSON.  I have written this url shortener in Python, Go, Objective - C, and C# so I know what I need to do and I have a reference.  So lets get started.&lt;/p&gt;
&lt;h2&gt;To the code!&lt;/h2&gt;
&lt;p&gt;The first thing we need to do to create this program in F# is to "open" our dependencies.  So at the top of a blank .fs file add these lines:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="n"&gt;light&lt;/span&gt;

&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Net&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Serialization&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We need the Net package for the WebRequest, IO for a StreamReader, Text for Encoding, and the Web.Script.Serialization for the JavaScriptSerializer so that we can convert the JSON response into a type that we can use.  You will have to add the System.Web.Extensions dll reference to your F# project to use the JavaScriptSerializer.&lt;/p&gt;
&lt;p&gt;Next we need to create a new type that the JavaScriptSerializer will deserialize the response to.  The response JSON looks like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;short url&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;&amp;quot;kind&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;kind of request&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;&amp;quot;longUrl&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;long url&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We will have to make this type a class with a default constructor so that the JavaScriptSerializer can create the instance.  The members of the type; id, kind, and longUrl will have to be mutable so that the serializer can set the values.  By default all types in F# are immutable unless set to mutable.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;longUrl&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;longUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So there is the Response type with the default constructor.  Next I will create another type that will hold all the request data that is needed.  This is more for convenience and not really needed.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;requestMethod&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now lets get the data required and populate a Request type.  We will receive the long url via the command line args and the other data such as request method and content type can be hard coded along with the url to the Google api.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetCommandLineArgs&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;https://www.googleapis.com/urlshortener/v1/url&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;requestMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.[&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h3&gt;Functions&lt;/h3&gt;
&lt;p&gt;We will need a few helper functions that can convert a string in to a byte array, read a Stream into a string, and deserialize the response string into our Response type.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;get_bytes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;read_to_string&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StreamReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadToEnd&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;deserialize_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JavaScriptSerializer&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;serializer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Deserialize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;read_to_string&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;These three functions are very straight forward and to the point.  One issue that I have ran into is that using classes from the .NET framework often requires a type annotation to be added to the function parameter(s) type inference cannot be used with overloaded methods.  It kinda sucks but it is what it is.  You will also notice the "use" keyword in the read_to_string function.  This is the same as the "using" keyword in C#.   Other than that everything is pretty obvious to a .NET developer looking at this code.&lt;/p&gt;
&lt;p&gt;Now we will need to create a WebRequest, add the default parameters, write our data to the request stream, and then read the response.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;add_params&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ContentType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contentType&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Method&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;requestMethod&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;write_to_stream&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetRequestStream&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_bytes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sprintf&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;{&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;longUrl&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;%s&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;}&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetResponse&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetResponseStream&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;print_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;%s&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Easy.  One of the things that you will notice is the lack of a return keyword.  This is because whatever is on the last line of a function is automatically returned.  You can see that in the write_to_stream function, even though it takes a webRequest as an input parameter, I set it on the last line so it can be returned.  In C# ( and other languages ) this is often known as a "Fluent api".  This will also help us a little bit later when we chain all of these functions together to process the request.&lt;/p&gt;
&lt;p&gt;You can also see the "&amp;lt;-" operator when I assign values from our request type to the webRequest.  You do not use the "=" operator for member assignment.&lt;/p&gt;
&lt;p&gt;Lastly, you may have noticed that I never created the actual webRequest in the above code snippet.  That is because in the final function we will create the webRequest and chain all of these functions together.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;get_short_url&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;HttpWebRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;add_params&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;write_to_stream&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;deserialize_response&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;print_response&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So get_short_url is the final function that ties everything together.  We create the webRequest and use it's value to chain all the remaining functions.  Each function consumes the result of the previous function this way we can use the "|&amp;gt;" to chain them together.  This is very LINQ like but taken to a whole new level in functional programming.&lt;/p&gt;
&lt;p&gt;So now all that is left is to get_short_url when a user passes in a long url on the command line.  For this we will use a pattern match in F#.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StartsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;http&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="bp"&gt;true&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;get_short_url&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="bp"&gt;false&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printfn&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;No url passed&amp;quot;&lt;/span&gt;

&lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Because our Request type's data member is a string we can use the standard .NET string function "StartsWith" to see if the first chars are "http".  Then by using the "match with" we can check for each boolean value to determine our control flow.  If true, we will call our get_short_url function, if false, we will tell the user that they did not pass a url.  If you are on Windows you will want to add the "Console.ReadLine" call at the end of the file or the command prompt will exit without you seeing your results.&lt;/p&gt;
&lt;h2&gt;Complete Code&lt;/h2&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="n"&gt;light&lt;/span&gt;

&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Net&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;
&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Serialization&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;longUrl&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;longUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;requestMethod&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetCommandLineArgs&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;https://www.googleapis.com/urlshortener/v1/url&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;requestMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;POST&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.[&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;contentType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;get_bytes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;read_to_string&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StreamReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadToEnd&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;print_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;%s&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;deserialize_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JavaScriptSerializer&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;serializer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Deserialize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;read_to_string&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;add_params&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ContentType&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contentType&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Method&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;requestMethod&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;write_to_stream&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetRequestStream&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_bytes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sprintf&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;{&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;longUrl&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;%s&lt;/span&gt;&lt;span class="se"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="s2"&gt;}&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;WebRequest&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;webRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetResponse&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetResponseStream&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;get_short_url&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;HttpWebRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;add_params&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;write_to_stream&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;deserialize_response&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;print_response&lt;/span&gt;

&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StartsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;http&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="bp"&gt;true&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;get_short_url&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="bp"&gt;false&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printfn&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;No url passed&amp;quot;&lt;/span&gt;

&lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReadLine&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So that's it.  I am still very fresh to F# and functional programming in general so my code does not look very elegant.  It feels a little verbose working with the .NET classes in a functional language but hopefully I will learn more and be able to remove some of the excess and make this program more idiomatic to functional programming.&lt;/p&gt;</summary></entry><entry><title>Infrastructure - Part 1</title><link href="http://crosbymichael.com/infrastructure-part-1.html" rel="alternate"></link><updated>2012-09-29T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-09-29:infrastructure-part-1.html</id><summary type="html">&lt;h2&gt;Servers&lt;/h2&gt;
&lt;p&gt;Running your own infrastructure is awesome, if your a hacker like me.  If your a square and you don't want to mess with your own hardware you have two choices if you are going to host your website or services online.  If you don't feel like spending $1k a year you will probably have a VPS or EC2 instance with less than 1GB of RAM.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;You can get a shared hosting provider and use what they give you.  Basically PHP and Mysql.  I run a heavy Python stack and Postgesql.  PHP is not an option for me.  Also 1GB of ram is not enough.  I have to run a Minecraft server for myself and a few friends and I need at least 3-4GB of RAM just for Minecraft.  ( This is also a test of how robust my setup is because if Minecraft would go down my friend my hurt himself. )  I also like running other services like jenkins as a build server.  You still have to spend some money on things like a battery backup or upgrade your internet connection.&lt;/p&gt;
&lt;p&gt;The trick to running your website on your own hardware and internet connection is to host all your content, images, js, css, etc.. on a CDN and only have your servers render the HTML and do database operations.  Leave any content on S3.  I run my websites on my own server and have S3 host all the images and content.  You cannot tell the difference loading my page.&lt;/p&gt;
&lt;p&gt;This is the first post of all the shit I have running.  I'll try to give a high level overview and for some posts I will go into detail on the setup and settings.  Keep an eye out for some of my shell scripts and other cool downloads.&lt;/p&gt;
&lt;h2&gt;Ajenti&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Admin Console&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Dashboard" src="images/ajenti-dashboard.png" /&gt;&lt;/p&gt;
&lt;p&gt;Ajenti is an easy to install Python app that provides information about services running on your server. It also provides a nice UI for reading logs, managing cron jobs and users, and many other tasks.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;On the main dashboard I can see the load and memory usage for my main node and the build server. &lt;/p&gt;
&lt;p&gt;&lt;img alt="Plugins" src="images/ajenti-plugins.png" /&gt;&lt;/p&gt;
&lt;p&gt;Ajenti has a nice plugin system.  I have not dug into developing a plugin but it looks fairly straight forward.  The default selection is great and plugins are fast and easy to install.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Munin&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The charts that you are seeing are from Munin.  It is a system monitoring application that can monitory local and remote systems.  You need one computer as the server and then install munin-node on each computer (including the server) that you want to monitor.  Munin is in apt and you only have a few lines to modify in the munin configs after install to get it up and running.  You will have to have a HTTP server to serve the munin graphs so that ajenti can read them into the dashboard.&lt;/p&gt;
&lt;p&gt;Check out the rest of these screenshots and view the links at the bottom of the page.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Logs&lt;/em&gt;&lt;/strong&gt;
&lt;img alt="Logs" src="images/ajenti-logs.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Users&lt;/em&gt;&lt;/strong&gt;
&lt;img alt="Users" src="images/ajenti-users.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Health&lt;/em&gt;&lt;/strong&gt;
&lt;img alt="Health" src="images/ajenti-health.png" /&gt;&lt;/p&gt;
&lt;h2&gt;Links&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://ajenti.org/"&gt;Ajenti&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://munin-monitoring.org/"&gt;Munin&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</summary></entry><entry><title>Usage Patterns</title><link href="http://crosbymichael.com/usage-patterns.html" rel="alternate"></link><updated>2012-09-28T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-09-28:usage-patterns.html</id><summary type="html">&lt;p&gt;&lt;strong&gt;&lt;em&gt; Update: I will have updated graphs in a day or two.  My time tracking app was only exporting the top 10 applications and I just found the setting to export the top 25.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I always have an app running on my computer that logs all the time I spend in each application.  It helps to give some interesting insights how, what, how long, what days, and times do I do certain tasks.  You can see what hours of the day that you are productive and what hours you just surf the web.&lt;/p&gt;
&lt;h2&gt;Usage by App&lt;/h2&gt;
&lt;p&gt;&lt;img alt="Apps" src="images/hours-by-app.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Just for clarification:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;iTerm - terminal app&lt;/li&gt;
&lt;li&gt;Forklift - file/ssh/ftp file management&lt;/li&gt;
&lt;li&gt;SublimeText 2 - Text Editor&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So Chrome wins pretty easy but I do spend a lot of my time in a terminal.  I hate pixels and gui's.  I wish I could do everything from the command line.  The other 21% of my time is spent programming in SublimeText.  Even though it is not a large amount 5% of my time sitting at my computer is managing files in Forklift.  That is still a lot of time for something like moving a file from one directory to another.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Usage by Hour" src="images/usage-by-hour.png" /&gt;
&lt;img alt="Apps by hour of day" src="images/apps-by-time-of-day.png" /&gt;&lt;/p&gt;
&lt;p&gt;These two graphs show pretty much the same thing.  What hours of the day do I must most of my work.  8pm-10pm is when I do most of my work.  However I do like the area graph about what apps I am using at what time.&lt;/p&gt;
&lt;p&gt;You can see that I have a large spike in the morning with Chrome.  This is when I get up and surf the internet looking for news or reading RSS feeds.  I have a pretty constant usage of the terminal throughout the entire day but my development really picks up in the later hours with SublimeText and iTerm.&lt;br /&gt;
&lt;/p&gt;
&lt;h2&gt;Hours by day&lt;/h2&gt;
&lt;p&gt;&lt;img alt="Day of Week" src="images/hours-by-day-of-week.png" /&gt;&lt;/p&gt;
&lt;p&gt;Thursday, Saturday, and Sunday are my big computing days.  This mostly because I am at work throughout the week so I do what I want on the weekends.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Usage by Day" src="images/hours-by-day.png" /&gt;&lt;/p&gt;
&lt;p&gt;Finally my usage throughout the past few months.  You can read this graph a few different ways.  You can look at the usage trends over time and see if I am starting to use a computer more or less.  You can also look at how many hours I am on a computer during a single day.  On average it's about ~4.5hrs a day.  Not too bad at all.&lt;/p&gt;
&lt;p&gt;You can study the graphs if you want and see if doing something like this would be advantageous to you.  I always enjoy looking over data and seeing trends in my own life and how I use and interact with things.  You can use data like this to determine when you are most productive by hour or day, day of week, etc...&lt;br /&gt;
&lt;/p&gt;</summary></entry><entry><title>Development Infrastructure</title><link href="http://crosbymichael.com/development-infrastructure.html" rel="alternate"></link><updated>2012-09-10T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-09-10:development-infrastructure.html</id><summary type="html">&lt;p&gt;Having servers that automate much of your development process is cool.  Entities living out on the web just waiting to process any task that you throw their way.&lt;/p&gt;
&lt;p&gt;I currently have two servers in my cluster that run my network.  Everything from my public web sites, to mongodb and Postgresql, to private git and build servers.  Why have all of this?  Because it makes me happy when I push changes to my git server and within seconds the project is built, unit tests ran, and a report is emailed to me.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Servers" src="images/infrastructure.png" /&gt;&lt;/p&gt;
&lt;h2&gt;Public Site&lt;/h2&gt;
&lt;p&gt;I run a Python stack for my web server.  Python 2.7 with django as the framework running on-top of lighttpd.  Lighttpd also acts as the proxy server for the rest of the services that run on the cluster.  Postgresql is the datastore for my sites.  I still run mongodb on the server for quick, scheme-less storage.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;On Node1 is my git repositories via &lt;a href="http://www.atlassian.com/software/stash/overview"&gt;Stash&lt;/a&gt;.  Stash is not necessary but it provides a nice UI for managing repositories and users.  I could do all of this via ssh but for $10 Starter License it is worth it.  You can easily browse and see diffs on the web as well.&lt;/p&gt;
&lt;p&gt;The only drawl back to Stash is that is runs on-top of Java and hogs RAM.  The Stash instance consumes about 700-900MB of RAM for not doing much.  The other memory hog is a minecraft server that I run.  Minecraft consumes about 2GB+.  This server has to be robust because if the minecraft sever goes down, I will have some angry users texting me until it's back online.&lt;/p&gt;
&lt;h2&gt;Build server&lt;/h2&gt;
&lt;p&gt;I run &lt;a href="http://jenkins-ci.org"&gt;Jenkins-Ci&lt;/a&gt; as my build server.  I have tried Bamboo for a week but the license was too restrictive for my needs, ( not enough jobs on the starter license ).  For my python projects Jenkins has many nice plugins.&lt;/p&gt;
&lt;p&gt;Even though the build server is running on Debian it can build both Python and .NET projects thanks to &lt;a href="https://github.com/mono/mono"&gt;mono&lt;/a&gt; ( xbuild ).  I am having some issues running nunit tests for a non-root user.  I have not been able to figure out what the issue is yet so my .NET projects do not have automated tests.&lt;/p&gt;
&lt;h2&gt;Server Software&lt;/h2&gt;
&lt;p&gt;Both servers run Debian and are administered via ssh, no passwords just ssh keys.  Each node does it's daily backup to the other with ~3day off-site dumps to Amazon S3.&lt;/p&gt;
&lt;h2&gt;Node1&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Debian 6&lt;/li&gt;
&lt;li&gt;lighttpd&lt;/li&gt;
&lt;li&gt;git 1.7+ ( built from src )&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.djangoproject.com"&gt;django&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.mongodb.org"&gt;mongodb&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;postgresql&lt;/li&gt;
&lt;li&gt;sun-java&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.atlassian.com/software/stash/overview"&gt;stash&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Amazon S3 as CDN for content&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Domains Hosted&lt;/strong&gt;
* &lt;a href="http://code-assistant.com"&gt;CodeAssistant&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Node2&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Debian 6&lt;/li&gt;
&lt;li&gt;sun-java&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jenkins-ci.org"&gt;jenkins&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="htts://github/mono/mono"&gt;mono&lt;/a&gt; ( built from git repo )&lt;/li&gt;
&lt;li&gt;git 1.7+( built from src )&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Future Changes&lt;/strong&gt;
Currently my blog &lt;a href="http://crosbymichael.com"&gt;crosbymichael.com&lt;/a&gt; ( this site ) is hosted on dreamhost.  Also HTTPAssistant is a wordpress site hosted on dreamhost as well.  My dreamhost service expires Feb. of next year so I will be moving both of these sites to Node1.  I will replace wordpress with a static one page landing page.  Hopefully I will have HTTPAssistant 2.0 done and replace that ugly thing.&lt;br /&gt;
&lt;/p&gt;</summary></entry><entry><title>web.py server setup</title><link href="http://crosbymichael.com/webpy-server-setup.html" rel="alternate"></link><updated>2012-08-04T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-08-04:webpy-server-setup.html</id><summary type="html">&lt;p&gt;Python is my favorite language to work with.  It is so versatile.  I can automate tasks, write quick scripts, process data, and even run a web application.  It is very portable and I love the syntax.  For a web framework I use web.py because it stays out of your way and does not impose itself on your application.  With other frameworks you have to do it their way or struggle the entire time.  It's awesome but setting up a "production" server is a bitch.  Tutorials suck and getting Python to run under Apache is a waste of 6+ hours with all the config bullshit.  Leave it and do it my way.&lt;/p&gt;
&lt;h2&gt;What you will learn:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Install lighttpd on Ubuntu/Debian&lt;/li&gt;
&lt;li&gt;Install MySQL&lt;/li&gt;
&lt;li&gt;Setup web.py to run under lighttpd&lt;/li&gt;
&lt;li&gt;Setup MySQLDB for Python to MySQL&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Ubuntu Server&lt;/h3&gt;
&lt;p&gt;Ubuntu or any Debian distro will work with this tutorial.  I am currently running my server on Ubuntu Server 11.04.  You need to make sure that Apache is not installed on your server.  If it is remove everything Apache.  Make sure you have shell plus access to install software on your server.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;MySQL&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now we will need to install MySQL on your server before we do anything else.  Just skip this part if you don't use MySQL.  This is very easy, just run these two commands to install MySQL and the development libraries that Python needs.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;mysql&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="n"&gt;libmysqlclient&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Remember that you should be running these as root or sudo.  If you didn't know this just give up now.  You will also need to setup your MySQL username and permissions for your web application to use.  I am not going to go over this process because I am lazy.  Google it if you don't know how.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;lighttpd&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now we can install the lighttpd server.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;lighttpd&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You can go ahead and browse to the site to see if lighttpd is functioning properly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Python Build Tools and MySQLDB&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With our web server and MySQL database setup on the server it is time to get our Python environment setup.  Start by installing the buildtools and other Python development tools.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;python&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;setuptools&lt;/span&gt; &lt;span class="n"&gt;python&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="n"&gt;build&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;essential&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will install "easy_install" and other tools so we can build the python MySQL driver.  After this is complete we need to download and install the MySQLDB driver.  This is easy if you have the libmysqlclient-dev and setuptools installed.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;wget&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz&lt;/span&gt;
&lt;span class="n"&gt;tar&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;zxf&lt;/span&gt; &lt;span class="n"&gt;MySQL&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;python&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.2.3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gz&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will download and unzip the driver.  Now "cd" in to the folder and run these two commands:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;build&lt;/span&gt;
&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;Web.py&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;All we have left to do is install web.py, flup, and configure lighttpd for our application.  The first two are easy.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;easy_install&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;
&lt;span class="n"&gt;easy_install&lt;/span&gt; &lt;span class="n"&gt;flup&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now lets configure lighttpd. &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;etc&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;lighttpd&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Replace the contents of lighttpd.conf with the text below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;mod_access&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;mod_alias&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;mod_compress&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;mod_accesslog&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/www&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upload&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;dirs&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/cache/lighttpd/uploads&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorlog&lt;/span&gt;             &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/log/lighttpd/error.log&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;             &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/run/lighttpd.pid&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;             &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;www-data&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;groupname&lt;/span&gt;            &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;www-data&amp;quot;&lt;/span&gt;

&lt;span class="cp"&gt;## Use ipv6 if available&lt;/span&gt;
&lt;span class="cp"&gt;#include_shell &amp;quot;/usr/share/lighttpd/use-ipv6.pl&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;compress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;dir&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/cache/lighttpd/compress/&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;compress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filetype&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;application/x-javascript&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;text/css&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;text/html&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;text/plain&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;include_shell&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/usr/share/lighttpd/create-mime.assign.pl&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;include_shell&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/usr/share/lighttpd/include-conf-enabled.pl&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modules&lt;/span&gt;   &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;mod_fastcgi&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modules&lt;/span&gt;   &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;mod_rewrite&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;

 &lt;span class="n"&gt;fastcgi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/index.py&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;socket&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/tmp/fastcgi.socket&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;bin-path&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/www/index.py&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;&amp;quot;max-procs&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="s"&gt;&amp;quot;bin-environment&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="s"&gt;&amp;quot;REAL_SCRIPT_NAME&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
   &lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="s"&gt;&amp;quot;check-local&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;disable&amp;quot;&lt;/span&gt;
 &lt;span class="p"&gt;))&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt;

 &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rewrite&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;once&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="s"&gt;&amp;quot;^/favicon.ico$&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/static/favicon.ico&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="s"&gt;&amp;quot;^/static/(.*)$&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/static/$1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="s"&gt;&amp;quot;^/(.*)$&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/index.py/$1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Note the server.username and server.groupname.  This user/group will need proper permissions to your application files.  Study the config above.  Everything is simple and you should be able to change whatever settings that you need.&lt;/p&gt;
&lt;p&gt;Make sure you updated the server.document-root path to where the location that you are going to be placing your application in.  I just use /var/www for my applications.  This path will need to be updated in the fastcgi.server section along with your main python file for web.py.  I usually call my main application file "index.py" and the above config is setup to reflect that.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Now that we have lighttpd set up we can go into our /var/www directory and set up a sample web.py application ( if you do not already have a working app ).  We can just use this simple template.  Make a note of the shebang at the top of the file.  You will need to add that to your application file.  Just save the file as "index.py".&lt;/p&gt;
&lt;table class="codehilitetable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#!/usr/bin/env python&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;web&lt;/span&gt;

&lt;span class="n"&gt;urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;/(.*)&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Index&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;globals&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Hello World&amp;#39;&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;Hello World&amp;#39;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Make the file's owner www-data and make it executable.  After that restart lighttpd.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;chown&lt;/span&gt; &lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;
&lt;span class="n"&gt;chmod&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;
&lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="n"&gt;lighttpd&lt;/span&gt; &lt;span class="n"&gt;restart&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Go ahead and navigate to your site in your browser and you should see "Hello World".  If you do not, cat out the lighttpd error log at:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;lighttpd&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;That's it.  I hope this helps to save hours of your life, hours that I wasted trying to get web.py to run under Apache bullshit.  This is the way to go, lighttpd is fast, easy to config, and perfect for web.py.&lt;/p&gt;</summary></entry><entry><title>Objective - C LINQ</title><link href="http://crosbymichael.com/objective-c-linq.html" rel="alternate"></link><updated>2012-07-06T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-07-06:objective-c-linq.html</id><summary type="html">&lt;p&gt;I have been working on two iOS applications over the past few days and have been longing  for a LINQ like syntax in Objective-C.  LINQ is an amazing way to work with collections and it really adds a productivity boost to your coding.  Since Objective C does not have LINQ I thought I would code it myself.  Right now it only supports non-destructive operations but here it is.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;NSArray&lt;/span&gt; &lt;span class="nl"&gt;(IQuery)&lt;/span&gt;

&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;firstOrDefault:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;firstOrDefault&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;any:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;BOOL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;all:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;ofClass:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;where:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;select:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;skip:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;forEach:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is the current interface.  Most methods return BOOL or an NSArray so that the query operations can be chained together.&lt;/p&gt;
&lt;h2&gt;Code&lt;/h2&gt;
&lt;p&gt;What we want to do in this example is to take an array with strings and ints, filter it by class, then by strings starting with 'h'.  Here is how you do it:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;initWithObjects&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                          &lt;span class="s"&gt;@&amp;quot;hot dog&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="s"&gt;@&amp;quot;hamburger&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="s"&gt;@&amp;quot;pizza&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="s"&gt;@&amp;quot;taco&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="s"&gt;@&amp;quot;pop&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt; 
                           &lt;span class="n"&gt;ofClass&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
                           &lt;span class="nl"&gt;where:&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="n"&gt;characterAtIndex&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sc"&gt;&amp;#39;m&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                        &lt;span class="p"&gt;}];&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="n"&gt;forEach&lt;/span&gt;&lt;span class="o"&gt;:^&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}];&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So that is it.  The first IQuery method is "ofClass".  This takes a class and returns an NSArray of all the items in the array that are of that type(class).  It is the same idea as C#'s OfType&lt;T&gt;.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Next is "where".  This takes a predicate block that will return any item that satisfies the condition.  The syntax is not as clean as C# implementation but it's a start.&lt;/p&gt;
&lt;p&gt;Now after we have this filtered list with the chain of calls to "ofClass" and "where", we can now use the "forEach" method to enumerate the array, passing a block as the action to perform on each item.&lt;/p&gt;
&lt;h2&gt;Select&lt;/h2&gt;
&lt;p&gt;One last thing, if you want to select a certain property from an item in the array, you can use the "select" method to do so.  Here we select the price for each of the items.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;initWithObjects&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
                         &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;initWithNameAndPrice&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;hamburger&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSNumber&lt;/span&gt; &lt;span class="n"&gt;numberWithInt&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; 
                         &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;initWithNameAndPrice&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;hot dog&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSNumber&lt;/span&gt; &lt;span class="n"&gt;numberWithInt&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt;
                         &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;initWithNameAndPrice&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;pop&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NSNumber&lt;/span&gt; &lt;span class="n"&gt;numberWithInt&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]],&lt;/span&gt; 
                       &lt;span class="nb"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;prices&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}];&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will return an array of NSNumber items.  Nothing more.&lt;/p&gt;
&lt;p&gt;That's all for now.  If you are interested in the project I will have the binary to download soon.  Just email me with any comments or questions.&lt;/p&gt;</summary></entry><entry><title>Objective C Design Patterns - Factory</title><link href="http://crosbymichael.com/objective-c-design-patterns-factory.html" rel="alternate"></link><updated>2012-05-21T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-05-21:objective-c-design-patterns-factory.html</id><summary type="html">&lt;p&gt;Design patterns can help you solve complex coding problems with proven solutions.  This will be a mini series about different design pattern implementations in Objective - C.  I will be using a simple console app and updating the code on github with each new pattern.&lt;/p&gt;
&lt;h2&gt;Factory Pattern&lt;/h2&gt;
&lt;p&gt;This pattern provides a consistent way for object creation.  A factory provides one point of creation for one or a group of objects.  It also helps to not code to a concrete class.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When to use it?&lt;/strong&gt;
&lt;em&gt; Many objects need created that share an interface
&lt;/em&gt; Creation of complex objects
&lt;em&gt; Hide concrete classes and implementation from client code
&lt;/em&gt; Provide one point of entry for the creation of a class or group of classes&lt;/p&gt;
&lt;h2&gt;Static Factory&lt;/h2&gt;
&lt;p&gt;Most of the time I find myself using a static factory instead of an abstract factory.  A static factory is what we are going to focus on now.  In the console app I have an interface for logging text to the console.  There are two implementations for this interface.  One uses NSLog and the other uses fprintf().&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;@protocol&lt;/span&gt; &lt;span class="nc"&gt;CMConsoleOutput&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;NSObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;outputText&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;

&lt;span class="c1"&gt;//Class one&lt;/span&gt;
&lt;span class="cp"&gt;#import &amp;quot;CMPrintLog.h&amp;quot;&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;CMPrintLog&lt;/span&gt;

&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;outputText:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;fprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="n"&gt;UTF8String&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;

&lt;span class="c1"&gt;//Class two&lt;/span&gt;
&lt;span class="cp"&gt;#import &amp;quot;CMLogOutput.h&amp;quot;&lt;/span&gt;

&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;CMLogOutput&lt;/span&gt;

&lt;span class="k"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;outputText:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;NSLog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need a factory to choose what implementation the client should use without knowing the exact types of objects that are returned.  The client does not care what they get as long as it implements the interface.  It also helps to cut down the number of imports you have in your header files.  You no longer have to import 5 different class headers; you just have to import one factory header.  I went ahead an created the factory class file, typedef for identifying what type of output object the client wants, and a static method for creating/returning the object.&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;ouputTypes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;NoTimestamp&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;OutputTypes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;CMOutputFactory&lt;/span&gt; : &lt;span class="nc"&gt;NSObject&lt;/span&gt;

&lt;span class="k"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;CMConsoleOutput&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;create:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OutputTypes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now the only thing that we need to remember is to autorelease the objects that we create if you are not using ARC.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;CMOutputFactory&lt;/span&gt;

&lt;span class="k"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;CMConsoleOutput&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;create:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OutputTypes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nv"&gt;type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;Timestamp&lt;/span&gt;:
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;CMLogOutput&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;NoTimestamp&lt;/span&gt;:
            &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;CMPrintLog&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Finally we just need to use our new factory.&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;#import &amp;quot;CMOutputFactory.h&amp;quot;&lt;/span&gt;

&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="n"&gt;autoreleasepool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;withTimestamp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;CMOutputFactory&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Timestamp&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;noTimestamp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;CMOutputFactory&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;NoTimestamp&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;withTimestamp&lt;/span&gt; &lt;span class="n"&gt;outputText&lt;/span&gt;&lt;span class="p"&gt;:@&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Output without a time stamp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;noTimestamp&lt;/span&gt; &lt;span class="n"&gt;outputText&lt;/span&gt;&lt;span class="p"&gt;:@&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Without a time stamp&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Very easy, we only have one import and a consistent way to create objects.  It is very straight forward for people consuming an API's classes when you provide a factory for object creation.  They do not need to know the inner workings of your API, they just want a certain type of functionality and your factory provides that.  If you have any questions just ask.&lt;/p&gt;</summary></entry><entry><title>CodeAssistant</title><link href="http://crosbymichael.com/codeassistant.html" rel="alternate"></link><updated>2012-05-14T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-05-14:codeassistant.html</id><summary type="html">&lt;p&gt;My second application was published this week.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;img alt="CodeAssistant" src="images/codeassistant.png" /&gt;&lt;/p&gt;
&lt;p&gt;CodeAssistant is a simple tool to test code snippets in virtually any language on Windows.  It has been in development for four months and has been a great learning experience for domain driven development and .NET's WPF.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;The application is build on top of WPF using the MVVM design pattern.  I created a MVVM framework called MVVM Async that you can find it on &lt;a href="https://github.com/crosbymichael/mvvm-async"&gt;github&lt;/a&gt;.  WPF and MVVM are still in their infancy and there are some rough edges and verbose syntax that needs fixed ( command delegates anyone? ).  I like WPF and MVVM but I wish they would have just used the proved MVC tooling form ASP.NET MVC in WPF.  There are a lot of developers familiar with MVC, partials, and the such that would have been a good fit for WPF.  But no, they had to build another framework and you have to relearn everything all over again.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;This has been the first project where I have built it from the ground up with domain driven design and solid patterns.  Each of the layers in the application are completely decoupled from the others and are in their own projects.  The view/application layer only has a reference to the ViewModel project and the ViewModel project only references the Domain layer.&lt;/p&gt;
&lt;h2&gt;Future Projects&lt;/h2&gt;
&lt;p&gt;I don't have any plans for a future project.  I have been wanting to work on a game.  Desktop or iOS, I have not decided.  I may play with the XNA development toolkit before making a decision.  A friend of mine said that Corona is a good platform to build games on.  I have a few game ideas but don't know yet.&lt;/p&gt;
&lt;p&gt;If anyone has a game that they want to work on, it does not matter what platform, I can code in any language, let me know.&lt;br /&gt;
&lt;/p&gt;</summary></entry><entry><title>Setup Ruby on Rails with Nginx and Unicorn</title><link href="http://crosbymichael.com/setup-ruby-on-rails-with-nginx-and-unicorn.html" rel="alternate"></link><updated>2012-02-27T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-02-27:setup-ruby-on-rails-with-nginx-and-unicorn.html</id><summary type="html">&lt;p&gt;I lost an entire day of coding trying to get his to work.  We are all lucky that I am a developer and not a systems admin.&lt;/p&gt;
&lt;p&gt;However around 9pm I finally have it running.  I burned through a lot of screwed up VM's trying to get this to work but finally made it.  I tried Apache, Nginx, Passenger, and finally Nginx and Unicorn worked like it should.  I don't know what the issues were but the actual rails app was not being ran.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;I'll try to go through every single step I make, from installing Debian and the settings that I use to the installing and compiling gems, rails, and Nginx.  I will also try to make this into a bash script to automate much of the install incase you f something up.&lt;/p&gt;
&lt;h2&gt;Requirements&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Email&lt;/li&gt;
&lt;li&gt;Rails&lt;/li&gt;
&lt;li&gt;MySQL&lt;/li&gt;
&lt;li&gt;Nginx&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Installing Debian 6&lt;/h1&gt;
&lt;p&gt;I am going to use Debain 6 ( net install ) because it has the bare minimum.  Only what we install is what it is going to have.  You can also use Ubuntu Server 10.04 or 11.x with these commands.  They should work the same.  I have tested this on Ubuntu Server 10.04 so I know it works.&lt;/p&gt;
&lt;p&gt;Ok boot into your linux install and configure your languages.  I set the hostname as:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;nginx&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;rails&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;img alt="Hostname" src="images/hostname.png" /&gt;&lt;/p&gt;
&lt;p&gt;The domain name to my url.&lt;/p&gt;
&lt;p&gt;Create your root and user passwords then for the disk partitioning just use the entire hard drive.  I also choose to keep all system and user files on one partition.&lt;/p&gt;
&lt;p&gt;Pick one of the package servers for debian.  It does not matter.&lt;/p&gt;
&lt;h2&gt;Install Package Selection&lt;/h2&gt;
&lt;p&gt;Here is my package selection for install.  We do not want a webserver or anything else.  I usually install the Mail server so that I don't have to mess with it but since we are going to use Nginx and install mySQL manually all we want is the SSH and Mail servers installed right now.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Packages" src="images/default-packages.png" /&gt;&lt;/p&gt;
&lt;p&gt;Install GRUB boot load and restart.&lt;/p&gt;
&lt;p&gt;Once your booted up, login really quick and run ifconfig to get the ip of the machine.  We will want to SSH into it so we can use our desktop Terminal ( copy and paste ) instead of using a keyboard on the actual machine or through the VM.&lt;/p&gt;
&lt;p&gt;Once you have the IP login as root via ssh.  Lets nano and paste in the setup script from below.  Chmod 755 it and let it rip.&lt;/p&gt;
&lt;h1&gt;Application Install&lt;/h1&gt;
&lt;p&gt;&lt;img alt="Setup Script" src="images/bash-setup.png" /&gt;&lt;/p&gt;
&lt;p&gt;So now we need to get our base applications installed on the server.  If you are using Ubutnu Server you will want to run:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;sudo&lt;/span&gt; &lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt;
&lt;span class="n"&gt;sudo&lt;/span&gt; &lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;upgrade&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Also from here on out I will not place sudo infront of all the commands that need it.  If you don't konw when you need to run the command with sudo or when not to, just go home.  Don't waste my time emailing why when you run a command on Ubuntu it says "permission denied."&lt;/p&gt;
&lt;p&gt;Here is the script that I will run to get the base apps installed from apt.&lt;/p&gt;
&lt;table class="codehilitetable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

apt-get update
apt-get upgrade -y

apt-get install wget build-essential ruby1.8 ruby1.8-dev irb1.8 rdoc1.8 zlib1g-dev libopenssl-ruby1.8 libopenssl-ruby libzlib-ruby libssl-dev libpcre3-dev -y

apt-get install vim -y
apt-get install git-core -y
apt-get install screen -y
apt-get install ctags -y
apt-get install curl -y
apt-get install libsqlite3-dev -y
apt-get install libcurl4-openssl-dev -y
apt-get install libssl-dev -y
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;If you notice I did not install mySQL in here because it will ask for the root passwords and I wanted this base install script to be fully automatic.&lt;/p&gt;
&lt;p&gt;Now that that is done lets go ahead and isntall mySQL&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;mysql&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="n"&gt;libmysqlclient16&lt;/span&gt; &lt;span class="n"&gt;libmysqlclient&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="n"&gt;mysql&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="n"&gt;mysql&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;common&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;When asked, pick a good mySQL root password.&lt;/p&gt;
&lt;h3&gt;Gems&lt;/h3&gt;
&lt;p&gt;Now that we have everything from apt installed lets get rubygems installed.  I don't want to install gems via apt so we are going to grab the tarbal and install that way.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;wget&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//production.cf.rubygems.org/rubygems/rubygems-1.5.1.tgz&lt;/span&gt;
&lt;span class="n"&gt;tar&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;zxvf&lt;/span&gt; &lt;span class="n"&gt;rubygems&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.5.1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tgz&lt;/span&gt;
&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;rubygems&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;1.5.1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;ruby&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rb&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will install gems in:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;gem1&lt;/span&gt;&lt;span class="mf"&gt;.8&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Logout of your bash session and them back in and you should be able to type gem1.8 to execute some commands.&lt;/p&gt;
&lt;p&gt;Create another bash setup script for all these gem installs.  They will take a while so it's easier to just let it roll.  I chose not to install the rdoc and ri because this is the server and we don't need it.&lt;/p&gt;
&lt;table class="codehilitetable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt;1
2
3
4
5
6
7
8&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
gem1.8 install fastthread --no-rdoc --no-ri 
gem1.8 install rake rack --no-rdoc --no-ri 
gem1.8 install rails --no-rdoc --no-ri 
gem1.8 install mysql --no-rdoc --no-ri 
gem1.8 install unicorn --no-rdoc --no-ri 
gem1.8 install sqlite3-ruby --no-rdoc --no-ri 
gem1.8 install execjs --no-rdoc --no-ri
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;h2&gt;Javascript engine&lt;/h2&gt;
&lt;p&gt;Rails needs a javascript engine to run.  Pick your favorite one and install it.  I will isntall node.js for this.  I will not use apt or gems to install node.js but will grab the latest stable release from github and compile it.&lt;/p&gt;
&lt;p&gt;Lets grab the latest stable release from github and download it in our /opt/ dir.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;wget&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;O&lt;/span&gt; &lt;span class="n"&gt;nodejs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zip&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//github.com/joyent/node/zipball/v0.6.11&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;One thing that I forgot to install is zip so that we can unzip this file.  So quickly install&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;zip&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;then unzip node.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;unzip&lt;/span&gt; &lt;span class="n"&gt;nodejs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zip&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Lets cd into joyent-node-* folder and start building it&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;configure&lt;/span&gt;
&lt;span class="n"&gt;make&lt;/span&gt;
&lt;span class="n"&gt;make&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Also make sure that you install coffee-rails and jquery-rails.  I got this error when trying to make the test app below.  It may be a good idea to make a test app as root so bundler to handle these for us.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;gem1&lt;/span&gt;&lt;span class="mf"&gt;.8&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;coffee&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;rdoc&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ri&lt;/span&gt;
&lt;span class="n"&gt;gem1&lt;/span&gt;&lt;span class="mf"&gt;.8&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;jquery&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;rdoc&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ri&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;img alt="Error" src="images/js-engine.png" /&gt;&lt;/p&gt;
&lt;h2&gt;Testing rails&lt;/h2&gt;
&lt;p&gt;Now that we have everything setup lets login to our user account ( not root ) and create a sample rails app and run it with the builtin WEBrick server to make sure that rails works on our server.&lt;/p&gt;
&lt;p&gt;In your home dir:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;testRails&lt;/span&gt;
&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="n"&gt;testRails&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;img alt="Webrick" src="images/webrick.png" /&gt;&lt;/p&gt;
&lt;p&gt;Your server should start up fine so navigate to it's ip and make sure that rails is running and the environment links displays information about your setup.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Test App" src="images/test-page.png" /&gt;&lt;/p&gt;
&lt;p&gt;Hopefully your rails test app looks like this in the browser.  If not then don't even try to go forward.  Get the WEBrick server working first and then move on.&lt;/p&gt;
&lt;h1&gt;Nginx&lt;/h1&gt;
&lt;p&gt;We will go ahead and install nginx via the apt repo.  We first need to add the key to the resposity so download it then add it.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;wget&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//nginx.org/keys/nginx_signing.key&lt;/span&gt;
&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;nginx_signing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now add the repos to your /etc/apt/sources.list&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;deb&lt;/span&gt; &lt;span class="s"&gt;http://nginx.org/packages/debian/&lt;/span&gt; &lt;span class="kp"&gt;squeeze&lt;/span&gt; &lt;span class="kp"&gt;nginx&lt;/span&gt;
&lt;span class="k"&gt;deb-src&lt;/span&gt; &lt;span class="s"&gt;http://nginx.org/packages/debian/&lt;/span&gt; &lt;span class="kp"&gt;squeeze&lt;/span&gt; &lt;span class="kp"&gt;nginx&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Then&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt;
&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;nginx&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;It's a really quick install.  After the install is finished you can test to make sure that it works by going to your machines ip address.  You should see the welcome to nginx text.&lt;/p&gt;
&lt;h2&gt;User group and dir&lt;/h2&gt;
&lt;p&gt;Now lets setup a new directory that nginx and our rails apps will be in.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt;
&lt;span class="n"&gt;mkdir&lt;/span&gt; &lt;span class="n"&gt;www&lt;/span&gt;
&lt;span class="n"&gt;usermod&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;G&lt;/span&gt; &lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;nginx&lt;/span&gt;
&lt;span class="n"&gt;usermod&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;G&lt;/span&gt; &lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;YOURUSERNAME&lt;/span&gt;

&lt;span class="n"&gt;chgrp&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt; &lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;chmod&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;R&lt;/span&gt; &lt;span class="mi"&gt;775&lt;/span&gt; &lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now our user and nginx should be part of www-data group and own /var/www&lt;/p&gt;
&lt;h2&gt;Nginx Config&lt;/h2&gt;
&lt;p&gt;Now lets grab the config file form the unicorn repo and place it in the nginx config directory overwriting the default config.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;etc&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;nginx&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;cp&lt;/span&gt; &lt;span class="n"&gt;nginx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt; &lt;span class="n"&gt;nginx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;old&lt;/span&gt;
&lt;span class="n"&gt;rm&lt;/span&gt; &lt;span class="n"&gt;nginx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;
&lt;span class="n"&gt;wget&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//raw.github.com/defunkt/unicorn/master/examples/nginx.conf&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need to setup our paths in the nginx.conf file.&lt;/p&gt;
&lt;p&gt;Uncomment this line:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="n"&gt;deferred&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Linux&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Change the root paths to there our apps public dir is:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;testapp&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Save and restart nginx&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;etc&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;nginx&lt;/span&gt; &lt;span class="n"&gt;restart&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now lets get unicorn setup with init.d and make a test app in the www dir via our other user.&lt;/p&gt;
&lt;h2&gt;Create the test app&lt;/h2&gt;
&lt;p&gt;Login as your normal user and create a new app called "testapp" in /var/www&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;testapp&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Everything should go smooth if you have things setup right.  You don't need root or sudo to make the test app in var/www because we added ourself and nginx to the group that owns the folder.  Lets cd into the test app's config dir and download the unicorn config file.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="n"&gt;testapp&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;wget&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;O&lt;/span&gt; &lt;span class="n"&gt;unicorn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rb&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//raw.github.com/defunkt/unicorn/master/examples/unicorn.conf.rb&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Vim or nano into it and setup the paths to your app.  Here is my config:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;# Sample verbose configuration file for Unicorn (not Rack)&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# This configuration file documents many features of Unicorn&lt;/span&gt;
&lt;span class="c"&gt;# that may not be needed for some applications. See&lt;/span&gt;
&lt;span class="c"&gt;# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb&lt;/span&gt;
&lt;span class="c"&gt;# for a much simpler configuration file.&lt;/span&gt;
&lt;span class="c"&gt;#&lt;/span&gt;
&lt;span class="c"&gt;# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete&lt;/span&gt;
&lt;span class="c"&gt;# documentation.&lt;/span&gt;

&lt;span class="c"&gt;# Use at least one worker per core if you&amp;#39;re on a dedicated server,&lt;/span&gt;
&lt;span class="c"&gt;# more will usually help for _short_ waits on databases/caches.&lt;/span&gt;
&lt;span class="n"&gt;worker_processes&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;

&lt;span class="c"&gt;# Since Unicorn is never exposed to outside clients, it does not need to&lt;/span&gt;
&lt;span class="c"&gt;# run on the standard HTTP port (80), there is no reason to start Unicorn&lt;/span&gt;
&lt;span class="c"&gt;# as root unless it&amp;#39;s from system init scripts.&lt;/span&gt;
&lt;span class="c"&gt;# If running the master process as root and the workers as an unprivileged&lt;/span&gt;
&lt;span class="c"&gt;# user, do this to switch euid/egid in the workers (also chowns logs):&lt;/span&gt;
&lt;span class="c"&gt;# user &amp;quot;unprivileged_user&amp;quot;, &amp;quot;unprivileged_group&amp;quot;&lt;/span&gt;

&lt;span class="c"&gt;# Help ensure your application will always spawn in the symlinked&lt;/span&gt;
&lt;span class="c"&gt;# &amp;quot;current&amp;quot; directory that Capistrano sets up.&lt;/span&gt;
&lt;span class="n"&gt;working_directory&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/www/testapp&amp;quot;&lt;/span&gt; &lt;span class="c"&gt;# available in 0.94.0+&lt;/span&gt;

&lt;span class="c"&gt;# listen on both a Unix domain socket and a TCP port,&lt;/span&gt;
&lt;span class="c"&gt;# we use a shorter backlog for quicker failover when busy&lt;/span&gt;
&lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/tmp/.sock&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;backlog&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;
&lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;tcp_nopush&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;

&lt;span class="c"&gt;# nuke workers after 30 seconds instead of 60 seconds (the default)&lt;/span&gt;
&lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;

&lt;span class="c"&gt;# feel free to point this anywhere accessible on the filesystem&lt;/span&gt;
&lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/www/testapp/shared/pids/unicorn.pid&amp;quot;&lt;/span&gt;

&lt;span class="c"&gt;# By default, the Unicorn logger will write to stderr.&lt;/span&gt;
&lt;span class="c"&gt;# Additionally, ome applications/frameworks log to stderr or stdout,&lt;/span&gt;
&lt;span class="c"&gt;# so prevent them from going to /dev/null when daemonized here:&lt;/span&gt;
&lt;span class="n"&gt;stderr_path&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/www/testapp/shared/log/unicorn.stderr.log&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;stdout_path&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/var/www/testapp/shared/log/unicorn.stdout.log&amp;quot;&lt;/span&gt;

&lt;span class="c"&gt;# combine Ruby 2.0.0dev or REE with &amp;quot;preload_app true&amp;quot; for memory savings&lt;/span&gt;
&lt;span class="c"&gt;# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow&lt;/span&gt;
&lt;span class="n"&gt;preload_app&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;span class="n"&gt;GC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;respond_to&lt;/span&gt;?&lt;span class="p"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;copy_on_write_friendly&lt;/span&gt;&lt;span class="p"&gt;=)&lt;/span&gt; &lt;span class="nb"&gt;and&lt;/span&gt;
  &lt;span class="n"&gt;GC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy_on_write_friendly&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;

&lt;span class="n"&gt;before_fork&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="c"&gt;# the following is highly recomended for Rails + &amp;quot;preload_app true&amp;quot;&lt;/span&gt;
  &lt;span class="c"&gt;# as there&amp;#39;s no need for the master process to hold a connection&lt;/span&gt;
  &lt;span class="n"&gt;defined&lt;/span&gt;?&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;and&lt;/span&gt;
    &lt;span class="n"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disconnect&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;

  &lt;span class="c"&gt;# The following is only recommended for memory/DB-constrained&lt;/span&gt;
  &lt;span class="c"&gt;# installations.  It is not needed if your system can house&lt;/span&gt;
  &lt;span class="c"&gt;# twice as many worker_processes as you have configured.&lt;/span&gt;
  &lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="c"&gt;# # This allows a new master process to incrementally&lt;/span&gt;
  &lt;span class="c"&gt;# # phase out the old master process with SIGTTOU to avoid a&lt;/span&gt;
  &lt;span class="c"&gt;# # thundering herd (especially in the &amp;quot;preload_app false&amp;quot; case)&lt;/span&gt;
  &lt;span class="c"&gt;# # when doing a transparent upgrade.  The last worker spawned&lt;/span&gt;
  &lt;span class="c"&gt;# # will then kill off the old master process with a SIGQUIT.&lt;/span&gt;
  &lt;span class="c"&gt;# old_pid = &amp;quot;#{server.config[:pid]}.oldbin&amp;quot;&lt;/span&gt;
  &lt;span class="c"&gt;# if old_pid != server.pid&lt;/span&gt;
  &lt;span class="c"&gt;#   begin&lt;/span&gt;
  &lt;span class="c"&gt;#     sig = (worker.nr + 1) &amp;gt;= server.worker_processes ? :QUIT : :TTOU&lt;/span&gt;
  &lt;span class="c"&gt;#     Process.kill(sig, File.read(old_pid).to_i)&lt;/span&gt;
  &lt;span class="c"&gt;#   rescue Errno::ENOENT, Errno::ESRCH&lt;/span&gt;
  &lt;span class="c"&gt;#   end&lt;/span&gt;
  &lt;span class="c"&gt;# end&lt;/span&gt;
  &lt;span class="c"&gt;#&lt;/span&gt;
  &lt;span class="c"&gt;# Throttle the master from forking too quickly by sleeping.  Due&lt;/span&gt;
  &lt;span class="c"&gt;# to the implementation of standard Unix signal handlers, this&lt;/span&gt;
  &lt;span class="c"&gt;# helps (but does not completely) prevent identical, repeated signals&lt;/span&gt;
  &lt;span class="c"&gt;# from being lost when the receiving process is busy.&lt;/span&gt;
  &lt;span class="c"&gt;# sleep 1&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;after_fork&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;worker&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="c"&gt;# per-process listener ports for debugging/admin/migrations&lt;/span&gt;
  &lt;span class="c"&gt;# addr = &amp;quot;127.0.0.1:#{9293 + worker.nr}&amp;quot;&lt;/span&gt;
  &lt;span class="c"&gt;# server.listen(addr, :tries =&amp;gt; -1, :delay =&amp;gt; 5, :tcp_nopush =&amp;gt; true)&lt;/span&gt;

  &lt;span class="c"&gt;# the following is *required* for Rails + &amp;quot;preload_app true&amp;quot;,&lt;/span&gt;
  &lt;span class="n"&gt;defined&lt;/span&gt;?&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;and&lt;/span&gt;
    &lt;span class="n"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;establish_connection&lt;/span&gt;

  &lt;span class="c"&gt;# if preload_app is true, then you may also want to check and&lt;/span&gt;
  &lt;span class="c"&gt;# restart any other shared sockets/descriptors such as Memcached,&lt;/span&gt;
  &lt;span class="c"&gt;# and Redis.  TokyoCabinet file handles are safe to reuse&lt;/span&gt;
  &lt;span class="c"&gt;# between any number of forked children (assuming your kernel&lt;/span&gt;
  &lt;span class="c"&gt;# correctly implements pread()/pwrite() system calls)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;They are easily found and just point them to your app dir.&lt;/p&gt;
&lt;p&gt;![Unicorn Config][8]&lt;/p&gt;
&lt;p&gt;Now lets run unicorn and test out our app.  Make two new folders in your app before login back in as root and running unicorn.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;shared&lt;/span&gt;
&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="n"&gt;shared&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;mkdir&lt;/span&gt; &lt;span class="n"&gt;pids&lt;/span&gt;
&lt;span class="n"&gt;mkdir&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Login as root to run unicorn:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;unicorn_rails&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;www&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;testapp&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;unicorn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rb&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now go to your server ip in your browser and you should see the same thing with the ability to click the envirionment link with data displayed.  If you do no see any data then unicorn is not running or setup correctly.  If you do see it then we are done.  It's working and ready to go.  You may want to add unicorn to an init.d script so that it starts with your server.&lt;/p&gt;
&lt;h2&gt;Unicorn init.d&lt;/h2&gt;
&lt;p&gt;As root:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;etc&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;vim&lt;/span&gt; &lt;span class="n"&gt;unicorn&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Here is the init.d script that I am using so just paste it into the file.&lt;/p&gt;
&lt;table class="codehilitetable"&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;div class="linenodiv"&gt;&lt;pre&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50&lt;/pre&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c"&gt;#! /bin/sh&lt;/span&gt;

&lt;span class="c"&gt;# File: /etc/init.d/unicorn&lt;/span&gt;

&lt;span class="c"&gt;### BEGIN INIT INFO&lt;/span&gt;
&lt;span class="c"&gt;# Provides:          unicorn&lt;/span&gt;
&lt;span class="c"&gt;# Required-Start:    $local_fs $remote_fs $network $syslog&lt;/span&gt;
&lt;span class="c"&gt;# Required-Stop:     $local_fs $remote_fs $network $syslog&lt;/span&gt;
&lt;span class="c"&gt;# Default-Start:     2 3 4 5&lt;/span&gt;
&lt;span class="c"&gt;# Default-Stop:      0 1 6&lt;/span&gt;
&lt;span class="c"&gt;# Short-Description: starts the unicorn web server&lt;/span&gt;
&lt;span class="c"&gt;# Description:       starts unicorn&lt;/span&gt;
&lt;span class="c"&gt;### END INIT INFO&lt;/span&gt;

&lt;span class="nv"&gt;DAEMON&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/bin/unicorn
&lt;span class="nv"&gt;DAEMON_OPTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;-c /var/www/testapp/config/unicorn.rb -D&amp;quot;&lt;/span&gt;
&lt;span class="nv"&gt;NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;unicorn
&lt;span class="nv"&gt;DESC&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Unicorn app for testapp&amp;quot;&lt;/span&gt;
&lt;span class="nv"&gt;PID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/var/web/testapp/shared/pid/unicorn.pid

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;$1&amp;quot;&lt;/span&gt; in
  start&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s2"&gt;&amp;quot;Starting $DESC: &amp;quot;&lt;/span&gt;
    &lt;span class="nv"&gt;$DAEMON&lt;/span&gt; &lt;span class="nv"&gt;$DAEMON_OPTS&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;$NAME.&amp;quot;&lt;/span&gt;
    ;;
  stop&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s2"&gt;&amp;quot;Stopping $DESC: &amp;quot;&lt;/span&gt;
        &lt;span class="nb"&gt;kill&lt;/span&gt; -QUIT &lt;span class="sb"&gt;`&lt;/span&gt;cat &lt;span class="nv"&gt;$PID&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;$NAME.&amp;quot;&lt;/span&gt;
    ;;
  restart&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s2"&gt;&amp;quot;Restarting $DESC: &amp;quot;&lt;/span&gt;
        &lt;span class="nb"&gt;kill&lt;/span&gt; -QUIT &lt;span class="sb"&gt;`&lt;/span&gt;cat &lt;span class="nv"&gt;$PID&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
    sleep 1
    &lt;span class="nv"&gt;$DAEMON&lt;/span&gt; &lt;span class="nv"&gt;$DAEMON_OPTS&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;$NAME.&amp;quot;&lt;/span&gt;
    ;;
  reload&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; -n &lt;span class="s2"&gt;&amp;quot;Reloading $DESC configuration: &amp;quot;&lt;/span&gt;
        &lt;span class="nb"&gt;kill&lt;/span&gt; -HUP &lt;span class="sb"&gt;`&lt;/span&gt;cat &lt;span class="nv"&gt;$PID&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
        &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;$NAME.&amp;quot;&lt;/span&gt;
        ;;
  *&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Usage: $NAME {start|stop|restart|reload}&amp;quot;&lt;/span&gt; &amp;gt;&amp;amp;2
    &lt;span class="nb"&gt;exit &lt;/span&gt;1
    ;;
&lt;span class="k"&gt;esac&lt;/span&gt;

&lt;span class="nb"&gt;exit &lt;/span&gt;0
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Edit to your correct paths then:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;chmod&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;unicorn&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;sbin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;rc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;unicorn&lt;/span&gt; &lt;span class="n"&gt;defaults&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Reboot and see if nginx and unicorn are successfully servering your app.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Everything should work on the defult page via port 80.  If you do not see images or the environment info that means unicorn is not running.  Check the log files that we setup in the config ( the paths ) and see what is happening.  I hope this tutorial works for everyone.  I had a lot of trouble setting this up so hopefully you will not repeat my pain.&lt;/p&gt;</summary></entry><entry><title>Multithreading in C#</title><link href="http://crosbymichael.com/multithreading-in-c.html" rel="alternate"></link><updated>2012-01-28T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-01-28:multithreading-in-c.html</id><summary type="html">&lt;p&gt;From my point of view the reason why we create multithreaded applications is so the user experience does not suffer.  We have operations that need completed but running these on the main thread will block the UI and piss off our users.&lt;/p&gt;
&lt;p&gt;Most of the operations have some similarities.  Atleast for myself, the most common use case is the user requests information, we process it, then display the results back.  That is the most common use that I use background threads for.  When we display these results back, we need to write code that is executed on the Main thread to update the UI.&lt;/p&gt;
&lt;p&gt;Working with delegates, callbacks, and IAsyncResults suck.  They cause you to write more methods and change your code.  There should be an easy way to write code normally and have all the benefits of async programming.  That is what I try to accomplish in my WPF MVVM-Async framework.  It allows you to write code normally and create multithreading applications with lambdas.&lt;/p&gt;
&lt;h2&gt;MVVM-Async&lt;/h2&gt;
&lt;p&gt;So first thing that you need to do to take advantage of the framework is to make your ViewModel inherit from ViewModelBase and pass it a &lt;T&gt; type for the model.  By doing this you automatically get access to the Model object.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;EditorViewModel&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ViewModelBase&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;EditorModel&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;...&lt;/span&gt;
&lt;span class="nx"&gt;this.Model.EditorModelProperties...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Async&lt;/h2&gt;
&lt;p&gt;Now just code and when you want to dispatch a process in a background thread call DispatchAsync() and pass it a lambda.&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;DispatchAsync&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;bla&lt;/span&gt; &lt;span class="n"&gt;bla&lt;/span&gt; &lt;span class="n"&gt;bla&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The framework will automatically spawn a new thread and execute whatever you placed inside the lambda on that thread.  You can even call another method and pass any variables to it.  By using lambdas you stay in the scope of the method calling DispatchAsync.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;DispatchAsync&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;SomeOtherFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Updating the UI&lt;/h2&gt;
&lt;p&gt;So now we have this lambda executing on a background thread and processing data.  We need to take that data and update our UI.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;DispatchAsync&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetFullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;MyViewsTextBoxDataSource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is very simple but you get the point.  If we were to try to set one of our view's controls from a background thread we would throw an exception because you can only set Controls on the thread that created it.  So without using callbacks we can do this easily with the framework.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;DispatchAsync&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetFullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;DispatchMainAsync&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;MyViewsTextBoxDataSource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;How hard was that?  If we want to dispatch a lambda to the main thread that updates the UI, we can write it inline, with all our variables in scope, and have that code executed on the main thread.  No exceptions.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Easy, simple, and you don't have to have callbacks or other crap to mess with.  If you do want a callback when a thread has finished executing the framework does support this.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;DispatchAsyncWithCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;DispatchAsyncWithCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;whereTo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The whereTo tells the framework on what thread do you want the callback executed one.  Call me back on a background thread or call me back on the main thread. &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;DispatchAsyncWithCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;doSomeStuff&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;doSomeStuffOnTheCallback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;DispatchOn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//Dispatch the callback on the main thread.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;It's all done inline.&lt;/p&gt;
&lt;h2&gt;Queues&lt;/h2&gt;
&lt;p&gt;There is also support of Queues.  You can load up a DispatchQueue with tasks and invoke the entire queue.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AddQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DispatchQueue&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;InvokeQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ProcessAllQueues&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Timed Execution&lt;/h2&gt;
&lt;p&gt;You can also tell the framework when to dispatch a thread.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;DispatchIn&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;somethingToDo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;5min&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or at a specific time.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="n"&gt;someTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;DispatchAt&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;somethingToDo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;someTime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Final&lt;/h2&gt;
&lt;p&gt;I am still working on this and will be releasing the code soon after CodeAssistant is complete.  CodeAssistant is using this framework.&lt;/p&gt;</summary></entry><entry><title>tCal - iCal events in terminal</title><link href="http://crosbymichael.com/tcal-ical-events-in-terminal.html" rel="alternate"></link><updated>2012-01-14T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2012-01-14:tcal-ical-events-in-terminal.html</id><summary type="html">&lt;p&gt;I just puked up some old code for a little app that displays your iCal events in the terminal.  I have it as one of my startup scripts so when I start a new bash session I see my events for the next 7 days.  It's a very simple but cool app.  I am not proud of the design but I just cleaned up the code tonight and threw it on &lt;a href="http://goo.gl/CNyGZ"&gt;tCal on Github&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Usage&lt;/h2&gt;
&lt;p&gt;It is very simple to use.  Just throw it in a folder that is in your PATH and type tCal to see your events for the next 5 days (default).  if you want more or less days then just:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;tCal&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;There is no config to setup with your calendars, it just works.&lt;/p&gt;
&lt;p&gt;Here is a little taste for the code.  Head over to Github for it all.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;autoreleasepool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;CMCalendar&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;calendar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;CMCalendar&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;CMArgumentParser&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;CMArgumentParser&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;NSInteger&lt;/span&gt; &lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;@try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="n"&gt;NSString&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="n"&gt;argumentForName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;@&amp;quot;-d&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;integerValue&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;@catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NSException&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DEFAULTDAYS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;NSArray&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;calendar&lt;/span&gt; &lt;span class="n"&gt;getEventsForDays&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ITerminalOutput&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you have any ideas just send them to me.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://dev.crosbymichael.com/downloads/?file=tCal-1.0.zip"&gt;Download Binary&lt;/a&gt;&lt;/p&gt;</summary></entry><entry><title>Bash Informational Startup</title><link href="http://crosbymichael.com/bash-informational-startup.html" rel="alternate"></link><updated>2011-11-11T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-11-11:bash-informational-startup.html</id><summary type="html">&lt;p&gt;If you use Terminal on your Mac like I do.  It is the first application that is run at starup and the last to close.  When you start Terminal in a new window or tab that is a great time to review your computer stats, iCal events, and disk sizes.  I saw a lifehacker post abut a menu bar app that shows all the disks that are connected to your Mac and their size and free space.  I also use todotxt for my todo lists and because it does not support dates for your tasks I keep my dates for events in iCal.  I wanted to see all of this whenever I startup a new bash session so here is how I did it.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Bash Startup" src="images/bash-start-info.png" /&gt;&lt;/p&gt;
&lt;p&gt;So this is what I see when I start a new bash session.  All my information that I want is there plain text and easy to read.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;There is my computers uptime at the very top with load averages.&lt;/p&gt;
&lt;p&gt;Next is all the disk space information of my internal hard drive and external USB hard drives.  This is a straight forward easy command.&lt;/p&gt;
&lt;p&gt;Next up is my iCal events for today and tommorrow.  You can easily set how many days forward you want.  This is done by a simple Objective - C command line app that I wrote.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Last is todotxt.  All my tasks.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;So here is the last part of my bash_profile that makes this all happen.  You can download all my app that displays your iCal events from my github of dotfiles (it's in the bin dir).  You will also find all kinds of awesome stuff in it. &lt;/p&gt;
&lt;h2&gt;End of my .bash_profile&lt;/h2&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;-----------------------------------------------&amp;quot;&lt;/span&gt;
&lt;span class="cp"&gt;#Display uptime&lt;/span&gt;
&lt;span class="n"&gt;uptime&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;-----------------------------------------------&amp;quot;&lt;/span&gt;
&lt;span class="cp"&gt;#Display disk space usage&lt;/span&gt;
&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;hl&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;-----------------------------------------------&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Up coming events:&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;-----------------&amp;quot;&lt;/span&gt;
&lt;span class="cp"&gt;#Call to my cmd line app to show iCal events&lt;/span&gt;
&lt;span class="n"&gt;CommandLineAssistant&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;-----------------------------------------------&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;Todo Tasks:&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;-----------&amp;quot;&lt;/span&gt;
&lt;span class="cp"&gt;#List my todos &lt;/span&gt;
&lt;span class="n"&gt;todo&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;
&lt;span class="n"&gt;echo&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</summary></entry><entry><title>Compile Vim for Python</title><link href="http://crosbymichael.com/compile-vim-for-python.html" rel="alternate"></link><updated>2011-10-13T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-10-13:compile-vim-for-python.html</id><summary type="html">&lt;h2&gt;Update&lt;/h2&gt;
&lt;p&gt;I just added the newest MacVim compiled for python using the 10.7 Lion SDK.  You can downloading it below.&lt;/p&gt;
&lt;p&gt;If you want to use the Python auto complete in vim then you have to compile vim for python on your system. If you are running OSX I have just compiled MacVim for Snow Leopard and I am working on a compiled Lion version but the SL one works fine.  Then you can setup an alias for vim so that it points to the binary in the MacVim app so you can still use vim from the command line and as the MacVim app.&lt;/p&gt;
&lt;p&gt;It works really well and is fairly easy this way.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://dev.crosbymichael.com/downloads/?file=MacVimSL.tbz"&gt;MacVim with Python SL&lt;/a&gt;
&lt;a href="http://dev.crosbymichael.com/downloads/?file=MacVim-Lion.tar.bz2"&gt;MacVim with Python Lion&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;First download the already compiled MacVim.app from the link above and extract it to your Applications folder.&lt;/p&gt;
&lt;p&gt;Then download:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.vim.org/scripts/download_script.php?src_id=10872"&gt;pythoncomplete.vim&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;vim files and put it in your ~/.vim/autoload/ folder.  It should be named:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;pythoncomplete&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vim&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you need, you can see my .dotfiles on github that already has my vimrc and vim dir in it.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/crosbymichael/.dotfiles"&gt;My dotfiles&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Then just add:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;filetype&lt;/span&gt; &lt;span class="n"&gt;plugin&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Last thing if you want to run this version form terminal just add:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="n"&gt;vim&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;Applications&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;MacVim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;Contents&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;MacOS&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;Vim&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;to your ~/.bash_profile&lt;/p&gt;
&lt;p&gt;You can see all these settings and more in my dotfiles.&lt;/p&gt;
&lt;p&gt;All done.  To use the auto complete just hit control+x then still holding down the control key it o&lt;/p&gt;</summary></entry><entry><title>Python talking to Arduino</title><link href="http://crosbymichael.com/python-talking-to-arduino.html" rel="alternate"></link><updated>2011-10-08T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-10-08:python-talking-to-arduino.html</id><summary type="html">&lt;p&gt;I just setup my Arduino with an LCD display.  You can follow the tutorial &lt;a href="http://arduino.cc/en/Tutorial/LiquidCrystalSerial"&gt;here&lt;/a&gt; to get the LCD and board wired up.  We will also be using the sample code from that tutorial.&lt;/p&gt;
&lt;h2&gt;The Problem&lt;/h2&gt;
&lt;p&gt;The issue that I ran into was displaying messages from the Arduino IDE's SerialMonitor worked fine but when I was trying to display a message that I sent via python, it was not showing on the screen.  After some help I finally have it working so this is how you fix it.&lt;/p&gt;
&lt;h2&gt;Python Code&lt;/h2&gt;
&lt;p&gt;You will need to install pyserial so that you can talk to your board via USB.  It's simple if you have build tools installed so just:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;sudo&lt;/span&gt; &lt;span class="n"&gt;easy_install&lt;/span&gt; &lt;span class="n"&gt;pyserial&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now if we look at the code that does not work first you can see what you need to change.  We need to import serial and then setup the connection to the board.&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;serial&lt;/span&gt; 
&lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;pathtoyourboard&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9600&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You will need to look in your Arduino IDE for the path to your board and the 9600 is the rate that is definded in the Arduino code that we are loading onto it.  Now we need to write some data do the Arduino and close the connection.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;sometext&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If we load the code from the tutorial that I linked at the beginning of this post onto your board and then ran this python code, we would just see a blank LCD.  The probelm is that whenever the serial connection is first made, it takes the Arduino two seconds to get that port open and setup.  So for our python code to work we need to import the time module and add a sleep for atleast 2 seconds right after our port = serial.Serial statement.  Here is the what the final python code will look like with it working and the message changed.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;serial&lt;/span&gt;
&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;

&lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;tty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usbmodemfd141&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9600&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;My&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="n"&gt;michael&lt;/span&gt;&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So this code makes the connection and waits 2 seconds for the Arduino to get setup, then starts writing data to it.  Everything works find and it's good to go.  So in the future when you are working with Python and Arduino via USB, wait 2 seconds before sending data to it or it will not get it.&lt;br /&gt;
&lt;/p&gt;</summary></entry><entry><title>How to backup gmail the right way</title><link href="http://crosbymichael.com/how-to-backup-gmail-the-right-way.html" rel="alternate"></link><updated>2011-10-04T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-10-04:how-to-backup-gmail-the-right-way.html</id><summary type="html">&lt;p&gt;So like many people my life is stored in my gmail.  I trust google to keep my data safe but since so many things, from contacts to many different online accounts, are all registered under your gmail address it is important to have that data whenever you need it.  There are some services online that will backup your gmail but it is still stored on their servers.  I feel a lot better if it was stored locally on my computers so that I have a copy of my emails whenever I need them.  So this is how you do it.&lt;/p&gt;
&lt;h2&gt;Got Your Back&lt;/h2&gt;
&lt;p&gt;Got Your Back is a python app that will connect to your gmail via oAuth and suck down your messages in simple txt files.  They have an *.eml extension but you can open all these files with a text editor and see the contents of your emails.  It is fast and also does incremental backups.  So after the initial backup of your account, Got Your Back will only download the messages that it doe not have instead of downloading everything each time you do a backup.  I use a simple cron job to get all my new messages everyday, automatically without having to think about it.&lt;br /&gt;
&lt;/p&gt;
&lt;h2&gt;Installing&lt;/h2&gt;
&lt;p&gt;You can get to the download page of got your back &lt;a href="" title="http://code.google.com/p/got-your-back/downloads/list"&gt;here.&lt;/a&gt;  They have downloads for Unix and Windows systems.  I have an server setup running debian that handles all my account backups so this tutorial will focus on running gyb (got your back) on Unix systems.  So download the &lt;a href="" title="http://code.google.com/p/got-your-back/downloads/detail?name=gyb-alpha-17-python-src.zip&amp;amp;can=2&amp;amp;q="&gt;source package&lt;/a&gt; or use subversion to download the source onto your computer:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;svn&lt;/span&gt; &lt;span class="n"&gt;checkout&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//got-your-back.googlecode.com/svn/trunk/ got-your-back-read-only&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you download the zip extract it to whatever folder you want and cd into the directory.  Now because gyb uses oAuth to login to your account you will need to initially run this script on a computer with a desktop.  Because I use a server over ssh to download my backups I need to first get the oAuth token on a computer with a web browser first.&lt;/p&gt;
&lt;h2&gt;Getting oAuth Tokens&lt;/h2&gt;
&lt;p&gt;So now that we are cd into the got-your-back directory run this command to authenticate and get an estimate of the size and number of messages that need to be backed up.&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;gyb&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;youremail&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;gmail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;estimate&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So make sure that you input your correct email address.  If you are already logged into your email account on your computer and you are trying to backup a different account, you will need to logout first.  So gyb will tell you that it will launch a browser, so just hit Enter and you will see a gmail page asking if you want to authenticate gyb to your gmail.  Hit yes and then go back to the command line.  No hit Enter again if it is still waiting and wait for it to make an estimate of your gmail's size and number of messages.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;So now it should give you an estimate.  I had 28k messages to download at 800mb.  It does not download attachments.  If you do an ls now you will see a new file.  It will be a file with your email address and an extension of .cfg.  This file now holds your oAuth token so if you are going to run the backups on a server over ssh, you will have to copy this file over to your server for it to work.  So either zip, tar, or 7z the entire gyb directory with your email's config file in it and transfer it to your server.  If you are just going to run it on the same computer then you don't have to do anything yet.&lt;br /&gt;
&lt;/p&gt;
&lt;h2&gt;Inital backup&lt;/h2&gt;
&lt;p&gt;So now that you have the config file and gyb on whatever computer that you want to setup the automated backups on lets run the initial backup.  First lets make the gyb.py file runnable.&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;chmod&lt;/span&gt; &lt;span class="mi"&gt;755&lt;/span&gt; &lt;span class="n"&gt;gyb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So run this command to perform the initial backup.&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;gyb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;youremail&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;gmail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;backup&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will perform the initial backup so depending on how many messages your estimate was, this make take awhile.&lt;br /&gt;
&lt;/p&gt;
&lt;h2&gt;Cron job for everyday backups&lt;/h2&gt;
&lt;p&gt;So now that we have an initial backup we will need to do an incremental backup everyday for all of the new messages that we receive.  So lets add a cron job with crontab that will run gyb.py everyday.  So remember where that file is on your computer because we will need it. &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;crontab&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Then input this into your crontab file with the proper changes.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;michael&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;got&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;your&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;back&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;only&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;gyb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;youremail&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;gmail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;backup&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So what this does is make a backup at 3am everyday running gyb.py with your specified email.  Easy and done.  It will now do incremental backups of only your new messages and you will always be able to get that data because it is now backed up locally on your computer or server.&lt;br /&gt;
&lt;/p&gt;</summary></entry><entry><title>How to manage your website with git</title><link href="http://crosbymichael.com/how-to-manage-your-website-with-git.html" rel="alternate"></link><updated>2011-09-28T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-09-28:how-to-manage-your-website-with-git.html</id><summary type="html">&lt;p&gt;So this is much easier if you have a static site that is generated using pelican or jerkyll.  Both of these apps are great for generating blogs out of markdown or other simple text markup language.  I use pelican because it is built with python and that is what I develop in.&lt;br /&gt;
&lt;/p&gt;
&lt;h2&gt;Local&lt;/h2&gt;
&lt;p&gt;So on your local machine you will need your basic folder structure setup and if you are not already using git to manage your files, now is the time to start.  In terminal cd into your site dir and:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is create the local git repo that you need.  Go ahead and commit the files that you already have done and now we can work on the server.&lt;br /&gt;
&lt;/p&gt;
&lt;h2&gt;Server&lt;/h2&gt;
&lt;p&gt;Make sure that you have ssh access to your web server.  The first thing you need to do is cd into your users main directory, not your www or directory that hosts the files that are displayed to your users and we will make a new directory:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt; &lt;span class="n"&gt;cmsite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt;
&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="n"&gt;cmsite&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;bare&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will create a bare git repo that will accept the files when you do a push to it.  Now we will need to setup a hook that will checkout the latest push into the actual directory that hosts our content.  So now we need to add the hook the the hooks directory in cmsite.git in my case.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;vim&lt;/span&gt; &lt;span class="n"&gt;hooks&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;receive&lt;/span&gt;
&lt;span class="cp"&gt;#!/bin/sh&lt;/span&gt;
&lt;span class="n"&gt;GIT_WORK_TREE&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;crosbymichael&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;crosbymichael&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt; &lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;checkout&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Make sure that this file is executable so do a quick:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;chmod&lt;/span&gt; &lt;span class="mi"&gt;755&lt;/span&gt; &lt;span class="n"&gt;hooks&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;receive&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;h2&gt;Final Steps&lt;/h2&gt;
&lt;p&gt;Now we just need to setup our new remote repo on our local machine.  So cd into your local git repo and type:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;remote&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;website&lt;/span&gt; &lt;span class="n"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//username@yoursite.com/~/cmsite.git&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now all your have to do is push to production when you need to update your site and git does all the rest.&lt;br /&gt;
&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;website&lt;/span&gt; &lt;span class="n"&gt;master&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Because you can have more than one remote in git, you could have a test site and your production site, and another offsite repo for backups.  You can run tests on your test site and then when everything is good to go you just push to production.&lt;br /&gt;
&lt;/p&gt;</summary></entry><entry><title>AVAudioPlayer Memory Leak</title><link href="http://crosbymichael.com/avaudioplayer-memory-leak.html" rel="alternate"></link><updated>2011-09-11T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-09-11:avaudioplayer-memory-leak.html</id><summary type="html">&lt;p&gt;This is a little bug in the AVFoundation framework. When using the AVAudioPlayer in &lt;AVFoundation/AVAudioPlayer.h&gt; you will get memory leaks when running in the iOS Simulator but not on devices.&lt;/p&gt;
&lt;p&gt;It is a pain whenever you do most of your testing on the desktop during development and then start testing on device after you have a working app. So just remember if you are using AVAudioPlayer on the iOS Simulator and you are getting memory leaks that look like this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="AVAudio Leak" src="images/avaudio.png" /&gt;&lt;/p&gt;
&lt;p&gt;Look at the type of leaks.
note the responsible library is AudioToolbox not even AVFoundation, go ahead and profile it on a device and see if the leaks go away. If they don't, then it's something that you need to fix in your code, if they do then you are good to go.&lt;/p&gt;</summary></entry><entry><title>My Creations</title><link href="http://crosbymichael.com/my-creations.html" rel="alternate"></link><updated>2011-09-01T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-09-01:my-creations.html</id><summary type="html">&lt;h2&gt;CodeAssistant&lt;/h2&gt;
&lt;p&gt;&lt;img alt="CodeAssistant" src="http://backend-api.s3-website-us-east-1.amazonaws.com/public/php.png" /&gt;&lt;/p&gt;
&lt;p&gt;CodeAssistant is a quick and simple desktop application to run code snippets.  Have you ever been messing with a new API and you just want to execute one routine to see the output?  Just throw your code snippet into CodeAssistant and it will run and display the output.  It works on many languages, from C#, C, C++, Go, Python, Ruby, PHP, and it can be easily extended by adding XML config files.  CHeck it out and download it for $5.00 &lt;a href="http://code-assistant.com"&gt;here.&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;HTTP Assistant&lt;/h2&gt;
&lt;p&gt;&lt;img alt="HTTP Assistanat" src="images/httpassistant.png" /&gt;&lt;/p&gt;
&lt;p&gt;HTTP Assistant is a cross-platform REST client for testing web services.  You can check out all the features and purchase it for $5.00 &lt;a href="http://httpassistant.com"&gt;here.&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Encoder Assistant&lt;/h2&gt;
&lt;p&gt;&lt;img alt="Encoder Assistant" src="images/encoderassistant.png" /&gt;&lt;/p&gt;
&lt;p&gt;This is another cross-platform app that will help you hash and encode text when working with web services.  You can grab it &lt;a href="http://httpassistant.com/?page_id=170"&gt;here as well.&lt;/a&gt;&lt;/p&gt;</summary></entry><entry><title>SVN to GIT and Extracting Individual Projects</title><link href="http://crosbymichael.com/svn-to-git-and-extracting-individual-projects.html" rel="alternate"></link><updated>2011-08-15T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-08-15:svn-to-git-and-extracting-individual-projects.html</id><summary type="html">&lt;h2&gt;This post is still not formatting.  I switched from worpress and this is a long post and have not had time to update it.&lt;/h2&gt;
&lt;p&gt;So here is the issue.  You first started to use version control and because you were coding solo and the low learning curve, you started to use Subversion (SVN).  It worked great for you because you had your own repository and you added all your projects to it and it was easy.  Then you started to work with other people on projects and the very first commit that you made to your svn repo ended in conflicts.  Not a good way to start.  So you started to use more powerful version controle systems like GIT and now you wonder why you ever used svn in the first place.&lt;/p&gt;
&lt;p&gt;However, you have a years worth of revision history sitting in your svn repository that you want out and placed into your shinny new git repo.  You have already been working in your git repo for a few months, you just checked out the latest copy from your svn repo and added it to a new git repo, and now you have a branches and commits of all your recent work.  It's time to convert your svn to git and unite your past revision history with the present.  Lets get to work.&lt;/p&gt;
&lt;p&gt;Video is at the bottom if this is confusing.&lt;/p&gt;
&lt;p&gt;Problems:&lt;/p&gt;
&lt;p&gt;SVN repo has many different projects in the one large repository&lt;/p&gt;
&lt;p&gt;SVN has a year worth of revision history with commits from many different projects&lt;/p&gt;
&lt;p&gt;You checked out the latest copy from svn and made git repositories for each of your projects&lt;/p&gt;
&lt;p&gt;These individual project git repos now have many commits of their own and branches of your recent work&lt;/p&gt;
&lt;p&gt;Goal:&lt;/p&gt;
&lt;p&gt;Extract individual projects with only their complete revision history from svn and merge that with your current work and revision history in your individual git repos.&lt;/p&gt;
&lt;p&gt;Lets look at our starting point.  SVN revision history and GIT revision history.&lt;/p&gt;
&lt;p&gt;Last svn file change and git's last change.  You can see that the files are different.  SVN on left and GIT on right.&lt;/p&gt;
&lt;p&gt;1:&lt;/p&gt;
&lt;p&gt;First we need to suck our svn repo down to a machine where we have shell access.  I had a large 4GB svn repo with 25+ projects in it.  It had 280 commits from various projects.  ie: commit 278 from Encoder Assistant changes, commit 279 from HTTP Assistant changes, and commit 280 from another project.  So the svn repo was tracking overall revision history for the entire repo and not individual projects.  This was a mistake on my part, I was new to svn and my hosting provider offered free private svn repos to use.  So it was a free way for me to have revision history and also offsite backup of my code.&lt;/p&gt;
&lt;p&gt;To grab your svn repo from various locations you need to first make a new folder and cd into it.  Yes, you will have to use the terminal.&lt;/p&gt;
&lt;p&gt;git svn clone url-to-repo&lt;/p&gt;
&lt;p&gt;(not the working copy, the actual svn repo)&lt;/p&gt;
&lt;p&gt;Local file:///Users/michael/svnrepo&lt;/p&gt;
&lt;p&gt;Http  http://yoursite.com/svnrepo&lt;/p&gt;
&lt;p&gt;SSH ssh://username@yoursite.com/svnrepo&lt;/p&gt;
&lt;p&gt;So to cone I will do this:&lt;/p&gt;
&lt;p&gt;[highlight]git svn clone file:///Users/michael/Development/Video/WIP/SVNTOGIT/[/highlight]&lt;/p&gt;
&lt;p&gt;If you get an error with the wrong url to the svn repo then you will have to delete any folders in your current folder before you can try again.&lt;/p&gt;
&lt;p&gt;We can see by the terminal output and then doing an ls that it received 3 revisions(commits) and created a new folder of the svn repo name.  Now cd into it and you can see that there are two projects exactly in the same as the svn repo.  If we want to check to see if git cloned the full revision history we can type:&lt;/p&gt;
&lt;p&gt;[highlight]git log[/highlight]&lt;/p&gt;
&lt;p&gt;See it's all there.&lt;/p&gt;
&lt;p&gt;If you only had one project in your svn repo then you can skip the next step.&lt;/p&gt;
&lt;p&gt;2:&lt;/p&gt;
&lt;p&gt;Now we need to extract one project out of this new git repo.  We only want one project per repo in our new setup.  To do this we need to do a filter.&lt;/p&gt;
&lt;p&gt;Lets extract the contents of ProjectOne to the root dir of this new git repo.  (If you have more than one project you will want to clone this git repo so that you can keep do this over and over again for each project.)&lt;/p&gt;
&lt;p&gt;[highlight]git filter-branch --subdirectory-filter ProjectOne/ -- --all[/highlight]&lt;/p&gt;
&lt;p&gt;Now you can see that it rewrote the branch and by doing an ls you can see the contents of ProjectOne are now in the root of your repo.  There is only one code.txt file from this repo.&lt;/p&gt;
&lt;p&gt;Lets do a git log again and see that only the ProjectOne revision history is extracted with the ProjectOne files.&lt;/p&gt;
&lt;p&gt;You can see only two commits, the init and Added more code.  These are only the commits that affected ProjectOne.  Nothing for ProjectTwo.&lt;/p&gt;
&lt;p&gt;Lets make a new dir to keep the same folder structure as your CURRENT git repo that we have been working in.&lt;/p&gt;
&lt;p&gt;[highlight]mkdir ProjectOne[/highlight]&lt;/p&gt;
&lt;p&gt;Now we need to move the files back into this project one folder.&lt;/p&gt;
&lt;p&gt;[highlight]mv * ProjectOne [/highlight]&lt;/p&gt;
&lt;p&gt;This will give you an error about moving ProjectOne into it's self but it works and moves every other file and folder into our new ProjectOne folder.  You can cd into it and check for your self.&lt;/p&gt;
&lt;p&gt;Now we need to commit the changes that we have just done to the repo.&lt;/p&gt;
&lt;p&gt;[highlight]git add .[/highlight]&lt;/p&gt;
&lt;p&gt;This adds and removed all changes that we have made.&lt;/p&gt;
&lt;p&gt;[highlight]git commit -a[/highlight]&lt;/p&gt;
&lt;p&gt;This will commit all the changes we made.  Give it a message for the commit.&lt;/p&gt;
&lt;p&gt;3:&lt;/p&gt;
&lt;p&gt;Now we need to merge the revision history, branches, etc from our CURRENT repo with the new git repo's svn revisions.  We are still in the same folder of the new git repo that we have created.&lt;/p&gt;
&lt;p&gt;Lets create a new branch to add our current work into.  We will now refer to our git repo with the svn history as our OLD repo and the git repo with the CURRENT changes as the CURRENT repo.  This will get confusing to pay attention.&lt;/p&gt;
&lt;p&gt;To create and new branch and check it out at the same time we do:&lt;/p&gt;
&lt;p&gt;[highlight]git checkout -b current-repo[/highlight]&lt;/p&gt;
&lt;p&gt;Now we need to pull in the changes from the CURRENT repo into the current-repo branch of the OLD repo (the one with the svn revision history and extracted ProjectOne)&lt;/p&gt;
&lt;p&gt;git pull pathtorepo&lt;/p&gt;
&lt;p&gt;So in my example&lt;/p&gt;
&lt;p&gt;[highlight]git pull ~/Development/Video/WIP/GitCopy/svn-repo/[/highlight]&lt;/p&gt;
&lt;p&gt;So if you notice, it pulled the CURRENT repo into the OLD repo and when it tried to merge the changes into the branch it received a conflict.  Don't worry, resolving merging conflict's in git is easy.  If you are on OSX like me or have a merge tool setup, ie: FileMerge all you have to do when you receive a conflict on a merge it type:&lt;/p&gt;
&lt;p&gt;[highlight]git mergetool[/highlight]&lt;/p&gt;
&lt;p&gt;It will tell you what file you are going to be working with and when you hit enter, it will launch the tool so you can work on the conflict in the file.&lt;/p&gt;
&lt;p&gt;Now we can see the code on the left is from the last commit from our svn repo and the code on the right is from our CURRENT git repo with all the changes.  Now you just go down through the differences and select the left or right to keep.  After you are finished,  I am just going to keep all the right side changes, save the file and close your merge tool.&lt;/p&gt;
&lt;p&gt;Now if we type:&lt;/p&gt;
&lt;p&gt;[highlight]git status [/highlight]&lt;/p&gt;
&lt;p&gt;We can see that we have the modified file to commit.  If you have untracked files or if you have files with a .orig extension, conflicted files, add or remove those before committing.&lt;/p&gt;
&lt;p&gt;[highlight]rm ProjectOne/code.txt.orig[/highlight]&lt;/p&gt;
&lt;p&gt;Now commit&lt;/p&gt;
&lt;p&gt;[highlight]git commit -a[/highlight]&lt;/p&gt;
&lt;p&gt;You can see that git has already added a comment for you stating that this was a merge from another repo and also adds the repo path or url.  Write and quit your comment tool and it's done.  If we type in:&lt;/p&gt;
&lt;p&gt;[highlight]gitk[/highlight]&lt;/p&gt;
&lt;p&gt;This will bring up an gui to show our git repos history.&lt;/p&gt;
&lt;p&gt;Now you can see that we not only have the Init and  Added more code commit's that were originally from the svn repo but also all the commits from the git project added to our new branch.  If you want to do another merge to merge the changes from the current-repo branch back into the master branch for the project you can do that and now we can finally call this repository our CURRENT, full revision history, repository of awesomeness.&lt;/p&gt;
&lt;p&gt;If you were able to follow along, your welcome.&lt;/p&gt;</summary></entry><entry><title>Setup your own Git Server over SSH</title><link href="http://crosbymichael.com/setup-your-own-git-server-over-ssh.html" rel="alternate"></link><updated>2011-08-08T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-08-08:setup-your-own-git-server-over-ssh.html</id><summary type="html">&lt;p&gt;The thing about dvsc that makes me nervous is that all the code and revision history is on your local computer. This makes it very fast when committing, branching, and merging but what if you have a drive failure or worse? All your code is gone if you don't have backups. You have many different ways to backup your git and mercurial repositories. Especially this open-source project that I am working when it is done: DVCS-Backup&lt;/p&gt;
&lt;p&gt;If you want to work with others on a project and need a git server setup to be an intermediary, then this is how you can do it as well as having an offsite backup copy of your repos.&lt;/p&gt;
&lt;p&gt;First find a server that you are going to use. Setup keyless ssh access to it following my tutorial here&lt;/p&gt;
&lt;p&gt;After that ssh into your server and make a new user that will manage the repos if sharing or just user your own account if it's just for you.&lt;/p&gt;
&lt;p&gt;Install git on your server. I run debian so it's a simple:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;apt&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;git&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;To get git installed.&lt;/p&gt;
&lt;p&gt;Now create a new folder for the repo that you want to push to. I have many repos on my development computer that I work from so we are going to push an already populated repo to the server.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt; &lt;span class="n"&gt;somereponame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You add the .git to the end of the folder name to that it's easy to distinguish between other folders. .git means this is a git repo.&lt;/p&gt;
&lt;p&gt;Now cd into it and create a bare repo.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;bare&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now that server part is done. Easy.&lt;/p&gt;
&lt;p&gt;Now go to your local dev computer and cd into the repo that you want to push to the server. Make sure that you don't have a remote origin already setup in this repo. To clear it out just:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;remote&lt;/span&gt; &lt;span class="n"&gt;rm&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need to set the origin to your remote server.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;remote&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="n"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//sshusername@yourserver.com/~/myrepo.git&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now your done. Time to push. If you have more than one branch, you can either push one or all the branches to the server. I just push all of them because I want the server to have a full backup or my local repo.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;all&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;But for inividual branches:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="n"&gt;branchname&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;That's it. Now when you want to get data from the server just do a pull.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;pull&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="n"&gt;branchname&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you want to clone the repo to a new folder:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;clone&lt;/span&gt; &lt;span class="n"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//sshusernane@yourserver.com/~/myrepo.git&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or you can make a new dir, cd into it and create a new local repo, set the origin to your server and pull specific branches.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt; &lt;span class="n"&gt;newrepo&lt;/span&gt;
&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="n"&gt;newrepo&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;remote&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="n"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//sshusername@yourserver.com/~/myrepo.git&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;pull&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="n"&gt;branchname&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</summary></entry><entry><title>Flex 4.5 vs Pure AS3</title><link href="http://crosbymichael.com/flex-45-vs-pure-as3.html" rel="alternate"></link><updated>2011-08-07T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-08-07:flex-45-vs-pure-as3.html</id><summary type="html">&lt;p&gt;I have built a few applications for Android using the Flex framework.  It is a great framework and I can build, test, and publish apps very quickly.  It takes productivity to the max.&lt;/p&gt;
&lt;p&gt;However, I have noticed that some of my apps take a noticeable time to launch on my device for the first time.  Even simple apps like QuickIP where all it does it check for external and wifi IP address.  So I ran a few comparison tests between pure AS3 projects and Flex projects to see three things.  Startup time, file (swf) size, and memory usage.  All important on a mobile device.  Here are the results.&lt;/p&gt;
&lt;h2&gt;Mobile Pure AS3&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup Time: 99ms&lt;/li&gt;
&lt;li&gt;SWF Size: 1.5kb&lt;/li&gt;
&lt;li&gt;Memory Usage*: 6.328mb&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Mobile Flex mxml&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup Time: 653ms&lt;/li&gt;
&lt;li&gt;SWF Size: 2.839mb&lt;/li&gt;
&lt;li&gt;Memory usage*: 21.397mb&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Memory usage is a sample taken after 5 seconds of the app being launched.  All the apps are just code to test startup time and memory usage after 5 seconds.  The Flex mobile project was a blank project.  Each project launched a view that was run on my desktop, not on a device.&lt;/p&gt;
&lt;h2&gt;So how does this compare?&lt;/h2&gt;
&lt;h2&gt;AS3 over Flex&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;AS3 launches in 15.16% of the time it takes a Flex project&lt;/li&gt;
&lt;li&gt;AS3's file size is .05% of the Flex projects size&lt;/li&gt;
&lt;li&gt;AS3 uses 29.58% of the memory that a Flex project uses&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As for the desktop, the numbers are closer to the same.  Remember that I launched the mobile project on my desktop rather than on a device so the tests have a consistent test environment.&lt;/p&gt;
&lt;h2&gt;Desktop Pure AS3&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup Time: 96ms&lt;/li&gt;
&lt;li&gt;SWF Size: 1.5kb&lt;/li&gt;
&lt;li&gt;Memory Usage*: 6.184mb&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Desktop Flex mxml&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup Time: 556ms&lt;/li&gt;
&lt;li&gt;SWF Size: 2.114mb&lt;/li&gt;
&lt;li&gt;Memory usage*: 19.021mb&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As you can see the desktop Flex project is a little lighter on the resources than a comparable Mobile Flex app.  I even did some tests on the desktop such as only using spark and no mx.  I have even changed the WindowedApplication to just Application (no visible window as launch) and was able to get the startup time down to 450ms and only using ~15mb of ram.  I tried to do tests with a Flex project's initial application file as a .as instead of a .mxml file but they were unsuccessful in launching a visible window.  However just launching an  initial .as instead of .mxml did make the Flex project have the same ~99ms startup time, ~1.5kb swf size, and ~6mb memory footprint.&lt;/p&gt;
&lt;p&gt;So the question is... Is the speed of productivity in building an app on the Flex framework worth the loss of speed running the app?  I guess it depends on what type of app you are building and what your user base is.  The big difference between the Flex framework and pure As3 is components.  You have to build all the components your self in pure AS3 projects.  But how will productivity change for pure As3 projects after you already have a library of components built?  What if you make all your assets and skin the Flex components anyway with bitmap or fxg assets?  Do the components matter then?&lt;/p&gt;
&lt;p&gt;These are the questions that I am asking my self currently.  I am going to rebuild my QuickIP app with pure AS3 since it has all custom bitmap assets then compare the performance on my Android device and see if the time of development makes a noticeable jump in performance.&lt;/p&gt;</summary></entry><entry><title>Setup Password-less Login over SSH</title><link href="http://crosbymichael.com/setup-password-less-login-over-ssh.html" rel="alternate"></link><updated>2011-08-07T00:00:00Z</updated><author><name>Michael Crosby</name></author><id>tag:crosbymichael.com,2011-08-07:setup-password-less-login-over-ssh.html</id><summary type="html">&lt;p&gt;Everyone needs a fast instance in a datacenter to work with. Amazon's EC2 or any other VPS provider will give you access. Sometimes you need to transfer files around fast or do some data processing. Having a fast instance will help you get your job done faster.&lt;/p&gt;
&lt;p&gt;I have a tutorial that I needed to upload to an FTP server and do an FXP copy. The file is resting on another FTP server form a basic hosting provider. I do not have FXP access. So I launched an instance on EC2. Logged in via ssh. wget the file from my hosting server. Then connected to the marketplace FTP and uploaded the file. Easy, fast, and all the transferring was done on someone else's bandwidth.&lt;/p&gt;
&lt;p&gt;To make this easier here is how to setup ssh to use a ssh key file instead of a password. This has more advantages than just making it quicker to ssh into my instances.  You can use this to setup automated backups using rsync.&lt;/p&gt;
&lt;h2&gt;How To:&lt;/h2&gt;
&lt;p&gt;On your local machine you need to jump into terminal for this. Go to your home dir and make a new .ssh folder if it does not exist. To see if it does exist just type:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;ls&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If not exist then:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ssh&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now cd into it:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ssh&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Now we need to generate an rsa key file:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;keygen&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="n"&gt;rsa&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Do not enter a passphrase, just leave it blank.&lt;/p&gt;
&lt;p&gt;Now you need to ssh into your instance or remote computer and make an .ssh folder in your user home folder. Same as above.&lt;/p&gt;
&lt;p&gt;Now cd into and make a new file called:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;authorized_keys&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;In this file, paste the contents of your local computers newly generated id_rsa.pub file into it.&lt;/p&gt;
&lt;p&gt;Now, still on your remote computer or instance, you need to change the permission of the authorized_keys file to 644:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;chmod&lt;/span&gt; &lt;span class="mi"&gt;644&lt;/span&gt; &lt;span class="n"&gt;authorized_keys&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;All done. Exit your ssh session and login again and you will notice that you don't have to enter a password now.&lt;/p&gt;
&lt;p&gt;I suggest changing your password to something really long and secure since you won't be using it anymore, unless logging into to it from a different computer.&lt;/p&gt;</summary></entry></feed>