Showing posts with label Spray. Show all posts
Showing posts with label Spray. Show all posts

Tuesday, April 15, 2014

Simple Scala REST example using Spray

http://sysgears.com/articles/building-rest-service-with-scala/
https://github.com/oermolaev/simple-scala-rest-example

Enterprise application might be developed using a variety of architectural styles. And REST is one of the most powerful of them. It allows us to build simple, scalable, highly productive APIs with independent components on the basis of widespread standards like HTTP, MIME etc, engaging their true potential.
Let's discuss how to create a lightweight, but full-featured RESTful service from scratch.
Consider building REST [1] service that doesn't contain any complicated functionality, but provide basic CRUD operations and have following HTTP endpoints as an API:
  • POST /customer/ to create Customer.
  • GET /customer// to retrieve specific Customer.
  • PUT /customer// to update specific Customer.
  • DELETE /customer// to delete specific Customer.
  • GET /customer/ to search for Customers with specific parameters.

Technology stack

Scala was chosen as the foundation for the REST service we are going to implement. On the Scala website [2] it is described as a "general purpose programming language designed to express common programming patterns in a concise, elegant and type-safe way. Also, it smoothly integrates features of object-oriented and functional languages."
Despite the fact that Scala is a relatively new language and may have some drawbacks, there are several attractive elements, that made it not just an "another new programming language":
  • Operating on the JVM. It is known fact that Java is de facto the most popular programming language for enterprise. Many libraries are written in Java. A variety of tools are designed for JVM. Environments have been stable for years. It can be rather risky to change the entire programming stack even if this step may provide apparent benefits. But Scala has a great interoperability with existing Java code because it still operates on the JVM, has compatible byte code and allows Scala applications to use most of the JVM libraries. Furthermore, developers are able to apply their existing Java skills after migration. Therefore, Scala could be integrated into enterprise infrastructure quite smoothly.
  • Functional programming. Except that Scala is a pure object-oriented language it provides syntactic sugar for functional programming. Like a pattern matching, anonymous and higher-order functions, carrying, immutable collections, etc.
  • Concise and powerful syntax. Code size is reduced significantly compared to an equivalent application written in Java. This may improve development performance: less key-strokes to make, easier code review and testing. Moreover, many features like function passing and type inference can reduce syntactic overhead.
  • Static typing. Scala is equipped with a rich and balanced type system. It provides compile time constraints that could help to avoid certain erroneous scenarios. On the other hand, a local type inference mechanism allows developers not to annotate the program with redundant type information.
The following software was also used:
  •  SBT (Simple Build Tool) - Build tool for Scala and Java projects. Maven or Gradle with appropriate Scala plug-ins can be used as well, however, SBT became de facto the number one build tool for Scala. It is easy-to-use but quite powerful utility. [3]
  •  Akka - Asynchronous event-driven middleware framework implemented in Scala, for building high performance and reliable distributed applications. Akka decouples business logic from low-level mechanisms such as threads, locks and non-blocking IO. [4]
  •  Spray - Scala framework for building RESTful web services on top of Akka: lightweight, asynchronous, non-blocking, actor-based, modular, testable. [5]
  •  Slick - Database query and access library for Scala. It provides a toolkit to work with stored data almost as using Scala collections. Features an extensible query compiler which can generate code for different backends. [6]
  •  MySQL - Well-known open-source RDBMS. [7]
  •  Lift-json - Parsing and formatting utilities library for JSON. [8]
  •  Logback - Fast and stable logging utility. Considered as a successor to the log4j project. Natively implements the SLF4J API. [9]

Spray + Bootstrap

https://bitbucket.org/diversit/spray-twirl-bootstrap
https://github.com/spray/spray-template

"Spray - Twirl - Twitter Bootstrap" Template Project

This project is based on the Spray Template project. I added support for serving static files, added Twitter Bootstrap files and added Twirl for templating. You can use this project as a template for a REST API or even serve a website.
Spray routing is split up in serveral traits for static routing, twirl pages and inline html.
This project uses Git Flow.
Follow these steps to get started:
  1. Git-clone this repository.
    $ git clone git@bitbucket.org:diversit/spray-twirl-bootstrap.git my-project
    
  2. Change directory into your clone:
    $ cd my-project
    
  3. Launch SBT:
    $ sbt
    
  4. Compile everything and run all tests:
    > test
    
    Note: For the Selenium Chome test to be able to run, you need to install thechromedriver locally. Read my blogpost about how Selenium is setup for ScalaTest. And read the ScalaTest Selenium documentation for using Selenium in ScalaTests.
  5. Start the application:
    > re-start
    
  6. Browse to http://localhost:8080/
  7. Start the application:
    > re-stop
    
  8. Start hacking on src/main/scala/com/example/MyService.scala



Spray Project
This projects provides a starting point for your own spray-routing endeavors. There are 8 branches, providing templates for spray-routing on
  • spray-can, Scala 2.9 + Akka 2.0 + spray 1.0 (the on_spray-can_1.0 branch)
  • spray-can, Scala 2.10 + Akka 2.1 + spray 1.1 (the on_spray-can_1.1 branch)
  • spray-can, Scala 2.10 + Akka 2.2 + spray 1.2 (the on_spray-can_1.2 branch)
  • spray-can, Scala 2.10 + Akka 2.3 + spray 1.3 (the on_spray-can_1.3 branch)
  • Jetty, Scala 2.9 + Akka 2.0 + spray 1.0 (the on_jetty_1.0 branch)
  • Jetty, Scala 2.10 + Akka 2.1 + spray 1.1 (the on_jetty_1.1 branch)
  • Jetty, Scala 2.10 + Akka 2.2 + spray 1.2 (the on_jetty_1.2 branch)
  • Jetty, Scala 2.10 + Akka 2.3 + spray 1.3 (the on_jetty_1.3 branch)
You are currently on the on_spray-can_1.1 branch.
> git clone git://github.com/spray/spray-template.git my-project -b on_spray-can_1.3