|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | + |
| 3 | +module Cardano.Metadata.Sync.Config where |
| 4 | + |
| 5 | +import Control.Exception |
| 6 | + ( bracket ) |
| 7 | +import qualified Data.ByteString.Char8 as BC |
| 8 | +import Data.Pool |
| 9 | + ( Pool, createPool, destroyAllResources ) |
| 10 | +import qualified Data.Pool as Pool |
| 11 | +import Data.Text |
| 12 | + ( Text ) |
| 13 | +import qualified Data.Text as T |
| 14 | +import qualified Data.Text.Encoding as TE |
| 15 | +import Data.Time.Clock |
| 16 | + ( NominalDiffTime ) |
| 17 | +import Database.PostgreSQL.Simple |
| 18 | + ( Connection ) |
| 19 | +import qualified Database.PostgreSQL.Simple as Sql |
| 20 | +import Options.Applicative |
| 21 | + ( Parser, ParserInfo ) |
| 22 | +import Options.Applicative |
| 23 | + ( auto |
| 24 | + , fullDesc |
| 25 | + , header |
| 26 | + , help |
| 27 | + , info |
| 28 | + , long |
| 29 | + , metavar |
| 30 | + , option |
| 31 | + , progDesc |
| 32 | + , showDefault |
| 33 | + , strOption |
| 34 | + , value |
| 35 | + ) |
| 36 | +import qualified Options.Applicative as Opt |
| 37 | + |
| 38 | +data Opts = Opts |
| 39 | + { optDbName :: Text |
| 40 | + , optDbUser :: Text |
| 41 | + , optDbHost :: FilePath |
| 42 | + , optDbMetadataTableName :: Text |
| 43 | + , optDbConnections :: Int |
| 44 | + , optGitURL :: Text |
| 45 | + , optGitSubFolder :: Text |
| 46 | + } |
| 47 | + deriving (Eq, Show) |
| 48 | + |
| 49 | +parseOpts :: Parser Opts |
| 50 | +parseOpts = Opts |
| 51 | + <$> strOption (long "db" <> metavar "DB_NAME" <> help "Name of the database to store and read metadata from") |
| 52 | + <*> strOption (long "db-user" <> metavar "DB_USER" <> help "User to connect to metadata database with") |
| 53 | + <*> strOption (long "db-host" <> metavar "DB_HOST" <> showDefault <> value "/run/postgresql" <> help "Host for the metadata database connection") |
| 54 | + <*> strOption (long "db-table" <> metavar "DB_TABLE" <> showDefault <> value "metadata" <> help "Table in the database to store metadata") |
| 55 | + <*> option auto (long "db-conns" <> metavar "INT" <> showDefault <> value 1 <> help "Number of connections to open to the database") |
| 56 | + <*> strOption (long "git-url" <> metavar "GIT_URL" <> help "URL of the metadata registry git repository") |
| 57 | + <*> strOption (long "git-metadata-folder" <> metavar "GIT_METADATA_FOLDER" <> help "Sub-folder of the git repository containing the metadata") |
| 58 | + |
| 59 | +opts :: ParserInfo Opts |
| 60 | +opts = |
| 61 | + info |
| 62 | + parseOpts |
| 63 | + ( fullDesc |
| 64 | + <> progDesc "Sync up a metadata database with a GitHub repository" |
| 65 | + <> header "metadata-sync - a tool to keep the metadata storage layer and the GitHub repository in sync" |
| 66 | + ) |
| 67 | + |
| 68 | +pgConnectionString :: Opts -> BC.ByteString |
| 69 | +pgConnectionString (Opts { optDbName = dbName, optDbUser = dbUser, optDbHost = dbHost }) = |
| 70 | + TE.encodeUtf8 $ "host=" <> T.pack dbHost <> " dbname=" <> dbName <> " user=" <> dbUser |
| 71 | + |
| 72 | +mkConnectionPool |
| 73 | + :: BC.ByteString |
| 74 | + -- ^ Libpq connection string |
| 75 | + -> Int |
| 76 | + -- ^ Maximum number of postgresql connections to allow |
| 77 | + -> IO (Pool Connection) |
| 78 | +mkConnectionPool connectionStr numConns = |
| 79 | + createPool |
| 80 | + (Sql.connectPostgreSQL connectionStr) |
| 81 | + Sql.close |
| 82 | + 1 -- Number of sub-pools |
| 83 | + (10 :: NominalDiffTime) -- Amount of time for which an unused connection is kept open |
| 84 | + numConns |
| 85 | + |
| 86 | +withConnectionPool |
| 87 | + :: BC.ByteString |
| 88 | + -- ^ Libpq connection string |
| 89 | + -> Int |
| 90 | + -- ^ Maximum number of postgresql connections to allow |
| 91 | + -> (Pool Connection -> IO r) |
| 92 | + -> IO r |
| 93 | +withConnectionPool connectionInfo numConns f = bracket |
| 94 | + (mkConnectionPool connectionInfo numConns) |
| 95 | + destroyAllResources |
| 96 | + f |
| 97 | + |
| 98 | +withConnectionFromPool :: Pool Connection -> (Connection -> IO b) -> IO b |
| 99 | +withConnectionFromPool pool action = Pool.withResource pool $ action |
0 commit comments