Coder Social home page Coder Social logo

akka-http-rest's Introduction

Akka Slick REST service template

Goal of example is to show how create reactive REST services on Lightbend stack with Akka and Slick.

Example contains complete REST service for entity interaction.

Features:

  • CRUD operations
  • Entity partial updates
  • CORS support
  • Authentication with JWT tokens
  • Test coverage with ScalaTest
  • Migrations with FlyWay
  • Ready for Docker
  • Testing with in-memory postgres instance that launch automatically
  • HikaryCP as connection pool

Requirements

Development guide

This application is fully tested with Unit and IT tests. You don't need to launch server locally for development. My recommendation is to write a test before changes and work via TDD. To ensure that application working properly, you should run it: sbt test.

Structure

All business logic is located in core package, every package inside is related to some domain. Service classes contains high level logic that related to data manipulation, that means that service MUST NOT implement storaging and querying for the data. For storaging there are Storage classes that always have interface with two implementation, production one and in-memory one. That's needed to fasten tests of services and make it independent from each other.

Code formatting

There are Scalafmt integrated to the project. Its a opinionated code formatter that formats a code automatically instead of you. To use it, please run sbt scalafmt before commit or enable format on save in IntelijIdea (should be available in other editors too).

Checking code coverage

To generate code coverage report, please run: sbt clean coverage test coverageReport. Then you will have HTML pages with reports in /target/scala-2.12/scoverage-report

Packaging

Application packaging implemented via sbt-native-packager plugin. Currently in build.sbt enabled two types: docker and universal.

Universal packager
To package application as a universal app, use: sbt universal:packageBin. Application zip archive will be generated in /target/universal/ folder.

Docker packager
To package application as docker image, use sbt docker:publishLocal. It will generate and push application image into your local docker store. For information about publishing to external store, please, read plugin documentation.

Running

If you want to launch application locally (its not recommended) you need to start Postgres instance locally and fulfill some env variables:

  • JDBC_URL - url to your database
  • JDBC_USER - database username
  • JDBC_PASSWORD - database password

After that, just run sbt run and enjoy hacking. For better expirience you can use sbt reStart that will give you ability to restart application without restarting of sbt.

Deployment on production

Easiest way to deliver your application, is to do it with docker. Publish image into the store and then use docker-compose file with structure like in docker-compose.yml.

Live example

Application deployed on heroku and can be accessed by URL http://akka-http-rest.herokuapp.com/.
First request can take some time, because heroku launch up project.
You can see documentation for this example on Apiary.

Copyright

Copyright (C) 2017 Arthur Kushka.
Distributed under the MIT License.

Contact

Wanna ask me something or stay in touch?
Follow my Twitter @arhelmus

akka-http-rest's People

Contributors

arhelmus avatar ktoso avatar linno60 avatar oviron avatar xavier-fernandez avatar zxiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

akka-http-rest's Issues

503 Application error.

I was just trying to go through the code and clicked on the link for live example. It is giving 503 "Application Error". I am facing the same issue on my akka http app on Heroku. Do you know what it might be? Logs are not showing much info on it.

How to fix Slick Exception of single AutoInc column returned on INSERT

I tried to implement the akka-http rest example provided at
https://github.com/ArchDev/akka-http-rest
but I'm stuck with the

    slick.SlickException: This DBMS allows only a single column to be returned from an INSERT, and that column must be an AutoInc column.
    at slick.jdbc.JdbcStatementBuilderComponent$JdbcCompiledInsert.buildReturnColumns(JdbcStatementBuilderComponent.scala:67)

Here is the Scala Code:
Signup API:

path("signUp") {
    pathEndOrSingleSlash {
      post {
        entity(as[UsernamePasswordEmail]) { userEntity =>
          complete(Created -> signUp(userEntity.username, userEntity.email, userEntity.password))
        }
      }
    }
  }

AuthService.scala

def signUp(login: String, email: String, password: String): Future[AuthToken] =
    authDataStorage
      .saveAuthData(AuthData(UUID.randomUUID().toString, login, email, password.sha256.hex))
      .map(authData => encodeToken(authData.id))

AuthDataStorage.scala

...
override def saveAuthData(authData: AuthData): Future[AuthData] =
    db.run((auth returning auth).insertOrUpdate(authData)).map(_ => authData)
...

Since I'm new to Scala and Slick, can anyway provide the information why this exception is occurring even though I've defined O.AutoInc in Model. I'm using MySQL RDBMS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.