module Network.PinPon.Config
(
App(..)
, Config(..)
, createConfig
, Platform(..)
, env
, arn
, platforms
) where
import Protolude
import Control.Lens
import Control.Monad.Base (MonadBase(..))
import Control.Monad.Catch (MonadCatch(..), MonadThrow(..))
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Except (MonadError)
import Control.Monad.Reader (MonadReader(..), ReaderT)
import Control.Monad.Trans.AWS
(Region, Credentials, newEnv, runResourceT, runAWST, send)
import Control.Monad.Trans.Resource (MonadResource(..), ResourceT)
import Data.Maybe (fromJust)
import Data.Set (Set)
import qualified Data.Set as Set (empty)
import Data.Text (Text)
import Network.AWS (Env, HasEnv(..))
import Network.AWS.SNS (createTopic, ctrsTopicARN)
import Servant (Handler, ServantErr)
data Platform
= APNS
| APNSSandbox
deriving (Eq, Ord, Enum, Bounded, Show, Read)
data Config = Config
{ _env :: !Env
, _arn :: !Text
, _platforms :: Set Platform
}
makeClassy ''Config
instance HasEnv Config where
environment = env
createConfig :: Region -> Credentials -> Text -> IO Config
createConfig region credentials topicName =
do e <- newEnv credentials
let enviro = e & envRegion .~ region
topic <- runResourceT . runAWST enviro $
send $ createTopic topicName
return Config { _env = enviro
, _arn = fromJust $ topic ^. ctrsTopicARN
, _platforms = Set.empty
}
newtype App a =
App {runApp :: ReaderT Config (ResourceT Handler) a}
deriving (Functor,Applicative,Monad,MonadBase IO,MonadError ServantErr,MonadCatch,MonadThrow,MonadReader Config,MonadIO,MonadResource)