-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAppEnv.hs
68 lines (62 loc) · 1.57 KB
/
AppEnv.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module App.AppEnv
( AppEnv
( AppEnv,
appEnvConfig,
appEnvLogFunc,
appEnvHttpConfig,
appEnvDbPool
),
appEnvInit,
)
where
import App.Cart
( HasCartConfig
( getBookingDelay,
getBookingUrl,
getPaymentDelay,
getPaymentUrl
),
)
import App.Config
( Config
( configBookingDelay,
configBookingUrl,
configDatabaseUrl,
configPaymentDelay,
configPaymentUrl
),
configInit,
)
import App.Db (HasDbPool (getDbPool), dbInit)
import App.Logging (HasLogFunc (getLogFunc), LogFunc, loggingInit)
import Data.Pool (Pool)
import Database.PostgreSQL.Simple (Connection)
import Network.HTTP.Req (HttpConfig, defaultHttpConfig)
data AppEnv = AppEnv
{ appEnvConfig :: Config,
appEnvLogFunc :: LogFunc,
appEnvHttpConfig :: HttpConfig,
appEnvDbPool :: Pool Connection
}
instance HasLogFunc AppEnv where
getLogFunc = appEnvLogFunc
instance HasCartConfig AppEnv where
getBookingUrl = configBookingUrl . appEnvConfig
getBookingDelay = configBookingDelay . appEnvConfig
getPaymentUrl = configPaymentUrl . appEnvConfig
getPaymentDelay = configPaymentDelay . appEnvConfig
instance HasDbPool AppEnv where
getDbPool = appEnvDbPool
appEnvInit :: IO AppEnv
appEnvInit = do
config <- configInit
logFunc <- loggingInit
dbPool <- dbInit $ configDatabaseUrl config
let app =
AppEnv
{ appEnvConfig = config,
appEnvLogFunc = logFunc,
appEnvHttpConfig = defaultHttpConfig,
appEnvDbPool = dbPool
}
pure app