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).

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.

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.

Please enable JavaScript to view the comments powered by Disqus.