Linnet is a slim wrapper around WAI Application
with Endpoint m a
as a main abstraction that takes HTTP request as an input and returns m a
as an output.
Simple but powerful combinator nature of Endpoint
together with provided transformers enables building of complex
HTTP API at a high degree of maintainability and efficiency.
Hello world
Here is a simple definition of endpoint that returns Hello, $name
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeSynonymInstances #-}
import Control.Exception (SomeException)
import Data.Function ((&))
import Data.Text (Text, append)
import Linnet
import Network.Wai (Application)
-- Linnet makes no assumption on how to encode exceptions.
-- It's necessary to define encoder of exceptions for used content-types.
-- Here it returns no content
instance Encode TextPlain SomeException where
encode _ = mempty
helloName :: Endpoint IO Text
helloName = get(p' "hello" // path @Text) ~>>
(\name -> return $ ok ("Hello, " `append` name))
app :: Application
app = bootstrap @TextPlain helloName & compile & toApp
To run the application using re-imported Warp:
main :: IO ()
main = run 9000 app
Now, to the call:
curl -v http://localhost:9000/hello/linnet