]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Private.hs
[FEAT] Sugar to connect to database in REPL.
[gargantext.git] / src / Gargantext / Database / Private.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE FlexibleContexts #-}
3
4 module Gargantext.Database.Private where
5
6 import qualified Database.PostgreSQL.Simple as PGS
7
8 import Data.Monoid ((<>))
9 import Data.Either.Extra (Either(Left, Right))
10 import Gargantext.Prelude
11 import Data.Text (unpack, pack)
12 import Text.Read (read)
13 import Data.Ini (readIniFile, lookupValue)
14 import Data.Word (Word16)
15 import System.IO (FilePath)
16 import Database.PostgreSQL.Simple (Connection, connect)
17
18 -- TODO add a reader Monad here
19 -- read this in the init file
20
21 databaseParameters :: FilePath -> IO PGS.ConnectInfo
22 databaseParameters fp = do
23 ini <- readIniFile fp
24 let ini'' = case ini of
25 Left e -> panic (pack $ "No ini file error" <> show e)
26 Right ini' -> ini'
27
28 let val x = case (lookupValue (pack "django") (pack x) ini'') of
29 Left _ -> panic (pack $ "no" <> x)
30 Right p' -> unpack p'
31
32 pure $ PGS.ConnectInfo { PGS.connectHost = val "DB_HOST"
33 , PGS.connectPort = read (val "DB_PORT") :: Word16
34 , PGS.connectUser = val "DB_USER"
35 , PGS.connectPassword = val "DB_PASS"
36 , PGS.connectDatabase = val "DB_NAME" }
37
38 connectGargandb :: FilePath -> IO Connection
39 connectGargandb fp = do
40 parameters <- databaseParameters fp
41 connect parameters