Pass from file

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

View File

@ -3,7 +3,7 @@ let config = ./dhall/package.dhall
in { amqp = config.AMQP::{ in { amqp = config.AMQP::{
, vhost = "reddit" , vhost = "reddit"
, username = env:AMQP_USER as Text ? "reddit_pub" , 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 = env:AMQP_HOST as Text ? "127.0.0.1"
-- , host = "10.233.5.2" -- , host = "10.233.5.2"
} }

View File

@ -1,5 +1,5 @@
{ vhost : Text { vhost : Text
, username : Text , username : Text
, password : Text , password : ../Password/Type.dhall
, host : Text , host : Text
} }

View File

@ -0,0 +1 @@
< Password : Text | File : Text >

View File

@ -0,0 +1 @@
{ Type = ./Type.dhall }

View File

@ -2,4 +2,5 @@
, default = ./default.dhall , default = ./default.dhall
, AMQP = ./AMQP/package.dhall , AMQP = ./AMQP/package.dhall
, Fetcher = ./Fetcher/package.dhall , Fetcher = ./Fetcher/package.dhall
, Password = ./Password/package.dhall
} }

View File

@ -11,10 +11,17 @@ import Dhall.Deriving
import Numeric.Natural (Natural) import Numeric.Natural (Natural)
import Data.SubReddit (SubReddit) import Data.SubReddit (SubReddit)
data Password
= Password Text
| File FilePath
deriving stock (Generic, Show)
deriving (FromDhall, ToDhall)
via (Codec AsIs Password)
data AMQP = AMQP data AMQP = AMQP
{ amqpVhost :: Text { amqpVhost :: Text
, amqpUsername :: Text , amqpUsername :: Text
, amqpPassword :: Text , amqpPassword :: Password
, amqpHost :: Text , amqpHost :: Text
} }
deriving stock (Generic, Show) deriving stock (Generic, Show)
@ -30,7 +37,7 @@ vhost = lens amqpVhost (\ am txt -> am{amqpVhost=txt})
username :: Lens' AMQP Text username :: Lens' AMQP Text
username = lens amqpUsername (\ am txt -> am{amqpUsername=txt}) username = lens amqpUsername (\ am txt -> am{amqpUsername=txt})
password :: Lens' AMQP Text password :: Lens' AMQP Password
password = lens amqpPassword (\ am txt -> am{amqpPassword=txt}) password = lens amqpPassword (\ am txt -> am{amqpPassword=txt})
data Fetcher = Fetcher data Fetcher = Fetcher

View File

@ -35,6 +35,7 @@ import Network.Wreq.Session (newSession)
import Publish (Publish(..)) import Publish (Publish(..))
import Data.Aeson.Lens (key, _String) import Data.Aeson.Lens (key, _String)
import Data.Bool (bool) import Data.Bool (bool)
import qualified Data.Text.IO as TI
data MessageType = Create | Update data MessageType = Create | Update
deriving stock (Show, Eq, Generic) deriving stock (Show, Eq, Generic)
@ -70,11 +71,12 @@ amqpPublisher sqlConn channel exchange = Publish $ \msg -> do
defaultMain :: FilePath -> IO () defaultMain :: FilePath -> IO ()
defaultMain path = do defaultMain path = do
conf <- readConfig path conf <- readConfig path
pass <- getPassword (conf ^. amqp . password)
let rabbitConnect = openConnection let rabbitConnect = openConnection
(conf ^. amqp . host . T.unpacked) (conf ^. amqp . host . T.unpacked)
(conf ^. amqp . vhost) (conf ^. amqp . vhost)
(conf ^. amqp . username) (conf ^. amqp . username)
(conf ^. amqp . password) pass
bracket rabbitConnect closeConnection $ \conn -> do bracket rabbitConnect closeConnection $ \conn -> do
SQL.withConnection (conf ^. sqlite) $ \sqlConn -> do SQL.withConnection (conf ^. sqlite) $ \sqlConn -> do
SQL.execute_ sqlConn "create table if not exists membership (reddit_id primary key)" 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 for_ (conf ^. fetchers) $ \fetcher -> do
print fetcher print fetcher
publishEntries (toMessage >$< publisher) sess fetcher publishEntries (toMessage >$< publisher) sess fetcher
getPassword :: Password -> IO Text
getPassword (Password p) = pure p
getPassword (File path) = TI.readFile path