Creating an Application from Scratch

Exclusive offer: get 50% off this eBook here
Implementing AppFog

Implementing AppFog — Save 50%

An effective, hands-on guide on deploying applications to the cloud using the AppFog service with this book and ebook.

$17.99    $9.00
by Isaac Rabinovitch Matthew Nohr | November 2013 | Web Services

In this article by Matthew Nohr and Isaac Robinovitch, we will learn how to create an application using Appfog. The AppFog console is a very useful tool, but you can also create applications completely from the command line. In this article, we will create a simple application directly from the command line and publish it as a new application to AppFog.

(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: matt@somecompany.com

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:


Implementing AppFog An effective, hands-on guide on deploying applications to the cloud using the AppFog service with this book and ebook.
Published: November 2013
eBook Price: $17.99
Book Price: $29.99
See more
Select your format and quantity:

About the Author :


Isaac Rabinovitch

Isaac Rabinovitch is a freelance technical writer in Portland, Oregon. He's worked with computers since the days they filled whole rooms and had hundreds of blinking lights. He's written documentation for programmers, system administrators, data center personnel, and ordinary people. His former employees include Sun, SGI, Borland, and Zilog. He's the co-author of the Java Tutorial, 4th edition. Read more about him at picknit.com.

Matthew Nohr

Matthew Nohr is a software developer with over ten years of experience in creating large scale applications. He has worked on a range of projects from enterprise Java desktop applications to commercial Java server-based products to cloud-based Groovy/Grails services. Matthew is a certified Scrum Master and has a Masters degree in Software Engineering.

Books From Packt


Instant AppFog [Instant]
Instant AppFog [Instant]

RESTful PHP Web Services
RESTful PHP Web Services

Learning FuelPHP for Effective PHP Development
Learning FuelPHP for Effective PHP Development

Instant Node.js Starter [Instant]
Instant Node.js Starter [Instant]

Instant CakePHP Starter [Instant]
Instant CakePHP Starter [Instant]

CoffeeScript Programming with jQuery, Rails, and Node.js
CoffeeScript Programming with jQuery, Rails, and Node.js

OpenCV 2 Computer Vision Application Programming Cookbook
OpenCV 2 Computer Vision Application Programming Cookbook

Using Node.js for UI Testing
Using Node.js for UI Testing

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software