paint-brush
Starting a project with Crystal and Kemalby@codelessfuture
1,716 reads
1,716 reads

Starting a project with Crystal and Kemal

by Massimiliano Bertinetti2mApril 16th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Hello guys. I was looking for a new programming language with which to start my freelance computer programmer work. Looking around I found <a href="https://crystal-lang.org/">Crystal</a> and I love the readable syntax, similar to Ruby. Reading the documentation you read the objectives that led to the creation of this language:

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Starting a project with Crystal and Kemal
Massimiliano Bertinetti HackerNoon profile picture

How I came to Crystal

Hello guys. I was looking for a new programming language with which to start my freelance computer programmer work. Looking around I found Crystal and I love the readable syntax, similar to Ruby. Reading the documentation you read the objectives that led to the creation of this language:

Crystal is a programming language with the following goals:

  • Have a syntax similar to Ruby (but compatibility with it is not a goal).
  • Be statically type-checked, but without having to specify the type of variables or method arguments.
  • Be able to call C code by writing bindings to it in Crystal.
  • Have compile-time evaluation and generation of code, to avoid boilerplate code. Compile to efficient native code.

OK. Looks good!

I searched among the resources to see if there were sufficient materials that should make it immediately usable (I have to report lack of standard documentation, but actually the language is in alpha stage). For me it was enough to continue.

How I came to Kemal

I think Kemal is the standard the facto web framework for Crystal. Serdar Dogruyol is a kind and helpful person and I love all the community on Gitter (both Crystal and Kemal have a channel).

Installation

Actually Crystal don’t work on Windows. So you have to use a Mac or a Linux based system.

OS X

brew update
brew install crystal-lang --with-llvm

Debian / Ubuntu

curl https://dist.crystal-lang.org/apt/setup.sh | sudo bash
sudo apt-get install crystal

For other distros check the official documentation.

The Workflow

Crystal is a compiled language, so it’s helpful to have a tool like Sentry that build your crystal application, watches files, and rebuilds app on file changes.

I chose Atom as IDE, with a couple of packages: Crystal-tools and Crystal.

The Project

I want to create a simple dynamic website, with Bootstrap layout and dynamic content, something of everyday use for a freelancer.

  • Create the project
  • Integration of sentry
  • Integration of layouts
  • Authorization for backend
  • Dynamic Content (pages, news….)

OK. Lets start!

A note: In the following code $ is the command prompt and the square brackets indicate a text chosen by you to replace the present one.

You should create your application first. Go to the desired location then:

$ crystal init app [APP_NAME]
$ cd [APP_NAME]

Install Sentry in your project:

$ curl -fsSLo- https://raw.githubusercontent.com/samueleaton/sentry/master/install.rb | ruby

open the project in your choosed editor,

go to .gitignore and modify as follow:

/.shards/
/doc/
/lib/
/bin/
/dev/
sentry
[APP_NAME]

(here we remove sentry and the executables from source control)

then add kemal to the shard.yml file as a dependency.

dependencies:
  kemal:
    github: kemalcr/kemal
    branch: master

and then

$ shards update

and substitute [APP_NAME].cr with the following:

require "kemal"

get "/" do
  "Hello World!"
end

Kemal.run

then start sentry:

$ ./sentry

you see something like:

🤖 Your SentryBot is vigilant. beep-boop…

🤖 watching file: ./src/prova/version.cr

🤖 watching file: ./src/prova.cr

🤖 compiling prova…

🤖 starting prova…

[development] Kemal is ready to lead at http://0.0.0.0:3000

go to the browser and check that everything is ok.

Enough for today.

Next time we work to make this skeleton app a working app.

See ya