module Control.Remote.Monad.JSON.Server
(
scottyReceiveAPI
, serverReceiveAPI
) where
import Control.Remote.Monad.JSON.Router (ReceiveAPI(..))
import Control.Monad.IO.Class
import Control.Natural
import Data.Aeson()
import Data.Default.Class (def)
import Web.Scotty (scottyOpts)
import Web.Scotty.Trans as T
import Network.Wai.Handler.Warp (setPort)
serverReceiveAPI :: Int -> String -> (ReceiveAPI :~> IO) -> IO ()
serverReceiveAPI port path f = scottyOpts opts $ post (literal path) $ scottyReceiveAPI f
where opts = def { verbose = 0, settings = setPort port (settings def) }
scottyReceiveAPI :: (ScottyError e, MonadIO m)
=> (ReceiveAPI :~> IO) -> ActionT e m ()
scottyReceiveAPI (Nat f) = do
d <- jsonData
r <- liftIO $ f $ Receive $ d
case r of
Nothing -> return ()
Just v -> T.json v