• Stars
    star
    750
  • Rank 60,494 (Top 2 %)
  • Language
    Scala
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Example of http (micro)service in Scala & akka-http

Akka HTTP microservice example

GitHub license Build Status

Due to recent Akka's license change, a fork named Pekko was created under the Apache Foundation. Starting a new project, you should consider using Pekko HTTP. There's a sibling open source template we've created named pekko-http-microservice.

This project demonstrates the Akka HTTP library and Scala to write a simple REST (micro)service. The project shows the following tasks that are typical for most Akka HTTP-based projects:

  • starting standalone HTTP server,
  • handling file-based configuration,
  • logging,
  • routing,
  • deconstructing requests,
  • unmarshalling JSON entities to Scala's case classes,
  • marshaling Scala's case classes to JSON responses,
  • error handling,
  • issuing requests to external services,
  • testing with mocking of external services.

The service in the template provides two REST endpoints - one which gives GeoIP info for given IP and another for calculating geographical distance between given pair of IPs. The project uses the service ip-api which offers JSON IP and GeoIP REST API for free for non-commercial use.

If you want to read a more thorough explanation, check out tutorial.

Deploy to Heroku Gitpod Ready-to-Code

Usage

Start services with sbt:

$ sbt
> ~reStart

With the service up, you can start sending HTTP requests:

$ curl http://localhost:9000/ip/8.8.8.8
{
  "city": "Mountain View",
  "query": "8.8.8.8",
  "country": "United States",
  "lon": -122.0881,
  "lat": 37.3845
}
$ curl -X POST -H 'Content-Type: application/json' http://localhost:9000/ip -d '{"ip1": "8.8.8.8", "ip2": "93.184.216.34"}'
{
  "distance": 4347.624347494718,
  "ip1Info": {
    "city": "Mountain View",
    "query": "8.8.8.8",
    "country": "United States",
    "lon": -122.0881,
    "lat": 37.3845
  },
  "ip2Info": {
    "city": "Norwell",
    "query": "93.184.216.34",
    "country": "United States",
    "lon": -70.8228,
    "lat": 42.1508
  }
}

Testing

Execute tests using test command:

$ sbt
> test

Author & license

If you have any questions regarding this project, contact:

Łukasz Sowa [email protected] from Iterators.

For licensing info see LICENSE file in project's root directory.

More Repositories

1

reactive-microservices

Project showcasing different microservice communication styles using Scala, Akka, Play and other tools from Scala ecosystem
Scala
242
star
2

kebs

Scala library to eliminate boilerplate
Scala
155
star
3

http4s-stir

http4s-stir offers Pekko HTTP-style (Akka HTTP-style) DSL directives for http4s using cats-effect's IO as an effect system
Scala
33
star
4

sealed-monad

Scala library for nice business logic oriented, for-comprehension-style error handling
Scala
23
star
5

free-monad-on-app-level-code

Scala
19
star
6

java-jwt-wrapper

Scala
10
star
7

flyway-docker

Dockerized flyway command line tool
Dockerfile
8
star
8

baklava

Generate openapi, HTML docs or TypeScript client interface from routing tests
Scala
7
star
9

lambda-days-free-workshop

Scala
7
star
10

kebs-intellij

A companion IntelliJ IDEA plugin for kebs library
Scala
6
star
11

error-benchmarks

Code for the "Benchamrking Functional Error Handlling in Scala" blog post (https://www.iteratorshq.com/blog/benchmarking-functional-error-handling-in-scala)
Scala
5
star
12

cors-buster

Simple, lightweight proxy that bypasses CORS intended for development
Scala
5
star
13

scalar-2017-optics-workshop

Scala
4
star
14

warsjawa-ms-spray

Scala
3
star
15

aglio-docker

Dockerfile for aglio
2
star
16

docker-git-s3cmd

Dockerfile
2
star
17

warsjawa-ms-play-template

Scala
2
star
18

scalawave-2017-optics-workshop

2
star
19

node-git-s3cmd

Docker image with node and s3cmd
Dockerfile
2
star
20

docker-git-sbt

Dockerfile
2
star
21

warsjawa-ms-play

Scala
2
star
22

warsjawa-ms-spray-template

Scala
2
star
23

drakov-docker

Dockerfile for drakov
2
star
24

logstash-jdbc-postgres

Logstash with PostgreSQL support via jdbc
Dockerfile
2
star