Giter Club home page Giter Club logo

servant-persistent's Introduction

servant-persistent

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.

Requirements:

You will need PostgreSQL installed and listening on port 5432. The default configuration uses a database name perservant with username/password test:test.

The API:

  • 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.

src/Main.hs

main starts off by pulling some settings from the environment, creating a connection pool, running the migrations, and finally running the app.

src/Api.hs

This source contains the actual API definition.

src/Config.hs

Contains the runDb, makePool, and Config definitions.

src/Models.hs

Contains fairly typical Persistent schema definitions.

servant-persistent's People

Contributors

parsonsmatt avatar

Stargazers

Levi Notik avatar

Watchers

Dennis Gosnell avatar James Cloos avatar  avatar

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.