-- | Module for HTML-based servers
module Mig.Html.IO
  (
  -- * methods
    Get (..)
  , Post (..)
  , Put (..)
  , Delete (..)
  , Patch (..)
  , Options (..)

  -- * common
  -- | Common re-exports
  , module X
  ) where

import Mig.Common as X
import Mig.Internal.Types (toMethod)
import Network.HTTP.Types.Method

-- Get

-- | Get method. Note that we can not use body input with Get-method, use Post for that.
-- So with Get we can use only URI inputs (Query, Optional, Capture)
newtype Get a = Get (IO a)

instance (ToHtmlResp a) => ToServer (Get a) where
  type ServerMonad (Get a) = IO
  toServer :: Get a -> Server (ServerMonad (Get a))
toServer (Get IO a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodGet (forall a. ToHtmlResp a => a -> Resp
toHtmlResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
act)

-- | Post method
newtype Post a = Post (IO a)

instance (ToHtmlResp a) => ToServer (Post a) where
  type ServerMonad (Post a) = IO
  toServer :: Post a -> Server (ServerMonad (Post a))
toServer (Post IO a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodPost (forall a. ToHtmlResp a => a -> Resp
toHtmlResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
act)

-- | Put method
newtype Put a = Put (IO a)

instance (ToHtmlResp a) => ToServer (Put a) where
  type ServerMonad (Put a) = IO
  toServer :: Put a -> Server (ServerMonad (Put a))
toServer (Put IO a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodPut (forall a. ToHtmlResp a => a -> Resp
toHtmlResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
act)

-- | Delete method
newtype Delete a = Delete (IO a)

instance (ToHtmlResp a) => ToServer (Delete a) where
  type ServerMonad (Delete a) = IO
  toServer :: Delete a -> Server (ServerMonad (Delete a))
toServer (Delete IO a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodDelete (forall a. ToHtmlResp a => a -> Resp
toHtmlResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
act)

-- | Patch method
newtype Patch a = Patch (IO a)

instance (ToHtmlResp a) => ToServer (Patch a) where
  type ServerMonad (Patch a) = IO
  toServer :: Patch a -> Server (ServerMonad (Patch a))
toServer (Patch IO a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodPatch (forall a. ToHtmlResp a => a -> Resp
toHtmlResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
act)

-- | Options method
newtype Options a = Options (IO a)

instance (ToHtmlResp a) => ToServer (Options a) where
  type ServerMonad (Options a) = IO
  toServer :: Options a -> Server (ServerMonad (Options a))
toServer (Options IO a
act) = forall (m :: * -> *). Monad m => Method -> m Resp -> Server m
toMethod Method
methodOptions (forall a. ToHtmlResp a => a -> Resp
toHtmlResp forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
act)