Applications have come such a long way that nowadays they help us in both working and daily routine tasks. Today we are going to talk about Scalatra, a tiny open-source web micro-framework, written in Scala. It’s most appropriate for construction of RESTful APIs, building high-performance web sites and mobile applications. Being extremely quick and simple, this framework could be considered as one of the best choices for beginners to learn Scala. At the same time, Scalatra is powerful enough to accomplish almost any widely required task thanks to the attachable libraries, which can greatly expand this tool’s basic possibilities.
How you can benefit with Scalatra:
- fast and easy development of high-performance, scalable and mobile applications
- entire server-side equivalent to in-browser client development frameworks such as backbone.js, ember.js or angular.js
- possibility to use available cores of the new 16-core server
- access to Scala’s new concurrency-management forms
So, let’s learn how to create and deploy an application into Jelastic Cloud with the help of Scalatra.
1. To start with, log in to your Jelastic account and click the New environment button in the upper left corner of the dashboard.
2. In the opened environment wizard, choose the Tomcat 7 application server within the Java tab. Define the required amount of resources using the Vertical scaling section, select the preferred environment region and name your environment (e.g. scalatra).
After all the rest of parameters are configured (if needed), click on the Create button at the bottom right corner of the frame.
3. Wait a minute for your environment to be created.
Installing the Required Utilities
For the further operations to be performed, you need to access your account via the Jelastic SSH Gateway, select the appropriate environment (scalatra in our case) and enter your Tomcat 7 container.
1. First of all, after connecting to the shell, add path to the Tomcat /bin folder to the corresponding variable for the comprised scripts to be easily executable:
2. Now let’s install a few necessary utilities:
- Conscript represents a plugin, required for building and updating Scala code. Execute the following command in order to download and install it:
curl https://raw.githubusercontent.com/n8han/conscript/master/setup.sh | sh
- giter8 is a tool, intended for generating the required project structure (i.e. the hierarchy of directories and files inside) based on project template, fetched from the appropriate Git repo. Get it downloaded using the previously installed Conscript utility:
3. Once these processes are completed, navigate to the bin folder and download the sbt-launch java-archive. It will install the sbtbuild tool, which represents an interactive shell for the scala-based applications’ building:
4. Now create a new sbt file here (using, for example, the vim sbt command) and paste the following lines to it:
SBT_OPTS=”-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M” java $SBT_OPTS -jar `dirname $0`/sbt-launch.jar “$@”
When everything is done, don’t forget to save the changes with typing :wq.
5. The next steps are to set the just added script to be executable by the current user with the chmod command, download and unpack an archive with an example app, and remove its zipped version for not occupying the extra disk space:
chmod u+x sbt
wget https://github.com/scalatra/scalatra-website-examples/archive/master.zip; unzip master.zip; rm master.zip
6. Then, switch to the folder with the Jelastic-dedicated project example and open the comprised build.sbt file (which represents a special Jelastic deployment plugin) for editing:
7. While inside, substitute the JelasticKeys.apiHoster parameter’s value with your hosting service provider’s domain (e.g.app.jelastic.com) and specify your environment’s name within the JelasticKeys.deploy parameter (scalatra in our case).
Save the changes and quit from the vim editor with the :wq command.
Build and Deploy an App
Now, as everything is configured, you can proceed to the actual deployment of your project just from the interactive shell.
1. For that, set sbt file to be executable (by means of the chmod command as shown below) and then call this tool to be started:
chmod +x sbt
On the first run, all the required dependencies are to be downloaded, so be patient – it may take some time.
2. Now you can make your project to be built and deployed directly to the Jelastic platform through simply typing the next command inside the shell:
Through the output, you can track how the project and its libraries are uploading to the Jelastic dashboard and deploying to the earlier specified environment.
When everything is done, you’ll be shown the corresponding success message with the process duration specified.
3. Now, enter the Jelastic dashboard (or refresh the page if you have it already opened) and click Open in browser for your environment to see what we’ve got:
Great! As a result, you’ve just deployed your first example Scalatra app into the Jelastic platform.
It was pretty simple, wasn’t it? So, for now you can have fun and learn Scalatra further to create your own stunning apps and get them hosted directly in the Cloud within a few commands! Go ahead and test it by yourself – simply sign up for a 2-week-free-trial and share your experience in the comments below.