2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2015-04-24 12:37:33 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Text
|
|
|
|
import GHC.Generics
|
|
|
|
import Network.Wai
|
|
|
|
import Network.Wai.Handler.Warp
|
|
|
|
import Network.Wai.Middleware.RequestLogger
|
|
|
|
import Servant
|
2015-04-24 12:37:33 +02:00
|
|
|
|
|
|
|
data Product = Product
|
|
|
|
{ name :: Text
|
|
|
|
, brand :: Text
|
|
|
|
, current_price_eur :: Double
|
|
|
|
, available :: Bool
|
|
|
|
} deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance ToJSON Product
|
|
|
|
|
|
|
|
products :: [Product]
|
|
|
|
products = [p1, p2]
|
|
|
|
|
|
|
|
where p1 = Product "Haskell laptop sticker"
|
|
|
|
"GHC Industries"
|
|
|
|
2.50
|
|
|
|
True
|
|
|
|
|
|
|
|
p2 = Product "Foldable USB drive"
|
|
|
|
"Well-Typed"
|
|
|
|
13.99
|
|
|
|
False
|
|
|
|
|
|
|
|
type SimpleAPI = Get '[JSON] [Product]
|
|
|
|
|
|
|
|
simpleAPI :: Proxy SimpleAPI
|
|
|
|
simpleAPI = Proxy
|
|
|
|
|
|
|
|
server :: Server SimpleAPI
|
|
|
|
server = return products
|
|
|
|
|
|
|
|
-- logStdout :: Middleware
|
|
|
|
-- i.e, logStdout :: Application -> Application
|
2016-01-14 23:43:48 +01:00
|
|
|
-- serve :: Proxy api -> Config a -> Server api -> Application
|
2015-04-24 12:37:33 +02:00
|
|
|
-- so applying a middleware is really as simple as
|
|
|
|
-- applying a function to the result of 'serve'
|
|
|
|
app :: Application
|
2016-01-14 23:43:48 +01:00
|
|
|
app = logStdout (serve simpleAPI EmptyConfig server)
|
2015-04-24 12:37:33 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = run 8080 app
|