graphql-engine/server/src-lib/Hasura/Server/Init/FeatureFlag.hs
Philip Lykke Carlsen 34e40e6caf refactor: newtype alias for action checking feature flags
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8429
GitOrigin-RevId: e543b608a91e0c39c39f06b772a7d43f360d5dc3
2023-03-22 10:48:22 +00:00

79 lines
2.4 KiB
Haskell

{-# LANGUAGE DeriveAnyClass #-}
-- | Feature Flags are /temporary/ toggles.
module Hasura.Server.Init.FeatureFlag
( FeatureFlag (..),
CheckFeatureFlag (..),
checkFeatureFlag,
Identifier (..),
FeatureFlags (..),
featureFlags,
logicalModelInterface,
)
where
--------------------------------------------------------------------------------
import Data.Aeson (FromJSON, ToJSON)
import Data.Environment qualified as Env
import Data.HashMap.Strict qualified as HashMap
import Hasura.Prelude
--------------------------------------------------------------------------------
newtype Identifier = Identifier {getIdentifier :: Text}
deriving stock (Generic)
deriving newtype (Eq, FromJSON, ToJSON)
deriving anyclass (Hashable)
data FeatureFlag = FeatureFlag
{ ffIdentifier :: Identifier,
ffDefaultValue :: Bool,
ffDescription :: Text,
ffEnvVar :: String
}
deriving stock (Eq, Generic)
deriving anyclass (Hashable, FromJSON, ToJSON)
-- | In OSS we look for a environment variable or fall back to the default
-- value
checkFeatureFlag :: Env.Environment -> FeatureFlag -> IO Bool
checkFeatureFlag env (FeatureFlag {ffEnvVar = envVar, ffDefaultValue = defaultValue}) =
case Env.lookupEnv env envVar of
Just found -> pure $ fromMaybe defaultValue (readMaybe found)
Nothing -> pure $ defaultValue
newtype CheckFeatureFlag = CheckFeatureFlag {runCheckFeatureFlag :: FeatureFlag -> IO Bool}
--------------------------------------------------------------------------------
newtype FeatureFlags = FeatureFlags {getFeatureFlags :: HashMap Text FeatureFlag}
featureFlags :: FeatureFlags
featureFlags =
FeatureFlags $
HashMap.fromList
[ ("test-flag", testFlag),
("native-query-interface", logicalModelInterface)
]
--------------------------------------------------------------------------------
testFlag :: FeatureFlag
testFlag =
FeatureFlag
{ ffIdentifier = Identifier "test-flag",
ffDefaultValue = False,
ffDescription = "Testing feature flag integration",
ffEnvVar = "HASURA_FF_TEST_FLAG"
}
logicalModelInterface :: FeatureFlag
logicalModelInterface =
FeatureFlag
{ ffIdentifier = Identifier "native-query-interface",
ffDefaultValue = False,
ffDescription = "Expose custom views, permissions and advanced SQL functionality via custom queries",
ffEnvVar = "HASURA_FF_LOGICAL_MODEL_INTERFACE"
}