Servant is an awesome Haskell library for writing web APIs. It uses the type system in a way that can only be described as magic to generate type safe routes as well as clients.
Persistent is another awesome Haskell library for querying databases. It manages migrations, your schema, and querying to make data transactions mostly painless.
For some reason, no one had created an example on how to use these guys together. I put together this minimal example to show an example implementation, along with some resource management and basic error handling.
I wrote a blog post that goes into a bit more detail, as well as having some exercises to work on.
You will need PostgreSQL installed and listening on port 5432. The default configuration uses a database name perservant
with username/password test:test.
- GET
/users
returns a list of all users in the database - GET
/users/:name
returns the first user whose name is:name
, and returns 404 if the user doesn't show up. - POST
/users
with JSON like{ "name": "String", "email": "String" }
to create a User.
main
starts off by pulling some settings from the environment, creating a connection pool, running the migrations, and finally running the app.
This source contains the actual API definition.
Contains the runDb
, makePool
, and Config
definitions.
Contains fairly typical Persistent schema definitions.