Code Monkey home page Code Monkey logo

todo-http4s-doobie's Introduction

todo-http4s-doobie

A sample project of a microservice using http4s, doobie, and circe.

The microservice allows CRUD of todo items with a description and an importance (high, medium, low).

End points

The end points are:

Method Url Description
GET /todos Returns all todos.
GET /todos/{id} Returns the todo for the specified id, 404 when no todo present with this id.
POST /todos Creates a todo, give as body JSON with the description and importance, returns a 201 with the created todo.
PUT /todos/{id} Updates an existing todo, give as body JSON with the description and importance, returns a 200 with the updated todo when a todo is present with the specified id, 404 otherwise.
DELETE /todos/{id} Deletes the todo with the specified todo, 404 when no todo present with this id.

Here are some examples on how to use the microservice with curl, assuming it runs on the default port 8080:

Create a todo: curl -X POST --header "Content-Type: application/json" --data '{"description": "my todo", "importance": "high"}' http://localhost:8080/todos

Get all todos: curl http://localhost:8080/todos

Get a single todo (assuming the id of the todo is 1): curl http://localhost:8080/todos/1

Update a todo (assuming the id of the todo is 1): curl -X PUT --header "Content-Type: application/json" --data '{"description": "my todo", "importance": "low"}' http://localhost:8080/todos/1

Delete a todo (assuming the id of the todo is 1): curl -X DELETE http://localhost:8080/todos/1

http4s

http4s is used as the HTTP layer. http4s provides streaming and functional HTTP for Scala. This example project uses cats-effect, but is possible to use http4s with another effect monad.

By using an effect monad, side effects are postponed until the last moment.

http4s uses fs2 for streaming. This allows to return streams in the HTTP layer so the response doesn't need to be generated in memory before sending it to the client.

In the example project this is done for the GET /todos endpoint.

doobie

doobie is used to connect to the database. This is a pure functional JDBC layer for Scala. This example project uses cats-effect in combination with doobie, but doobie can use another effect monad.

Because both http4s and doobie use an effect monad, the combination is still pure and functional.

circe

circe is the recommended JSON library to use with http4s. It provides automatic derivation of JSON Encoders and Decoders.

Configuration

pureconfig is used to read the configuration file application.conf. This library allows reading a configuration into well typed objects.

Database

h2 is used as a database. This is an in memory database, so when stopping the application, the state of the microservice is lost.

Using Flyway the database migrations are performed when starting the server.

Tests

This example project contains both unit tests, which mock the repository that accesses the database, and integration tests that use the http4s HTTP client to perform actual requests.

Running

You can run the microservice with sbt run. By default it listens to port number 8080, you can change this in the application.conf.

todo-http4s-doobie's People

Contributors

jaspervz avatar lemastero avatar saurabh-maheshwari 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

todo-http4s-doobie's Issues

How to run?

Hi @jaspervz ; I just saw your post about this on LinkedIn and was interested. Nice work!

No front end is included for your API in order to try it out. That wouldn't fit the microservices approach, so I get that.

Question

But could you describe - in the README.md - an example how to test this? For instance how to make a JSON post request to add a todo. Since at the start it seems empty, there are no test todo's; e.g. I get [] back from /todos. Or you could seed the H2 database to have test data.

Or perhaps there is something wrong with running h2? I'm not familiar with this, how can I test this separately?

My attempts

I tried to add a todo with a POST request via POSTman as you can see in the following screenshot. After some trial and error I didn't get an error back anymore, but still it seems like no todo was added. Or is there still a bug? Or is the

todo-http4s-doobie

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.