My Adventures in Coding

September 29, 2011

Scala – Hello World REST API with Scalatra and Simple Build Tool

Filed under: REST,Scala,Simple Build Tool — Brian @ 9:00 pm
Tags: , ,

I am new to Simple Build Tool and also to building REST APIs in Scala. Mostly I have been using Python with Bottle for REST APIs up until this point. For our current project our REST API will be written in Scala, so I wanted to try out Scalatra. They have excellent documentation and examples, however they assume you have a good understanding of Simple Build Tool (known as sbt or xsbt). I will walk you through running the Scalatra example with Simple Build Tool, assuming you are using it for the first time.

The example we are using is the main example from Scalatra’s website http://www.scalatra.org/.

Get the example application

Clone the Scalatra Hello World REST API example application using git.

git clone http://github.com/scalatra/scalatra-sbt-prototype.git

Download Simple Build Tool

Download version 10 (used for the Scalatra example) of Simple Build Tool into the “scalatra-sbt-prototype” directory.

cd scalatra-sbt-prototype
wget http://typesafe.artifactoryonline.com/typesafe/ivy-releases/org.scala-tools.sbt/sbt-launch/0.10.0/sbt-launch.jar

NOTE: If you use version 0.11.0 of Simple Build Tool with this example you will get the error: “sbt.ResolveException: unresolved dependency: com.github.siasia#xsbt-web-plugin_2.9.1;0.1.0-0.11.0: not found”.

Create a script to run Simple Build Tool

In the “scalatra-sbt-prototype” directory create a file called “sbt.sh” containing the following text:

java -Xmx512M -XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch.jar "$@"

Also, make sure the script is executable:

chmod 755 sbt.sh

NOTE: setting MaxPermSize is important. This example will sometimes throw a “Error during sbt execution: java.lang.OutOfMemoryError: PermGen space” error if the PermGen memory size has not been increased.

Run the example

In the “scalatra-sbt-prototype” folder execute the sbt script:

./sbt.sh

Once the Simple Build Tool prompt has opened, you can now start the web app which uses Jetty:

jetty-run

Confirm it is working

If everything went as planned, you should be able to navigate to http://localhost:8080/ and see “Hello World”.

That’s all!

September 11, 2011

Python – Upgrading Python with easy_install, pip, and virtualenv on a Mac

Filed under: Mac,Python — Brian @ 1:45 pm
Tags: , ,

For this tutorial I am upgrading from python 2.6 to python 2.7 on my Mac. Every time I have to upgrade Python versions, it always takes me a few minutes to remember the steps since I do this task so rarely. So here is a simple set of instructions that seem to work for me!

Install python 2.7

  • Download the Python Mac dmg file from the Python Downloads page
    Just open the dmg and follow the installer as you would with any application
  • You should now have Python 2.7 installed in:
    /Library/Frameworks/Python.framework/Versions/2.7/bin/python

NOTE: Even though we have installed python 2.7, your /usr/bin/python is still pointing to an older version of Python, so for example if you run easy_install, if will run from the older version of Python, and install packages into the library of the old Python installation.

So, on to step two of the install:
Let’s switch the /usr/bin/python link to point to the newest version of Python, and at the same time let’s create a soft link in the Python install folder that always points to the current install of Python. So the next time we upgrade, we will only need to change the “Current” version link.

#Create a soft link in /Library/Frameworks/Python.framework/Versions/
cd /Library/Frameworks/Python.framework/Versions/
ln -s 2.7 Current

#Switch the /usr/bin/python link to point to current python link
cd /usr/bin
rm -f python
ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/python python

Install Setup Tools (this includes easy_install)

Download the egg file for your version of python (for this example, 2.7) from the Setup Tools Downloads page. To install setup tools just run the egg file like any other shell script:

sudo sh setuptools-0.6c11-py2.7.egg

Install pip (Using easy_install)

Open a new Terminal window session. You will need a new terminal session for easy_install to show up on the path.

sudo easy_install pip

Now we can install any python packages we want using pip!

Install Virtualenv (Using pip)

Now that we have pip, lets install a very important package called virtualenv!

pip install virtualenv

Thats it!

Now you will have Python 2.7 on your Mac, with easy_install, pip, and virtualenv ready to go.

Create a free website or blog at WordPress.com.