2023-01-23 16:35:48 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
|
|
|
|
-- | Feature Flags are /temporary/ toggles.
|
|
|
|
module Hasura.Server.Init.FeatureFlag
|
|
|
|
( FeatureFlag (..),
|
2023-03-22 13:46:54 +03:00
|
|
|
CheckFeatureFlag (..),
|
2023-01-26 12:39:49 +03:00
|
|
|
checkFeatureFlag,
|
2023-01-23 16:35:48 +03:00
|
|
|
Identifier (..),
|
|
|
|
FeatureFlags (..),
|
|
|
|
featureFlags,
|
2023-02-22 16:45:27 +03:00
|
|
|
logicalModelInterface,
|
2023-01-23 16:35:48 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
import Data.Aeson (FromJSON, ToJSON)
|
2023-01-26 12:39:49 +03:00
|
|
|
import Data.Environment qualified as Env
|
2023-01-23 16:35:48 +03:00
|
|
|
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,
|
2023-01-26 12:39:49 +03:00
|
|
|
ffDescription :: Text,
|
|
|
|
ffEnvVar :: String
|
2023-01-23 16:35:48 +03:00
|
|
|
}
|
|
|
|
deriving stock (Eq, Generic)
|
|
|
|
deriving anyclass (Hashable, FromJSON, ToJSON)
|
|
|
|
|
2023-01-26 12:39:49 +03:00
|
|
|
-- | 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
|
2023-01-23 16:35:48 +03:00
|
|
|
|
2023-03-22 13:46:54 +03:00
|
|
|
newtype CheckFeatureFlag = CheckFeatureFlag {runCheckFeatureFlag :: FeatureFlag -> IO Bool}
|
|
|
|
|
2023-01-23 16:35:48 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
newtype FeatureFlags = FeatureFlags {getFeatureFlags :: HashMap Text FeatureFlag}
|
|
|
|
|
|
|
|
featureFlags :: FeatureFlags
|
|
|
|
featureFlags =
|
|
|
|
FeatureFlags $
|
|
|
|
HashMap.fromList
|
|
|
|
[ ("test-flag", testFlag),
|
2023-02-22 16:45:27 +03:00
|
|
|
("native-query-interface", logicalModelInterface)
|
2023-01-23 16:35:48 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
testFlag :: FeatureFlag
|
|
|
|
testFlag =
|
|
|
|
FeatureFlag
|
|
|
|
{ ffIdentifier = Identifier "test-flag",
|
|
|
|
ffDefaultValue = False,
|
2023-01-26 12:39:49 +03:00
|
|
|
ffDescription = "Testing feature flag integration",
|
|
|
|
ffEnvVar = "HASURA_FF_TEST_FLAG"
|
2023-01-23 16:35:48 +03:00
|
|
|
}
|
|
|
|
|
2023-02-22 16:45:27 +03:00
|
|
|
logicalModelInterface :: FeatureFlag
|
|
|
|
logicalModelInterface =
|
2023-01-23 16:35:48 +03:00
|
|
|
FeatureFlag
|
|
|
|
{ ffIdentifier = Identifier "native-query-interface",
|
|
|
|
ffDefaultValue = False,
|
2023-01-26 12:39:49 +03:00
|
|
|
ffDescription = "Expose custom views, permissions and advanced SQL functionality via custom queries",
|
2023-02-22 16:45:27 +03:00
|
|
|
ffEnvVar = "HASURA_FF_LOGICAL_MODEL_INTERFACE"
|
2023-01-23 16:35:48 +03:00
|
|
|
}
|