This code base is used in a talk outlining ADTs and Typeclasses.
Install Java 8, for example from Oracle.
Install the sbt build tool for Scala as described in the reference manual.
Then run the following from your terminal/shell:
sbt run
This will start the project on http://localhost:8080/
First time you run it, libraries will be downloaded from the internet....
These examples use HTTPie to call the server:
-
List all customers:
http :8080/customers
(with cURL this would becurl http://localhost:8080/customers
) -
List a customer:
http :8080/customers/2
-
Add a customer:
http POST :8080/customers name=Connie phone='+1 555 9876'
-
Update a customer:
http PATCH :8080/customers/2 phone="+44 555 555" name="Robert"
api.scala
defines the REST endpoints.database.scala
defines all the queries and establishes an in-memory database.model.scala
gives the data model.main.scala
pulls everything together into a web server.
Also:
type-class.scala
contains the code used in the presentation for building up the idea of a type class.
This code base makes use of:
- circe to encode and decode JSON text into case classes.
- doobie for database access.
- http4s as a web server.
It makes use of the types Stream
and Task
, which are concurrency concepts.
These are from the fs2 project. For example, Task
describes an effect.
The effect isn't performed until one of the unsafeRun
methods is called.
The http4s web server expects Task
to be used, and arranges for them to be run
on an appropriate thread pool.
This is in part a mash-up of parts from Typelevel todomvc, http4s todomvc, and Bulletin