Skip to content

gastonschabas/rumble-on-scala

Repository files navigation

Rumble On Scala

just a project to experiment with different things around scala ecosystem and other useful tools to build a project.

Coverage Continious Delivery Create Release Latest Release By Date Scala Steward badge Docker Pulls Codacy Badge

Table of contents

Technology Stack Used

  • Scala Icon Scala: The Scala Programming Language combines object-oriented and functional programming in one concise, high-level language. Scala's static types help avoid bugs in complex applications, and its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries.

  • Play Framework: Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

  • Slick Icon Slick: Slick ("Scala Language-Integrated Connection Kit") is Lightbend’s Functional Relational Mapping (FRM) library for Scala that makes it easy to work with relational databases. It allows you to work with stored data almost as if you were using Scala collections while at the same time giving you full control over when database access happens and which data is transferred. You can also use SQL directly. Execution of database actions is done asynchronously, making Slick a perfect fit for your reactive applications based on Play and Akka.

  • Scala Test: ScalaTest is designed to increaste your team's productivity through simple, clear tests and executable specifications that improve both code and communication.

  • Testcontainers: Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

  • sbt-native-packager: SBT native packager lets you build application packages in native formats and offers different archetypes for common configurations, such as simple Java apps or server applications.

  • random-data-generator: A library to generate random data for test purposes, using ScalaCheck and scalacheck-shapeless.

Topics Experimented

Unit Test

  • ScalaTest: is designed to increase your team's productivity through simple, clear tests and executable specifications that improve both code and communication.

Integration Test

Static code analyser

  • ScalaFmt: Formatter tool that can check is your code is well formatted, also you can use it to auto format your code instead of just checking.

  • Wart Remover: WartRemover takes the pain out of writing scala by removing some of the language’s nastier features. Its main goal is to help you write safe and correct software without having to constantly double-check yourself.

  • Scalastyle: Scalastyle examines your Scala code and indicates potential problems with it. If you have come across Checkstyle for Java, then you’ll have a good idea what scalastyle is. Except that it’s for Scala obviously.

  • Sonarcloud: SonarCloud is a cloud-based code analysis service designed to detect code quality issues in 25 different programming languages, continuously ensuring the maintainability, reliability and security of your code. Uploaded reports can be found here

CI/CD

  • Github Action: Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you'd like, including CI/CD, and combine actions in a completely customized workflow.

    • Workflows:
      • Continuous Integration: will be triggered when a PR is opened against master. Unit tests, Integration tests and static code analyser will be executed, and the generated reports will be sent to sonarcloud.
      • Continuous Delivery: it will be triggered each time new code is pushed to master. Unit tests, Integration tests and static code analyser will be executed, and the generated reports will be sent to sonarcloud. Once all the steps mentioned before were executed, it will bump the project version and publish a tag.
      • Create Release: each time a tag is published a docker image is publish to the following docker hub repo https://hub.docker.com/repository/docker/gastonschabas/rumble-on-scala, which means the image can be pulled to be executed in an environment that can run docker containers.
      • Scala Steward: Scala Steward is a bot that helps you keep scala library dependencies and sbt plugins up-to-date. This workflow is scheduled to be triggered each Saturday at 9AM.

Build & Publish Local Docker Image

sbt-native-packager have a couple of tasks and one of them is to build and publish a local docker image. This can be done running the following command sbt docker:publishLocal.

Run in local environment

Using docker-compose

Before start the app and db using docker-compose command, a Dockerfile must be generated using sbt docker:stage. With the following command the app and db can be started:

docker-compose --env-file .env up -d

Start app and db using docker

PostgreSQL Container

A PostgreSQL database must be running to start the app. With the following command, a docker container with the PostgreSQL database can be started:

docker run --name rumble-on-scala-postgres \
            -e POSTGRES_PASSWORD=postgre \
            -e POSTGRES_USER=postgre \
            -e POSTGRES_DB=postgre \
            -p 5432:5432 \
            -d postgres:12.0-alpine

API Container

The following environment variables must be passed as parameters:

  • PLAY_SECRET_KEY: When started in prod mode, if Play finds that the secret is not set, or if it is set to changeme, Play will throw an error.
  • JDBC_DATABASE_URL: jdbc url to a postgres database
  • JDBC_DATABASE_USERNAME: username to access postgres database
  • JDBC_DATABASE_PASSWORD: password to access postgres database

The image has port 9000 exposed, so it must be mapped to be accessed from the outside. The command to run the docker image in a container would be something like this:

docker run --name rumble-on-scala-API \
            -e PLAY_SECRET_KEY="$(head -c 32 /dev/urandom | base64)" \
            -e JDBC_DATABASE_URL="jdbc:postgresql://172.17.0.2:5432/postgres" \
            -e JDBC_DATABASE_USERNAME=postgre -e JDBC_DATABASE_PASSWORD=postgre \
            -p 9000:9000 \
            -d gastonschabas/rumble-on-scala:0.0.0

API Spec

/v0/hello

Accepted Headers

  • Accept-Language: ${lang} # available languages: es, en, de, fr

Curl Request

curl --request GET \
     --header 'Accept-Language: es' \
    'localhost:9000/v0/hello'