module Ecluse.Config.Ambient (
AmbientAws (..),
ambientAwsFromEnv,
parseEndpointUrl,
) where
import Data.List (lookup)
import Data.Text qualified as T
import Ecluse.Core.Security (splitHostPort)
import Ecluse.Core.Text (nonBlank)
data AmbientAws = AmbientAws
{ AmbientAws -> Maybe Text
ambientAwsRegion :: Maybe Text
, AmbientAws -> Maybe Text
ambientAwsEndpointUrlSqs :: Maybe Text
, AmbientAws -> Maybe Text
ambientAwsEndpointUrl :: Maybe Text
}
deriving stock (AmbientAws -> AmbientAws -> Bool
(AmbientAws -> AmbientAws -> Bool)
-> (AmbientAws -> AmbientAws -> Bool) -> Eq AmbientAws
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AmbientAws -> AmbientAws -> Bool
== :: AmbientAws -> AmbientAws -> Bool
$c/= :: AmbientAws -> AmbientAws -> Bool
/= :: AmbientAws -> AmbientAws -> Bool
Eq, Int -> AmbientAws -> ShowS
[AmbientAws] -> ShowS
AmbientAws -> String
(Int -> AmbientAws -> ShowS)
-> (AmbientAws -> String)
-> ([AmbientAws] -> ShowS)
-> Show AmbientAws
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AmbientAws -> ShowS
showsPrec :: Int -> AmbientAws -> ShowS
$cshow :: AmbientAws -> String
show :: AmbientAws -> String
$cshowList :: [AmbientAws] -> ShowS
showList :: [AmbientAws] -> ShowS
Show)
ambientAwsFromEnv :: [(String, String)] -> AmbientAws
ambientAwsFromEnv :: [(String, String)] -> AmbientAws
ambientAwsFromEnv [(String, String)]
env =
AmbientAws
{ ambientAwsRegion :: Maybe Text
ambientAwsRegion = String -> Maybe Text
look String
"AWS_REGION"
, ambientAwsEndpointUrlSqs :: Maybe Text
ambientAwsEndpointUrlSqs = String -> Maybe Text
look String
"AWS_ENDPOINT_URL_SQS"
, ambientAwsEndpointUrl :: Maybe Text
ambientAwsEndpointUrl = String -> Maybe Text
look String
"AWS_ENDPOINT_URL"
}
where
look :: String -> Maybe Text
look String
name = String -> Text
T.pack (String -> Text) -> Maybe String -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> [(String, String)] -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
name [(String, String)]
env
parseEndpointUrl :: Text -> Maybe (Bool, Text, Int)
parseEndpointUrl :: Text -> Maybe (Bool, Text, Int)
parseEndpointUrl Text
raw = do
(secure, afterScheme) <-
((Bool
True,) (Text -> (Bool, Text)) -> Maybe Text -> Maybe (Bool, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> Maybe Text
T.stripPrefix Text
"https://" Text
raw) Maybe (Bool, Text) -> Maybe (Bool, Text) -> Maybe (Bool, Text)
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((Bool
False,) (Text -> (Bool, Text)) -> Maybe Text -> Maybe (Bool, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> Maybe Text
T.stripPrefix Text
"http://" Text
raw)
let authority = (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> String -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` [Char
'/', Char
'?', Char
'#']) Text
afterScheme
(hostText, portText) <- splitHostPort authority
host <- nonBlank hostText
port <- case T.stripPrefix ":" portText of
Maybe Text
Nothing -> Int -> Maybe Int
forall a. a -> Maybe a
Just (if Bool
secure then Int
443 else Int
80)
Just Text
digits -> String -> Maybe Int
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
forall a. ToString a => a -> String
toString Text
digits)
pure (secure, host, port)