5 min read

(For more resources related to this topic, see here.)

Creating an application

Using the Command Line Tool, we are going to create a very new application. This application is also going to be a Sinatra application that displays some basic date and time information.

First, navigate to a new directory that will be used to contain the code. Everything in this directory will be uploaded to AppFog when we create the new application.

$ mkdir insideaf4 $ cd insideaf4

Now, create a new file called insideaf4.rb. The contents of the file should look like the following:

require 'sinatra' get '/' do erb :index end

This tells Sinatra to listen for requests to the base URL of / and then render the index page that we will create next.

If you are using Ruby 1.8.7, you may need to add the following line at the top:

require 'rubygems'

Next, create a new directory called views under the insideaf4 directory:

$ mkdir views $ cd views

Now we are going to create a new file under the views directory called index.erb. This file will be the one that displays the date and time information for our example.

The following are the contents of the index.erb file:

<html><head> <title>Current Time</title> </head><body> <% time = Time.new %> <h1>Current Time</h1> <table border="1" cellpadding="5"> <tr> <td>Name</td> <td>Value</td> </tr> <tr> <td>Date (M/D/Y)</td> <td<%= time.strftime('%m/%d/%Y') % </tr> <tr> <td>Time</td> <td><%= time.strftime('%I:%M %p') </tr> <tr> <td>Month</td> <td><%= time.strftime('%B') %></t </tr> <tr> <td>Day</td> <td><%= time.strftime('%A') %></td </tr> </table> </body></html>

This file will create a table that shows a number of different ways to format the date and time. Embeded in the HTML code are Ruby snippets that look like <%= %>. Inside of these snippets we use Ruby’s strftime method to display the current date and time in a number of different string formats. At the beginning of the file, we create a new instance of a Time object which is automatically set to the current time. Then we use the strftime method to display different values in the table.

For more information on using Ruby dates, please see the documentation available at http://www.ruby-doc.org/core-2.0.0/Time.html.

Testing the application

Before creating an application in AppFog, it is useful to test it out locally first. To do this you will again need the Sinatra Gem installed. If you need to do that, refer to Appendix, Installing the AppFog Gem.

The following is the command to run your small application:

$ ruby indiseaf4.rb

You will see the Sinatra application start and then you can navigate to http://localhost:4567/ in a browser. You should see a page that has the current date and time information like the following screenshot:

To terminate the application, return to the command line and press Control+C.

Publishing to AppFog

Now that you have a working application, you can publish it to AppFog and create the new AppFog application.

Before you begin, make sure you are in the root director of your project. For this example that was the insideaf4 directory.

Next, you will need to log in to AppFog.

$ af login

Attempting login to [https://api.appfog.com]

Email: [email protected]

Password: ********

Successfully logged into [https://api.appfog.com]

You may be asked for your e-mail and password again, but the tool may remember your session if you logged in recently.

Now you can push your application to AppFog, which will create a new application for you. Make sure you are in the correct directory and use the Push command. You will be prompted for a number of settings during the publishing process. In each case there will be a list of options along with a default. The default value will be listed with a capital letter or listed by itself in square brackets. For our purposes, you can just press Enter for each prompt to accept the default value. The only exception to that is the step that prompts you to choose an infrastructure. In that step you will need to make a selection.

$ af push insideaf4

Would you like to deploy from the current directory? [Yn]:

Detected a Sinatra Application, is this correct? [Yn]:

1: AWS US East – Virginia

2: AWS EU West – Ireland

3: AWS Asia SE – Singapore

4: HP AZ 2 – Las Vegas

Select Infrastructure:

Application Deployed URL [insideaf4.aws.af.cm]:

Memory reservation (128M, 256M, 512M, 1G, 2G) [128M]:

How many instances? [1]:

Bind existing services to insideaf4? [yN]:

Create services to bind to insideaf4? [yN]:

Would you like to save this configuration? [yN]:

Creating Application: OK

Uploading Application:

Checking for available resources: OK

Packing application: OK

Uploading (1K): OK

Push Status: OK

Staging Application insideaf4: OK

Starting Application insideaf4: OK

Summary

Hence we have learned how to create application using Appfog.

Resources for Article :


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here