Sinatra on Heroku

Steps to get a sinatra/datamapper site running on heroku.  First do git init in a directory then you need 3 files: A Gemfile with the gems used, make sure to group them since sqlite isn't available on heroku.
source :rubygems
gem 'sinatra'
gem 'thin'
gem 'haml'
gem 'datamapper'
gem 'dm-postgres-adapter', :group => :production
gem 'dm-sqlite-adapter', :group => :development
A Procfile, simply tells heroku which file to start with, note the -e production to put the site in production mode (in case you load different dependencies depending on environment)
web: bundle exec ruby demo.rb -p $PORT -e production
And a simple Sinatra site
require 'sinatra'
require 'haml'
require 'datamapper'

class Item
  include DataMapper::Resource

  property :text, String,:key => true
end

configure do
  DataMapper.setup(:default, ENV['DATABASE_URL'] ||
                             "sqlite3://#{Dir.pwd}/demo.db")
  DataMapper.finalize
  DataMapper.auto_migrate!

  Item.create(:text => "hi")
end

get '/' do
  @demo = Item.all

  haml :view
end
Install heroku toolbelt, then do the following:
heroku login
heroku create
heroku addons:add heroku-postgresql:dev
heroku pg:promote HEROKU_POSTGRESQL_BROWN
The last command sets the DATABASE_URL environment variable to point to the added postgres instance, the name depends on the added instance type use heroku config to get the correct one. Once all these things are done you can push the site to heroku.
git push heroku origin
For local testing start the site with this command
bundle exec ruby demo.rb
I've put a demo repo for the sinatra site in a github repo here

How to use IProcessHostPreloadClient

With .Net Framework 4.0 an interface called IProcessHostPreloadClient was added, it has one method called Preload. With a correctly configured application pool and Website this method will be called as soon as IIS has started, this allows you to run code in your website before the first visitor has arrived causing application_start to kick in. This can be used to prewarm your website cache, to magic stuff during site start up (register with a loadbalancer maybe?) or well anything else that can't wait until someone visits the website. Assuming you have a application pool and website in your IIS you first need to add a class implementing the IProcessHostPreloadClient interface that does some magic. Once that is done you need to edit the applicationHost.config (full path C:\Windows\System32\inetsrv\config\applicationHost.config) First find the applicationPool under the applicationPools node and add the following attributes to it:
  • autoStart=”true”
  • startMode="AlwaysRunning"
Then under the system.applicationHost node add a registration for your PreloadClient
<serviceAutoStartProviders>

  <add name="RegisterApplicationHandler" type=X.RegisterApplicationHandler, X" />

</serviceAutoStartProviders>
The name can be anything you want, the type needs to be the full name of the class implementing the IProcessHostPreloadClient (that namespace and class name)  comma the name of the assembly the class is in. Could be something like: WebApplication1.PreloadClient, WebApplication1 When the provider is added you need add it to the website, this is done by adding the these attributes:
  • application tag: serviceAutoStartEnabled="true"
  • serviceAutoStartProvider="RegisterApplicationHandler"
The value of serviceAutoStartProvider should be the same as the name of the provider you added. When everything is added save the file, this will cause IIS to reset since the applicationHost.config was altered. If everything is setup correctly your IIS should automatically spawn a w3wp.exe process as soon as it's started. Some things to think about
  • It's hard to debug this code since it's run before your debugger has a chance to attach to it.
  • If the configuration is incorrect your website and/or IIS won't start, check the event log for info
  • If Preload throws an exception then your site won't start, use pokemon exception handling if you must ;)
  • If you setup an IoC container in Application_start then that won't be available in Preload, so you either need to load it there too or find ways to do your work without it.

Rate All The Things

Last month I worked on a small idea that I called rate all the things. The idea was to create a smart phone app that could be used to rate anything that has a bar code (or QR code). So that's what I built, mostly just for the fun of it and since I fail at UX that was all the feedback I got from my Show Hacker News post. After a bit of thinking I decided to open source the site and Android app i built so that I can go on building something else (or just finish one of the billion other projects I toy with). Back end: built on asp.net using Nancy (instead of asp.net mvc) to support simple routing and overall fun to work with instead of an headache. Entry point are the different modules mapping out routes. To handle authentication the simplest way was to use basic authentication combined with strong random passwords (users are not able to select their own passwords) and running everything over SSL. Since most of the traffic for the site is over Json I decided that the SuperSimpleViewEngine bundled with Nancy was more that enough for my needs, just as me using TinyIoC since it's bundled and I'm not doing anything that requires something more fancy. Android app: To be able to scan bar codes I used the IntentIntegrator class supplied by zxing, it handles talking to Barcode Scanner and if it's not installed can take the user to Google Play to install it. I used the ActionBarCompat example to have an Action Bar in the app in all versions and not just on devices running Android 4. On the arcitecture side I used roboguice for ioc handling and alot of async tasks to wrap all network calls.

IpBackend

Small project that can be hosted on a free Appharbor instance that makes it possible for computers to report their IP address. Why ? Because my dyndns account expired and it was more fun to build a custom thing than register a new domain. The back end is based on my new favorite .net project: Nancy. To get started simply create database tables based on the db.sql script and put a user name and password into the web.config. There's a ruby script included on github with a example client the one I run from a cronjob to keep track of the IP of my home computer. For a even simpler client you could just use Curl.