Brief tutorial on how to put together a Web Application using SBT and Scala. I want to introduce a new project called Scalens to perform News Scrapping from several brazilian News websites.


I actually like to have small projects to motivate me to work on something new. I really want to learn Scala better and I am trying to get involved with the language as much as possible.

Due to the conditions of my tech stack at work, we are probably developing everything in Python there. However, I want to build a Scala Rest Webservice. So, let's get into the main idea for the project.

Imagine you want to keep safe all the news you read from some major Brazilian websites such as Folha de São Paulo or Estadão. I find myself several times incapable of reading something and always don't know where are the data I have saved.

So now, I wish to provide some services through an API to solve this issues (probably only mine, but who knows!).

  1. The first service for the MVP of my product will be **Receiving a LINK as POST route and return an JSON file with all the news decoded and ready to store**
  2. Store this inside a database

Get an Web APP running using Scala

I already read a lot about the Play Framework and there are plenty of material out there, let's get into it.

First, use the SBT template:

sbt new playframework/play-scala-seed.g8

This will ask you for a project name and them you will be able to run your application.

Go inside the folder and type:

sbt run

Wait until all the compilation is done and them go to http://localhost:9000.

You should see an:

Welcome to Play

Congrats! Your app is up and running!.

This is how your project should looks like.

├── app
│   ├── controllers
│   └── views
├── conf
├── logs
├── project
│   ├── project
│   └── target
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
├── target
│   ├── scala-2.12
│   ├── streams
│   └── web
└── test
    └── controllers

Now go to the conf folder and open the file routes.

# Routes
# This file defines all application routes (Higher priority routes first)

GET     /                           controllers.HomeController.index

You should see here what method is being called when someone hits your root url. Let's see this file. Go to app/controllers and open the HomeController file.

Let's make some modifications on the index() method:

def index() = Action { implicit request: Request[AnyContent] =>
  val returnVal: Result = Ok("Welcome to Scalens Project!!!")

Now go to http://localhost:9000 and you will see the modifications.

How to set new methods

The only thing you need to do right now is to setup new routes on your routes file and target it to the right method that will execute the call when someone hits the predefined route tag.

Deploy your APP

There are several options varying from creating an DEBIAN package, register to Maven repository or simply running your APP in production.

If you stick to SBT the deploy is extremely simple. First, run:

sbt clean stage

This command will clean, compile, retrieve the required dependencies and copies everything to the target/universal/stage directory. It also creates a /bin/<projectName> script. For windows it also creates a bat file.

So now you need to run the script created:

target/universal/stage/bin/scalens -Dplay.http.secret.key=YOURPASSWD

You can find more about the application secret here, but this password will control the ability to run your service so it's not a good practice to have this password under version control.

The Play framework even provides an utility to create those keys:



Alright, the idea is to keep it direct to the point. The web app was created, however there are several factors not in place yet as security and of course our business logic.

However, this is good enough for the first post about this project.

Let's see if I have the time needed to keep this going. There is already some value on the settings for WebApps in Scala here.