An open API service indexing awesome lists of open source software.

https://github.com/nsaunders/purescript-nodetrout

Build a Node HTTP server with Trout.
https://github.com/nsaunders/purescript-nodetrout

Last synced: about 1 year ago
JSON representation

Build a Node HTTP server with Trout.

Awesome Lists containing this project

README

          

# purescript-nodetrout [![build status](https://img.shields.io/travis/nsaunders/purescript-nodetrout.svg)](https://travis-ci.org/nsaunders/purescript-nodetrout) [![purescript-nodetrout on Pursuit](https://pursuit.purescript.org/packages/purescript-nodetrout/badge)](https://pursuit.purescript.org/packages/purescript-nodetrout) [![nodetrout in package-sets](https://img.shields.io/endpoint.svg?url=https://package-sets-badge-zxa7vacp3dju.runkit.sh/nodetrout)](https://github.com/purescript/package-sets)
## Build a Node HTTP server with Trout.

purescript-nodetrout

[Trout](https://github.com/purescript-hyper/purescript-trout) is a type-level routing DSL. Similar to Haskell's [Servant](https://github.com/haskell-servant/servant) library, Trout allows routes to be specified as a data type. Given this data type along with a [record](https://github.com/purescript/documentation/blob/master/language/Records.md) of corresponding handlers, this library can produce a [`node-http`](https://github.com/purescript-node/purescript-node-http) request handler of type `Request -> Response -> Effect Unit`, which [can then be used to create an HTTP server](https://pursuit.purescript.org/packages/purescript-node-http/5.0.2/docs/Node.HTTP#v:createServer).

### An API in 4 simple steps
1. Specify routes as a data type. Here, we create a `GET /admin` route that requires a Basic Authorization header and responds with a greeting:
```purescript
type Site = "admin" := "admin" :/ Header "Authorization" BasicAuth :> Resource (Get Greeting JSON)
```

2. Create a [`Proxy`](https://pursuit.purescript.org/packages/purescript-proxy/3.0.0/docs/Type.Proxy) value to capture the route specifications:
```purescript
site :: Proxy Site
site = Proxy
```

3. Define a handler for each route. Here, we greet the user by the username specified in the Basic Authorization header:
```purescript
resources :: forall m. Monad m => { admin :: BasicAuth -> { "GET" :: ExceptT HTTPError m Greeting } }
resources = { admin: \auth -> { "GET": pure $ Greeting $ "Hello, " <> (fst $ un BasicAuth auth) } }
```

4. Serve the API using `node-http`:
```purescript
main :: Effect Unit
main = do
server <- createServer $ serve' site resources (const $ pure unit)
listen server { hostname: "0.0.0.0", port: 3000, backlog: Nothing } $ log "Listening on port 3000..."
```

### Installation

[bower](https://github.com/bower/bower):
```
bower install --save purescript-nodetrout
```

[psc-package](https://github.com/purescript/psc-package):
```
psc-package install nodetrout
```

[spago](https://github.com/spacchetti/spago):
```
spago install nodetrout
```

### Examples

A number of usage examples are available [here](example).

To run the examples, clone the repository and run one of the following depending on your package manager and build tool, replacing `` with the name of one of the examples.

[bower](https://github.com/bower/bower) + [pulp](http://github.com/purescript-contrib/pulp):
```
bower install
pulp run -I example -m Example.
```

[spago](https://github.com/spacchetti/spago):
```
spago run -p example/.purs -m Example.
```