Pass from file

This commit is contained in:
2022-02-10 22:34:55 +02:00
parent 63411532c4
commit 94f4593fdc
7 changed files with 21 additions and 5 deletions

View File

@ -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

View File

@ -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