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 qualified System.Environment as Env
import qualified Data.Text as Text
import Control.Monad.Trans.Maybe
initS3Storage :: Text -> Text -> State.StateT TMap.TMap IO ()
initS3Storage :: Text -> Text -> StateT TMap IO ()
initS3Storage Text
region Text
bucket = do
ConnectInfo
connectInfo <- ConnectInfo
awsCI
ConnectInfo -> (ConnectInfo -> ConnectInfo) -> ConnectInfo
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Text -> ConnectInfo -> ConnectInfo
setRegion Text
region
ConnectInfo -> (ConnectInfo -> IO ConnectInfo) -> IO ConnectInfo
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> [Provider] -> ConnectInfo -> IO ConnectInfo
setCredsFrom [Provider
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 (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
let baseUrl :: Text
baseUrl = Text
"https://" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
bucket Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".s3." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
region Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".amazonaws.com/"
FileStorage -> StateT TMap IO ()
forall option. Typeable option => option -> StateT TMap IO ()
option S3Storage :: ConnectInfo -> Text -> Text -> FileStorage
S3Storage { ConnectInfo
$sel:connectInfo:StaticDirStorage :: ConnectInfo
connectInfo :: ConnectInfo
connectInfo, Text
$sel:bucket:StaticDirStorage :: Text
bucket :: Text
bucket, Text
$sel:baseUrl:StaticDirStorage :: Text
baseUrl :: Text
baseUrl }
initMinioStorage :: Text -> Text -> State.StateT TMap.TMap IO ()
initMinioStorage :: Text -> Text -> StateT TMap IO ()
initMinioStorage Text
server Text
bucket = do
ConnectInfo
connectInfo <- Text
server
Text -> (Text -> String) -> String
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Text -> 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
|> [Provider] -> ConnectInfo -> IO ConnectInfo
setCredsFrom [Provider
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 (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
let baseUrl :: Text
baseUrl = Text
server Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
bucket Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/"
FileStorage -> StateT TMap IO ()
forall option. Typeable option => option -> StateT TMap IO ()
option S3Storage :: ConnectInfo -> Text -> Text -> FileStorage
S3Storage { ConnectInfo
connectInfo :: ConnectInfo
$sel:connectInfo:StaticDirStorage :: ConnectInfo
connectInfo, Text
bucket :: Text
$sel:bucket:StaticDirStorage :: Text
bucket, Text
baseUrl :: Text
$sel:baseUrl:StaticDirStorage :: Text
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 :: Text -> State.StateT TMap.TMap IO ()
initFilebaseStorage :: Text -> StateT TMap IO ()
initFilebaseStorage Text
bucket = do
ConnectInfo
connectInfo <- ConnectInfo
filebaseCI
ConnectInfo -> (ConnectInfo -> IO ConnectInfo) -> IO ConnectInfo
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> [Provider] -> ConnectInfo -> IO ConnectInfo
setCredsFrom [Provider
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 (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
let baseUrl :: Text
baseUrl = Text
"https://" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
bucket Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".s3.filebase.com/"
FileStorage -> StateT TMap IO ()
forall option. Typeable option => option -> StateT TMap IO ()
option S3Storage :: ConnectInfo -> Text -> Text -> FileStorage
S3Storage { ConnectInfo
connectInfo :: ConnectInfo
$sel:connectInfo:StaticDirStorage :: ConnectInfo
connectInfo, Text
bucket :: Text
$sel:bucket:StaticDirStorage :: Text
bucket, Text
baseUrl :: Text
$sel:baseUrl:StaticDirStorage :: Text
baseUrl }
filebaseCI :: ConnectInfo
filebaseCI :: ConnectInfo
filebaseCI = ConnectInfo
"https://s3.filebase.com" ConnectInfo -> (ConnectInfo -> ConnectInfo) -> ConnectInfo
forall t1 t2. t1 -> (t1 -> t2) -> t2
|> Text -> ConnectInfo -> ConnectInfo
setRegion Text
"us-east-1"
fromFilebaseEnv :: Provider
fromFilebaseEnv :: Provider
fromFilebaseEnv = MaybeT IO Credentials -> Provider
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT IO Credentials -> Provider)
-> MaybeT IO Credentials -> Provider
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
$ String -> IO (Maybe String)
Env.lookupEnv String
"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
$ String -> IO (Maybe String)
Env.lookupEnv String
"FILEBASE_SECRET"
Credentials -> MaybeT IO Credentials
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Credentials -> MaybeT IO Credentials)
-> Credentials -> MaybeT IO Credentials
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Credentials
Credentials (String -> Text
Text.pack String
filebaseKey) (String -> Text
Text.pack String
filebaseSecret)