Monday, December 23, 2019

Scala Mongo example with akka-http and akka-stream

Introduction

In this tutorial we shall create and CRUD web application in Scala. We will use MongoDB Scala Driver to communicate to the mongo database. It is officially supported Scala driver for MongoDB. Along with that we shall create a Rest API using akka-http module and akka-streams.

Prerequisites

  • Scala version: 2.13.1
  • Database: Mongo:3.4.23-xenial

Other dependencies - 


About the service/project

Application should be able to do CRUD operations on the in mongodb document. This application should also provide Rest apis to access and modify the data. So it is an employee management. We have employee details in a document called `employee`. We will create reset services to create, update and search the employee data.

Mongo Configuration

Codec Registry

Mongo documents required the codec information for marshal and un-marshal the data. This codecs are already written and available here. This repository have codec implementations for most of the available types. So first create the the java codec as below -

In the above code `Employee` is our mongo document class with the following details. 

Document Class

Employee document contains employee name and date of birth.

Database Details

`MongoClientSettings` is the companion object to configure the mongodb settings. 

In the above snippet; An Mongo client settings object is created that contains the configuration details for the connecting mongo database.

Collection Details

Collection details can be obtained from the database representation we get the previous step

This above step required for every collection to get the data.

JSON Support

We need formatter to format the date type to and from JSON format. We used Spray JSON here for this. Spray JSON providing automatic to and from JSON marshalling/un-marshalling using an in-scope *spray-json* protocol.

Repository

Repository contains the create, update, delete and search commands for the employee document. 

Actors to get the user data 

Employee actor received messages to perform different operation on the employee document. Actor shall call the actual service and then send the responds to the sender.

Routing Configuration



#1
GET /api/employee/create
To create employee data 
#2 
POST /api/employee/search
To search employee data
#3
PUT /api/employee/update
To update employee data
#4
DELETE /api/employee/delete
To delete user data

The routing code should look like this-


Web Server

This class is used to create http server and bind the endpoints with the http server. You can get more information in my previous blog.

Run Application

Start the mongodb docker container 

`docker-compose up -d`

Compile the code using below command

`sbt compile`
Run the application
`sbt run`
Now go to terminal, and run below https://httpie.org/ commands 

Create Employee Data

Search Employee Data

Update Employee Data

Check current data in mongodb

Update the data by id 

Search data after update 



Delete Employee data

Search employee data

Delete employee data using id

Search data after delete

Github links

Full code is available here to to explore and fork. Feel free to do whatever you want. ;)

Conclusion

We have seen here how to use akka http and streams to create rest services in scala. You can get more information here - 

Tuesday, December 10, 2019

Scala Rest API with akka-http

Introduction


In this tutorial we shall explore how we can use akka-http to create service with scala. Here we create CRUD service with akka actor and try to see how it work. 

Versions

Scala version: 2.13.1

SBT version: 1.3.4

Other dependencies - 

About the service/project

We’ll create two actors that communicate with each other to get the user data and user activity data. 

Data Classes

We need two data classes that keeps the user data and user activity data.

Repository

Repositories are used to fetch the data from the actual source of data. The actual source can be anything like another service or some database. We created `UserActivityRepository` that have only one method `queryHistoricalActivities` to fetch the user activity by `userId`.

Actors to get user data 

We need two actor classes for `UserDataActor` and `UserActivityActor`. `UserDataActor` received 4 types of methods `Get`, `Post`,`Put` and `Delete` to retrieve, create, update and delete the user data respectively.  Once a message received, User data will be sent to the sender asynchronously.


`UserActivityActor` retrieve the user active once it receives the message `Get` 

Routing Configuration

Now, we need to create routing details, to access the user details from rest endpoints. We needs following endpoints 


#1
GET /api/users/activity
Retrieve user and its activity details.
#2 
POST /api/user
To create user data
#3
PUT /api/user
To update user data
#4
DELETE /api/user
To delete user data


Routing code looks like below -


We have two `implicit` parameters in the routing config `implicit val userDataActorRef: ActorRef` and `implicit val system: ActorSystem`. `userDataActorRef` is used to get the user data. Since user details is required to register `UserActivityActor` we use actor system and implicit parameter to register `UserActivityActor` when we have user information available.


Web Server
This class is used to create http server and bind the endpoints with the http server. 


Run Application

Compile the code using below command

sbt compile

Run the application

sbt run

Now go to terminal, and run below httpie scripts 

Output of testing endpoints

Github links

Full code is available here to to explore and fork. Feel free to do whatever you want. ;)

Conclusion

We have seen here it’s easy to create rest services with akka and scala. You can get more information at
akka-http official document