module IHP.FileStorage.Config
( initS3Storage
, initStaticDirStorage
, initMinioStorage
, initFilebaseStorage
) where
import IHP.Prelude
import IHP.FileStorage.Types
import IHP.FrameworkConfig
import Network.Minio
import qualified Control.Monad.Trans.State.Strict as State
import qualified Data.TMap as TMap
import Control.Monad.Trans.Maybe
import qualified IHP.EnvVar as EnvVar
initS3Storage :: HasCallStack => Text -> Text -> State.StateT TMap.TMap IO ()
initS3Storage :: HasCallStack => Region -> Region -> StateT TMap IO ()
initS3Storage Region
region Region
bucket = do
ConnectInfo
connectInfo <- ConnectInfo
awsCI
ConnectInfo -> (ConnectInfo -> ConnectInfo) -> ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Region -> ConnectInfo -> ConnectInfo
setRegion Region
region
ConnectInfo -> (ConnectInfo -> IO ConnectInfo) -> IO ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> [CredentialLoader] -> ConnectInfo -> IO ConnectInfo
setCredsFrom [CredentialLoader
fromAWSEnv]
IO ConnectInfo
-> (IO ConnectInfo -> StateT TMap IO ConnectInfo)
-> StateT TMap IO ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> IO ConnectInfo -> StateT TMap IO ConnectInfo
forall (monad :: * -> *) result.
(MonadIO monad, HasCallStack) =>
IO result -> monad result
configIO
let baseUrl :: Region
baseUrl = Region
"https://" Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
bucket Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
".s3." Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
region Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
".amazonaws.com/"
FileStorage -> StateT TMap IO ()
forall option. Typeable option => option -> StateT TMap IO ()
option S3Storage { ConnectInfo
connectInfo :: ConnectInfo
connectInfo :: ConnectInfo
connectInfo, Region
bucket :: Region
bucket :: Region
bucket, Region
baseUrl :: Region
baseUrl :: Region
baseUrl }
initMinioStorage :: HasCallStack => Text -> Text -> State.StateT TMap.TMap IO ()
initMinioStorage :: HasCallStack => Region -> Region -> StateT TMap IO ()
initMinioStorage Region
server Region
bucket = do
ConnectInfo
connectInfo <- Region
server
Region -> (Region -> String) -> String
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Region -> String
forall a b. ConvertibleStrings a b => a -> b
cs
String -> (String -> ConnectInfo) -> ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> String -> ConnectInfo
forall a. IsString a => String -> a
fromString
ConnectInfo -> (ConnectInfo -> IO ConnectInfo) -> IO ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> [CredentialLoader] -> ConnectInfo -> IO ConnectInfo
setCredsFrom [CredentialLoader
fromMinioEnv]
IO ConnectInfo
-> (IO ConnectInfo -> StateT TMap IO ConnectInfo)
-> StateT TMap IO ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> IO ConnectInfo -> StateT TMap IO ConnectInfo
forall (monad :: * -> *) result.
(MonadIO monad, HasCallStack) =>
IO result -> monad result
configIO
let baseUrl :: Region
baseUrl = Region
server Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
"/" Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
bucket Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
"/"
FileStorage -> StateT TMap IO ()
forall option. Typeable option => option -> StateT TMap IO ()
option S3Storage { ConnectInfo
connectInfo :: ConnectInfo
connectInfo :: ConnectInfo
connectInfo, Region
bucket :: Region
bucket :: Region
bucket, Region
baseUrl :: Region
baseUrl :: Region
baseUrl }
initStaticDirStorage :: State.StateT TMap.TMap IO ()
initStaticDirStorage :: StateT TMap IO ()
initStaticDirStorage = FileStorage -> StateT TMap IO ()
forall option. Typeable option => option -> StateT TMap IO ()
option FileStorage
StaticDirStorage
initFilebaseStorage :: HasCallStack => Text -> State.StateT TMap.TMap IO ()
initFilebaseStorage :: HasCallStack => Region -> StateT TMap IO ()
initFilebaseStorage Region
bucket = do
ConnectInfo
connectInfo <- ConnectInfo
filebaseCI
ConnectInfo -> (ConnectInfo -> IO ConnectInfo) -> IO ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> [CredentialLoader] -> ConnectInfo -> IO ConnectInfo
setCredsFrom [CredentialLoader
fromFilebaseEnv]
IO ConnectInfo
-> (IO ConnectInfo -> StateT TMap IO ConnectInfo)
-> StateT TMap IO ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> IO ConnectInfo -> StateT TMap IO ConnectInfo
forall (monad :: * -> *) result.
(MonadIO monad, HasCallStack) =>
IO result -> monad result
configIO
let baseUrl :: Region
baseUrl = Region
"https://" Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
bucket Region -> Region -> Region
forall a. Semigroup a => a -> a -> a
<> Region
".s3.filebase.com/"
FileStorage -> StateT TMap IO ()
forall option. Typeable option => option -> StateT TMap IO ()
option S3Storage { ConnectInfo
connectInfo :: ConnectInfo
connectInfo :: ConnectInfo
connectInfo, Region
bucket :: Region
bucket :: Region
bucket, Region
baseUrl :: Region
baseUrl :: Region
baseUrl }
filebaseCI :: ConnectInfo
filebaseCI :: ConnectInfo
filebaseCI = ConnectInfo
"https://s3.filebase.com" ConnectInfo -> (ConnectInfo -> ConnectInfo) -> ConnectInfo
forall {t1} {t2}. t1 -> (t1 -> t2) -> t2
|> Region -> ConnectInfo -> ConnectInfo
setRegion Region
"us-east-1"
fromFilebaseEnv :: CredentialLoader
fromFilebaseEnv :: CredentialLoader
fromFilebaseEnv = MaybeT IO CredentialValue -> CredentialLoader
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IO CredentialValue -> CredentialLoader)
-> MaybeT IO CredentialValue -> CredentialLoader
forall a b. (a -> b) -> a -> b
$ do
String
filebaseKey <- IO (Maybe String) -> MaybeT IO String
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (IO (Maybe String) -> MaybeT IO String)
-> IO (Maybe String) -> MaybeT IO String
forall a b. (a -> b) -> a -> b
$ ByteString -> IO (Maybe String)
forall (monad :: * -> *) result.
(MonadIO monad, EnvVarReader result) =>
ByteString -> monad (Maybe result)
EnvVar.envOrNothing ByteString
"FILEBASE_KEY"
String
filebaseSecret <- IO (Maybe String) -> MaybeT IO String
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (IO (Maybe String) -> MaybeT IO String)
-> IO (Maybe String) -> MaybeT IO String
forall a b. (a -> b) -> a -> b
$ ByteString -> IO (Maybe String)
forall (monad :: * -> *) result.
(MonadIO monad, EnvVarReader result) =>
ByteString -> monad (Maybe result)
EnvVar.envOrNothing ByteString
"FILEBASE_SECRET"
CredentialValue -> MaybeT IO CredentialValue
forall a. a -> MaybeT IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CredentialValue { cvAccessKey :: AccessKey
cvAccessKey = String -> AccessKey
forall a. IsString a => String -> a
fromString String
filebaseKey, cvSecretKey :: SecretKey
cvSecretKey = String -> SecretKey
forall a. IsString a => String -> a
fromString String
filebaseSecret, cvSessionToken :: Maybe SessionToken
cvSessionToken = Maybe SessionToken
forall a. Maybe a
Nothing }