Skip to content

Create your application

Plaristote edited this page Mar 9, 2016 · 2 revisions

Create a Crails Application

In this tutorial, we'll see:

  • What dependencies Crails needs you to install in your system
  • How to install Crails on your system
  • How to create and launch a simple Crails application

Install Crails' dependencies

Ruby

While Crails server completely runs over C++, its asset pipeline and many other utilities are based on guard, a tool that watches files and re-compile your assets, your build targets, or run your tests when changes are detected.

Note that your production server doesn't need to provide ruby. This is only a development dependency. Make sure your development environment provides a version of ruby >= 1.9.

Boost

The first dependency you'll need to provide is a version of Boost >= 1.59.

cpp-netlib

Once Boost is installed, you can install the cpp-netlib, the framework that provides our http server.

Optional dependencies

There are a few other libraries that you may need to install before compiling Crails, if you want to use additional modules. See the respective documentation for each module in order to find out what more you need to install.

Common optional dependencies would be ODB for SQL databases, or Magick++ for image processing.

Install Crails

Using the following commands, you will clone the Crails repository, configure the project, build it and install it on your system.

$> git clone https://github.com/Plaristote/crails.git
$> mkdir crails/build
$> cd crails/build
$> cmake ..
$> make && sudo make install

Create a Crails application

To create a crails application, use the command crails new [application_name]. This will create a directory named 'application_name', bootstrap it with all the necessary file to make a crails server, and use bundle install to prepare your ruby environment.

Build your newly made application

On your own

You may build the application on your own, by going to the build directory and run the cmake .. && make command.

Using guard

You may also build your application from guard, by launching the crails guard command, and type the crailscmake command once guard's prompt appear.

Run your newly made application

You can then launch your server from your application directory by launching the build/server command. Your server comes with a few options:

-h, --hostname: the hostname to listen from, defaults to 127.0.0.1
-p, --port: the port to bind, default to 3001,

If you are using an asynchronous server, a few more options are open to you:

-t, --threads: the number of threads to use for the thread-pool,
--ssl: enables ssl
--ssl-cert: the path to your ssl certificate file
--ssl-key: the path to your ssl key file
--ssl-dh: the path to a tmp_dh_file

Stopping your application

The server reacts to the INT signal. You may send it using the kill command, or by typing Ctrl+C from the console.

Serving file from your application

Any file in the public folder can already be served by your application. For instance, public/assets/application.js would be reachable from http://localhost:3001/assets/application.js

Doing more stuff with your application

Of course, if you're using a framework, you probably didn't intend to just make a file server. Now that you can create a Crails application, you may go to the next step of the tutorial and learn about how to implement a Router.