module Conduit.App.Env where

import Conduit.App.Has (Has', obtain)
import Conduit.DB.Core (DBPool)
import Conduit.Identity.JWT (JWTInfo)
import Data.Aeson (FromJSON)

-- | The type of the runtime environment. @Development@ has more logging and auto-runs DB migrations.
data EnvType = Production | Development
  deriving (Int -> EnvType -> ShowS
[EnvType] -> ShowS
EnvType -> String
(Int -> EnvType -> ShowS)
-> (EnvType -> String) -> ([EnvType] -> ShowS) -> Show EnvType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EnvType -> ShowS
showsPrec :: Int -> EnvType -> ShowS
$cshow :: EnvType -> String
show :: EnvType -> String
$cshowList :: [EnvType] -> ShowS
showList :: [EnvType] -> ShowS
Show, EnvType -> EnvType -> Bool
(EnvType -> EnvType -> Bool)
-> (EnvType -> EnvType -> Bool) -> Eq EnvType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EnvType -> EnvType -> Bool
== :: EnvType -> EnvType -> Bool
$c/= :: EnvType -> EnvType -> Bool
/= :: EnvType -> EnvType -> Bool
Eq, ReadPrec [EnvType]
ReadPrec EnvType
Int -> ReadS EnvType
ReadS [EnvType]
(Int -> ReadS EnvType)
-> ReadS [EnvType]
-> ReadPrec EnvType
-> ReadPrec [EnvType]
-> Read EnvType
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS EnvType
readsPrec :: Int -> ReadS EnvType
$creadList :: ReadS [EnvType]
readList :: ReadS [EnvType]
$creadPrec :: ReadPrec EnvType
readPrec :: ReadPrec EnvType
$creadListPrec :: ReadPrec [EnvType]
readListPrec :: ReadPrec [EnvType]
Read, (forall x. EnvType -> Rep EnvType x)
-> (forall x. Rep EnvType x -> EnvType) -> Generic EnvType
forall x. Rep EnvType x -> EnvType
forall x. EnvType -> Rep EnvType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EnvType -> Rep EnvType x
from :: forall x. EnvType -> Rep EnvType x
$cto :: forall x. Rep EnvType x -> EnvType
to :: forall x. Rep EnvType x -> EnvType
Generic, Maybe EnvType
Value -> Parser [EnvType]
Value -> Parser EnvType
(Value -> Parser EnvType)
-> (Value -> Parser [EnvType]) -> Maybe EnvType -> FromJSON EnvType
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser EnvType
parseJSON :: Value -> Parser EnvType
$cparseJSONList :: Value -> Parser [EnvType]
parseJSONList :: Value -> Parser [EnvType]
$comittedField :: Maybe EnvType
omittedField :: Maybe EnvType
FromJSON)

-- | Global state of the application.
data Env = Env
  { Env -> DBPool
envDBPool  :: !DBPool  -- ^ The database connection pool.
  , Env -> JWTInfo
envJWTInfo :: !JWTInfo -- ^ The necessary info for creating & verifying JWTs.
  , Env -> EnvType
envType    :: !EnvType -- ^ The type of the runtime environment.
  }

instance Has' DBPool Env where
  obtain :: Env -> DBPool
  obtain :: Env -> DBPool
obtain = (.envDBPool)
  {-# INLINE obtain #-}

instance Has' JWTInfo Env where
  obtain :: Env -> JWTInfo
  obtain :: Env -> JWTInfo
obtain = (.envJWTInfo)
  {-# INLINE obtain #-}

instance Has' EnvType Env where
  obtain :: Env -> EnvType
  obtain :: Env -> EnvType
obtain = (.envType)
  {-# INLINE obtain #-}