Crystal and Kemal

I’ve had my eye on the crystal programming language for a while now, and I have to say I’m pretty excited about it. Ruby syntax combined with the speed of a statically typed compiled language, common now. It’s compiler is now bootstrapped and benchmarks performances are impressive (yeah I know, benchmark scores don’t matter). I would suggest watching the video on the project’s own Bountysource page for a more in-depth look into the language itself. Here I plan on playing around mostly with the Kemal framework (seriously those benchmarks though).

benchmarks

There is already a decent collection of frameworks and libraries over at Awesome Crystal. Database drivers, a CSFML wrapper, 3rd party api libraries, etc, and amongst those is Kemal.

Most of my experince is with Ruby, specifically Ruby on Rails. I’ve also used both Sinatra and Padrino, and I enjoyed working with both of those. I’m going to try and build in a bit of structure to the project: models in a models folder, controllers in a controllers folder, etc. This will also be a single page application using AngularJS and Kemal as the JSON api.

beer

Before we get started, I just want to throw this out there. I’m double fisting New Belgium Ranger and a Mikes Harder Strawberry Lemonade because thats how I roll, gotta hit that Balmer Peak





I’m assuming you already have Crystal installed, if not visit their installation page follow the instructions per your platform. I’m using Crystal 0.13.0.

If you want to check out the source for this project, i’ve stuck it in this repo fridgerator/kemal_test.

Generating the project was easy enough, crystal has a built in project generator which creates a basic project structure for us crystal init app kemal_test. Then add the require dependencies to the shards.yml file (kemal, active_record, and postgres_adapter). Shards is a dependency manager similar to ruby gems and the shard.yml file looks like a cross between a Gemfile and a package.json file. Pretty slick, the crystal project is in alpha stage still and already 13x easier to set up than any node.js application.

For data / domain models, I’m using active_record.cr. I didn’t find anything akin to rails DB rake tasks or migrations, so I created the database and tables manually in the psql interface.

class Post < ActiveRecord::Model
  adapter postgres

  primary id        : Int
  field title       : String
  field body        : String

  def to_h
    {
      id: id,
      title: title,
      body: body
    }
  end
end

I didn’t use anything special for controllers, just Classes containing route definitions.

class KemalTest::Controllers::PostsController
  before_all "/posts" do |env|
    env.response.content_type = "application/json"
  end

  get "/posts" do |env|
    posts = Post.all.map(&.to_h)
    posts.to_json
  end

  ...
end

Kemal serves up static files out of /public, so I threw my main.js, main.css and angular template files in that directory.

So far I have everything I set out for: simple rails-like structure, ruby syntax, insane server response times (most were in the hundreds of microseconds). I’m 100% satisfied and will definitely continue to dink around with Crystal/Kemal in the future.

Goodbye Dropbox and Spotify, Hello ownCloud

I already pay $20 a month for two Spotify accounts (one for me and my wife) and now my dropbox is getting full.  Instead of dishing out the $30 a month I decided to check out other options first.  I came across this article at lifehacker.com which sparked my attention.  I did some more digging around and read quite a bit about ownCloud that sounded right up my alley other than the fact that its written in php... boooooo.

I also, had been previously looking into getting a VPS but could never think of a good enough use for one to justify the cost.  Backupsy.com is a VPS configured for higher storage options (base level is 250GB) AND you can get it for $5 a month.  For me this sounded like a couple of hours of work for a $25 a month savings and WAY more customization and options. 

Visit backupsy.com and sign up for their 250GB storage, 512mb ram 1,000GB bandwidth plan using coupon code "BIGDADDY" to get all this for $5 a month (50% off).  I opted for the automatic OS install but apparently their automatic OS install wasn't working correctly so I installed the OS myself (Ubuntu 12.04).

Once all this is finished, log into the box using the web console.  I had to use firefox because the Java applet needed to do this doesn't work in chrome.  Log in as the user you created during set up and install an ssh server with "sudo apt-get install openssh-server".  You might want to read up on securing an ssh server, and configure at least disabling root login via ssh.

Next lets get lamp, php and ownCloud installed following the instructions here.  When you're done you should have a working ownCloud install and an admin user set up.

I wanted to be able to sync my current ~/Dropbox folder, and also upload all of my digital music without syncing the music back to my computer.  Luckily ownCloud supports the option to only sync specific folders with remote ones.

Install the ownCloud sync client, and create a folder that you want to sync, I created an ownCloud folder in my home folder.  And instead of syncing data with the remote root folder, choose and create a new remote folder to sync with.  If you sync with the remote root folder everything will sync (music, photos) and you will only be able to create one sync.

You also might want to check out the ownCloud apps that are available to use.  I will probably end up creating an account for my wife on my ownCloud and installing the internal messages app for us to use.  And giving her an account will allow her to have her own storage and music service as well.

I've copied my entire dropbox folder into my ownCloud folder and I'm waiting for that to sync.  After that I'll begin uploading my music and testing out the streaming both to my laptop and my android.  As long as this all works out ok I'll be dropping both Spotify and my dropbox account for this.  Next I'll also be looking into enabling SSL and maybe developing my own addons / apps.

HOWTO: Set up a Rails 3.2.11 (ruby 1.9.3) app on a dreamhost shared account

I recently took advantage of a sweet deal at Dreamhost and signed up for a year of their "Shared Hosting". Being a Railsist (??) I wanted a temporary domain / server where I could throw up my random Rails / Rack projects.  Its definitely doable, but there is a tint of hacking involved.

First off, go into your panel and click "Manage Domains" section and edit the domain you want to run rails on.  Scroll down and check the Passenger checkbox since we'll be using their pre-installed passenger setup to serve up our applications.  Since I will be using Capistrano to deploy my applications I also changed the 'Web directory' to /home/username/domain.name/current/public.  This might not be necessary for you depending on your setup.

Next up we're going to want to enable shell access so you can deploy your application and install some other required software.  Go to 'Manager Users' section of the panel and click 'edit' on the user you want to allow ssh access to.  Change the 'account type' to Shell account instead of the default FTP account.

Now create a mysql database (unless you plan on using sqlite).  Click into "Mysql Database" and add a new one.  Note that you will not be able to connect to the database via localhost or the socket file, so you will have to add whatever hostname you chose to your production database.yml file.

You'll see a warning that it might take 5-10 minutes for the settings to change, one that happens then you can proceed.

By default, Dreamhost only has Ruby 1.8.7 installed :(  Go ahead and ssh into your shell and we'll start by installing RVM and the latest ruby.  Run the command \curl -L https://get.rvm.io | bash -s stable --ruby to install both in one shot.  I usually do a rvm use default 1.9.3 just be sure I'll be using the correct ruby version, but this might not be necessary.

When thats finished go ahead and insall the Rails, mysql and therubyracer gems gem install rails mysql2 therubyracer

We're also going to want to install node.js for our javascipt runtime and compilation.  In your home directory, create a downloads folder mkdir downloads; cd downloads .    We're also going to need a ~/usr/local/bin dir to store the node binaries mkdir -p ~/usr/local/bin. Grab the latest node.js source from github git clone git://github.com/ry/node.git; cd node.  Make that shit ./configure --prefix=~/usr/local; make; make install and add our the bin directory we created to the environment path by adding export PATH=$PATH:/home/USERNAME/usr/local/bin to both .bashrc and .bash_profile replacing USERNAME with your account username.  Now close your connection and re-connect to check if node installed correctly with node -v .

You now have an up to date Rails server with the latest Ruby and Node versions installed.