How to build a mail search engine using Nitro

By Kashia.

Part 2 - Nitro Setup

This step assumes that Nitro is correctly installed and a database with enabled TSearch2 is available.

An initial Nitro project

This one's easy again:

$ gen app ~/my_mailsearch
$ cd ~/my_mailsearch

You have now a working Nitro project, you can test if everything works by running it.

$ chmod +x run.rb
$ ./run.rb --port 9999

This will fire up WEBrick on the port 9999 (which is also the default, it would've been chosen if you wouldn't have specified it). If you get a nice welcome message, you can applaud yourself already, you did great.

Next up will be some cleaning. You will replace the run.rb, as you don't need that help text, and the stuff in the public dir.

$ cd public
$ rm -r cgi.rb fcgi.rb js media scaffold robots.txt
$ cd ..
$ rm -r script conf
$ mv src app

You will now have following application tree:

$ ls *
    run.rb

    app:
    skin.rb

    public:
    error.xhtml

Now, that done, it all doesn't look all to scary, does it? Only 3 files to worry about.

error.xhtml

The error.xhtml you had better leave alone. It's just there to give you nice error messages. If you use Nitro for bigger projects, you'll remove that one as well.

run.rb

This is your main file, which you'll see in almost every Nitro project. It has everything in it, to initialize and start a Nitro application.

skin.rb

The filename of this 'concept' is very exact. This file is also a convention in the Nitro community. It has a Element inside, which you can use to encapsulate your templates within a common structure.

If you look into it, you will see a Ruby class derived from Nitro::Element. The most important thing here is the content variable, which will be where the content of your own templates will be pasted into.

Getting comfy within your environment

We will create a few directories which will be filled further on:

$ mkdir -p app/mail
$ mkdir -p templates/mail

As I said, you will replacing the run.rb, since it's useless like this.

#!/usr/bin/env ruby

require 'rubygems'

# gems
require_gem 'facets', '= 1.4.5' # a specific facets version, facet version
                                # changes can break your code.
require 'nitro'
require 'og'

# nitro helpers
require 'nitro/helper/pager'

include Nitro

# needs to be before models, allows changes to your database
Og.create_schema = true

### Current Application

# utilities                     # (1.)
require 'app/mail/mail_utils'

# models
require 'app/mail/model_mail'

# controllers
require 'app/controller_main'
require 'app/mail/model_mail'



# This has to be _after_ the models
$psql = Og.setup(               # (2.)
  :destroy => false,            # if `true`, it drops your database at start
  :evolve_schema => true,       # if `true`, it changes when your model does
  :store => :psql,              # :psql is postgres, :mysql, :sqlite etc. 
  :name => 'mymails',           # the name of the database
  :user => 'johndoe',           # database username
  :password => '',              # database password
  :connection_count => 2        # how many connections Og can make
  )

Template.root = 'templates'

Server.map.update(              # (3.)
  '/' => MainController,
  '/mail' => MailController
)


Nitro.run                       # (4.)

  1. Don't be confused by the require's to non-existing files in your app/ directory, we will add them shortly.

  2. The Og.setup is doing some magic, it connects the database with your model classes, which you'll create in a second.

  3. The Server.map.update() just sets some default values. It maps your controllers (yes, yes, I know, be patient, will ya?) to URLs.

  4. Nitro.run finally starts the application.

first
last