diff --git a/config.dhall b/config.dhall index 4ec83e1..290e3cc 100644 --- a/config.dhall +++ b/config.dhall @@ -3,7 +3,7 @@ let config = ./dhall/package.dhall in { amqp = config.AMQP::{ , vhost = "reddit" , username = env:AMQP_USER as Text ? "reddit_pub" - , password = env:AMQP_PASS as Text ? "tester" + , password = config.Password.Type.Password (env:AMQP_PASS as Text ? "tester") , host = env:AMQP_HOST as Text ? "127.0.0.1" -- , host = "10.233.5.2" } diff --git a/dhall/AMQP/Type.dhall b/dhall/AMQP/Type.dhall index 42fec25..2d2f4c0 100644 --- a/dhall/AMQP/Type.dhall +++ b/dhall/AMQP/Type.dhall @@ -1,5 +1,5 @@ { vhost : Text , username : Text -, password : Text +, password : ../Password/Type.dhall , host : Text } diff --git a/dhall/Password/Type.dhall b/dhall/Password/Type.dhall new file mode 100644 index 0000000..cd381a0 --- /dev/null +++ b/dhall/Password/Type.dhall @@ -0,0 +1 @@ +< Password : Text | File : Text > diff --git a/dhall/Password/package.dhall b/dhall/Password/package.dhall new file mode 100644 index 0000000..ac35283 --- /dev/null +++ b/dhall/Password/package.dhall @@ -0,0 +1 @@ +{ Type = ./Type.dhall } diff --git a/dhall/package.dhall b/dhall/package.dhall index d5d9d1b..19abe30 100644 --- a/dhall/package.dhall +++ b/dhall/package.dhall @@ -2,4 +2,5 @@ , default = ./default.dhall , AMQP = ./AMQP/package.dhall , Fetcher = ./Fetcher/package.dhall +, Password = ./Password/package.dhall } diff --git a/src/Data/Config.hs b/src/Data/Config.hs index 2bd20a3..329cac3 100644 --- a/src/Data/Config.hs +++ b/src/Data/Config.hs @@ -11,10 +11,17 @@ import Dhall.Deriving import Numeric.Natural (Natural) import Data.SubReddit (SubReddit) +data Password + = Password Text + | File FilePath + deriving stock (Generic, Show) + deriving (FromDhall, ToDhall) + via (Codec AsIs Password) + data AMQP = AMQP { amqpVhost :: Text , amqpUsername :: Text - , amqpPassword :: Text + , amqpPassword :: Password , amqpHost :: Text } deriving stock (Generic, Show) @@ -30,7 +37,7 @@ vhost = lens amqpVhost (\ am txt -> am{amqpVhost=txt}) username :: Lens' AMQP Text username = lens amqpUsername (\ am txt -> am{amqpUsername=txt}) -password :: Lens' AMQP Text +password :: Lens' AMQP Password password = lens amqpPassword (\ am txt -> am{amqpPassword=txt}) data Fetcher = Fetcher diff --git a/src/MyLib.hs b/src/MyLib.hs index 7f9f732..dc448e6 100644 --- a/src/MyLib.hs +++ b/src/MyLib.hs @@ -35,6 +35,7 @@ import Network.Wreq.Session (newSession) import Publish (Publish(..)) import Data.Aeson.Lens (key, _String) import Data.Bool (bool) +import qualified Data.Text.IO as TI data MessageType = Create | Update deriving stock (Show, Eq, Generic) @@ -70,11 +71,12 @@ amqpPublisher sqlConn channel exchange = Publish $ \msg -> do defaultMain :: FilePath -> IO () defaultMain path = do conf <- readConfig path + pass <- getPassword (conf ^. amqp . password) let rabbitConnect = openConnection (conf ^. amqp . host . T.unpacked) (conf ^. amqp . vhost) (conf ^. amqp . username) - (conf ^. amqp . password) + pass bracket rabbitConnect closeConnection $ \conn -> do SQL.withConnection (conf ^. sqlite) $ \sqlConn -> do SQL.execute_ sqlConn "create table if not exists membership (reddit_id primary key)" @@ -87,3 +89,7 @@ defaultMain path = do for_ (conf ^. fetchers) $ \fetcher -> do print fetcher publishEntries (toMessage >$< publisher) sess fetcher + +getPassword :: Password -> IO Text +getPassword (Password p) = pure p +getPassword (File path) = TI.readFile path