Web Frameworks in Crystal


Bookmark and Share

 

The creator of Ruby, Yukihiro Matsumoto said that the language was made to make programmers happy. Imagine a language as elegant as Ruby and blazingly fast as C. If you already love Ruby, maybe it is time to start considering Crystal lang, the younger sibling of Ruby. It is a compiled language which is syntactically very similar to Ruby and designed for high throughput and performance.

Features

  • Native WebSocket support
  • Compiles to efficient native code
  • Statically typed => if any errors it will let you know at compile time.
  • Less memory consumption

Web frameworks in Crystal

Applications developed using framework are easy to maintain and upgrade at a lower cost. This article lets you get familiar with some of the most popular frameworks of Crystal.

Install Crystal to get started.

https://crystal-lang.org/docs/installation/index.html

Create our Crystal app

Kemal

https://github.com/kemalcr/kemal

To know the true potential of Crystal, let’s familiarise with Kemal, most popular Crystal framework. It’s a Sinatra inspired framework.

Install Kemal

In our app,

Open in an editor and add Kemal as a dependency in the shard.yml file.

To get dependencies, run

This will install Kemal.

This is similar to adding gems in gem file and bundle install in Rails.

In the file sample-app.cr created in the src directory, substitute the following

In the …

Read More

Painless Cron jobs in Crystal using Schedule


Bookmark and Share

Many Ruby developers use the awesome whenever gem for scheduling tasks in their projects, so do we and with ‘whenever’  scheduling tasks become so effortless that we absolutely loved it. Recently we have been deploying Crytal apps to production and Kemal is our framework of choice. During the process, we felt the urge to have something similar to whenever in Crystal for cron jobs and that’s when we discovered Schedule – a Crystal shard that provides a clear DSL to write periodic or scheduled tasks and there was no turning back.

Getting started

Add Schedule to your shard.yml file

you are all set to schedule your tasks, all you gotta do is require the schedule module as require “schedule”

Examples

Schedul’s API defines 2 important methods, .every and .after for periodic and scheduled execution of tasks respectively.

Periodic execution

For running a task periodically we have to pass in a valid interval as well as a block to the .every method. For example,

will print the message on every 3 seconds.

Similarly

will call the class method hourly_job every 1 hour

Scheduled execution

For scheduling tasks to run sometime in the future we can make use of the after method.

Stop and Retry jobs

Call Schedule.retry to retry a task and …

Read More

Deploying Crystal app to production using Heroku


Bookmark and Share

This article assumes that you have Crystal and Heroku CLI installed.

Create a sample application

Create a new application using

You should see the following on your terminal

now cd to the root of the app, $ cd sample-app  and add Kemal to shrad.yml file

and run $ crystal deps for installing dependencies.

Add a “/” route

Open sample-app.cr file and add replace the contents with the following code

now run $ crystal src/kemal_sample.cr  and go to http://localhost:3000on your browser. You should see ur app running and the message ‘Hello World’.

Deploy to Heroku

So far so good 🙂 Now let us deploy our app to Heroku. Run

$ heroku create your-custom-name --buildpack https://github.com/crystal-lang/heroku-buildpack-crystal.git

You should see something like the following on your terminal.

You are all set. Commit and push to heroku

To see your app running go to https://your-custom-name.herokuapp.com/ . Congratulations you just deployed your crystal app to production

Happy Coding

Read More