Skip to content

theiterators/pekko-http-microservice

 
 

Repository files navigation

Pekko HTTP microservice example

GitHub license Build Status

This repository is a fork of akka-http-microservice, migrated to Apache Pekko and Scala 3.

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

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

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

For a more thorough explanation, check out the tutorial.

Usage

Start the service 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:

Lukasz Sowa lukasz@iteratorshq.com from Iterators.

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

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Scala 100.0%