Pages

Saturday, July 6, 2013

Erlang Web Application: Getting Started

Lately I've been getting back into building my own web application. It started with some MongoDB which led to Python and Bottle through this video to Apache to Yaws and Erlang.  I've finally settled on working through some of the examples included in the O'Reily book Building Web Applications with Erlang.

It's been a lot of installation and figuring out what goes where, but I'm starting to see some cool stuff. I really like the idea of running a web application through Yaws. Erlang has always made a lot of sense to me with it's light-weight application threads and message passing.

More Details

I didn't do anything special from the standard way of installing packages in Ubuntu. I don't think there's anything special other than
steve@ubuntu$ sudo apt-get install yaws
This should install all the dependencies, including Erlang which you'd need to get started. The yaws service is started, using 
/usr/share/yaws
  as a home directory for the server. Starting/stopping/restarting the service is as easy as:

steve@ubuntu$ sudo service yaws <command>

 First Examples

  I just have a couple very simple examples right now. Hopefully more to come shortly. One thing I've found is that if you modify index.html in the server's home directory, changes can be seen by visiting localhost:8080/index.html but not simply localhost:8080 until you perform a server restart (or maybe reload). Most likely due to file handles of the server. Fortunately, creating examples and running <erl> tags within new files works because you specify the URL for the files explicitly, so you don't have to restart the server between every step.

First output

The first example was straight from the book. Creating method.yaws in the server home directory:
<erl>
method(Arg) ->
  Rec = Arg#arg.req,
  Rec#http_request.method.

out(Arg) ->
  {ehtml, f("Method: ~s" , [method(Arg)])}.

</erl>
Then  browsing to localhost:8080/method.yaws shows that you used a get request.

 Simple Redirect

The next simple example is to redirect to another page:
<erl>
method(Arg) ->
  Rec = Arg#arg.req,
  Rec#http_request.method.

out(Arg) ->
  {ehtml, f("Method: ~s" , [method(Arg)])}.

</erl>
This simply redirects to the method.yaws example above.
 

Saturday, September 22, 2012

Standalone Java Application

I've been using java for years, but I've never really used it to create a standalone application that actually did something. In school we were spoon fed projects to work on. At work I add to or fix the current java web applications. What I really want is to write programs that do what I want them to do. But what is that? I've tried asking others what would be helpful, but without real passion for what needs are out there, I'm not going to get anything done. It's time to start coding and let the purpose follow. My current goal is to start coding some helpful utility applications, so when I have a real purpose I'll have the means to jump right in. So skipping command line execution of plain class files... I started with creating Executable Jar files.


Requirements for the Executable .jar

Class files
Something with a main method to run. I'll be starting with the quintessential 'Hello World' project.
Manifest file
The manifest tells the jvm what you want the point of entry to be for the jar.

Hello World!

My little java class lives in the demo package, and prints "Hello World!" and then lists the arguments passed in.
package demo;
public class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello World!");
for (String each : args)
System.out.println (each);
}
}
Compile the classfile (commandLine$ javac HelloWorld.java) and you're good to go.

Manifest.txt

This file is just one line, and ends with a newline
Main-Class: demo.HelloWorld

Putting it all together

Here's what I have so far

~/MANIFEST.txt
~/demo/
~/demo/HelloWorld.java
~/demo/HelloWorld.class
 The .java file isn't really necessary from this point forward, but if you want to go back and look inside your jar for the source code you'll want to keep it around.

Create the .jar

Use the jar utility to package up the jar. cmfv = create, manifest (filename), filename (for jar), verbose

jar cmfv MANIFEST.txt demo.jar demo
added manifest
adding: demo/
adding: demo/HelloWorld.java
adding: demo/MANIFEST.MF
adding: demo/HelloWorld.class

The Result

All said in done, we're left with demo.jar
When you run java -jar demo.jar in the command-line, you get the result below
steve:~/Desktop$ java -jar demo.jar
Hello World!
You can also add extra parameters and they will be printed below
steve:~/Desktop$ java -jar demo.jar what\'s\ up?
Hello World!
what's up?