Skip to content

Commit e65a5bf

Browse files
committed
Rename App to AppEnv and AppM to App
1 parent 79dbcbc commit e65a5bf

File tree

6 files changed

+128
-218
lines changed

6 files changed

+128
-218
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
dist-newstyle
2-
client_session_key.aes

example-handlers.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ common options
7575
library
7676
import: options
7777
exposed-modules:
78+
App.AppEnv
7879
App.Cart
7980
App.Config
8081
App.Db

src/App/AppEnv.hs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
module App.AppEnv
2+
( AppEnv
3+
( AppEnv,
4+
appEnvConfig,
5+
appEnvLogger,
6+
appEnvHttpConfig,
7+
appEnvDbPool
8+
),
9+
appEnvInit,
10+
)
11+
where
12+
13+
import App.Cart
14+
( HasCartConfig
15+
( getBookingDelay,
16+
getBookingUrl,
17+
getPaymentDelay,
18+
getPaymentUrl
19+
),
20+
)
21+
import App.Config
22+
( Config
23+
( configBookingDelay,
24+
configBookingUrl,
25+
configDatabaseUrl,
26+
configPaymentDelay,
27+
configPaymentUrl
28+
),
29+
configInit,
30+
)
31+
import App.Db (HasDbPool (getDbPool), dbInit)
32+
import Blammo.Logging (HasLogger (loggerL), Logger)
33+
import Blammo.Logging.Simple (newLoggerEnv)
34+
import Data.Pool (Pool)
35+
import Database.PostgreSQL.Simple (Connection)
36+
import Lens.Micro (lens)
37+
import Network.HTTP.Req (HttpConfig, defaultHttpConfig)
38+
39+
data AppEnv = AppEnv
40+
{ appEnvConfig :: Config,
41+
appEnvLogger :: Logger,
42+
appEnvHttpConfig :: HttpConfig,
43+
appEnvDbPool :: Pool Connection
44+
}
45+
46+
instance HasLogger AppEnv where
47+
loggerL = lens appEnvLogger $ \x y -> x {appEnvLogger = y}
48+
49+
instance HasCartConfig AppEnv where
50+
getBookingUrl = configBookingUrl . appEnvConfig
51+
getBookingDelay = configBookingDelay . appEnvConfig
52+
getPaymentUrl = configPaymentUrl . appEnvConfig
53+
getPaymentDelay = configPaymentDelay . appEnvConfig
54+
55+
instance HasDbPool AppEnv where
56+
getDbPool = appEnvDbPool
57+
58+
appEnvInit :: IO AppEnv
59+
appEnvInit = do
60+
config <- configInit
61+
logger <- newLoggerEnv
62+
dbPool <- dbInit $ configDatabaseUrl config
63+
let app =
64+
AppEnv
65+
{ appEnvConfig = config,
66+
appEnvLogger = logger,
67+
appEnvHttpConfig = defaultHttpConfig,
68+
appEnvDbPool = dbPool
69+
}
70+
pure app

src/App/Scotty.hs

+20-77
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,40 @@ module App.Scotty
55
)
66
where
77

8+
import App.AppEnv (AppEnv (appEnvConfig, appEnvHttpConfig), appEnvInit)
89
import App.Cart
910
( BookingId,
1011
CartException (CartException),
1112
CartId (CartId),
1213
CartStatus (CartStatusLocked, CartStatusOpen, CartStatusPurchased),
13-
HasCartConfig
14-
( getBookingDelay,
15-
getBookingUrl,
16-
getPaymentDelay,
17-
getPaymentUrl
18-
),
1914
PaymentId,
2015
getCartStatus,
2116
markCartAsPurchased,
2217
processBooking,
2318
processPayment,
2419
withCart,
2520
)
26-
import App.Config
27-
( Config
28-
( configBookingDelay,
29-
configBookingUrl,
30-
configDatabaseUrl,
31-
configPaymentDelay,
32-
configPaymentUrl,
33-
configPurchaseDelay
34-
),
35-
configInit,
36-
)
37-
import App.Db (HasDbPool (getDbPool), dbInit)
21+
import App.Config (Config (configPurchaseDelay))
3822
import App.Json (defaultToJSON)
3923
import Blammo.Logging
40-
( HasLogger (loggerL),
41-
Logger,
42-
LoggingT,
24+
( LoggingT,
4325
Message ((:#)),
4426
MonadLogger (monadLoggerLog),
4527
logInfo,
4628
logWarn,
4729
runLoggerLoggingT,
4830
(.=),
4931
)
50-
import Blammo.Logging.Simple (newLoggerEnv)
5132
import Control.Concurrent (threadDelay)
5233
import Control.Monad.IO.Class (MonadIO (liftIO))
5334
import Control.Monad.Reader (MonadReader, ReaderT, asks, runReaderT)
5435
import Control.Monad.Trans (lift)
5536
import Data.Aeson (ToJSON (toJSON), object)
56-
import Data.Pool (Pool)
5737
import Data.String.Conversions (cs)
5838
import Data.Text (Text)
59-
import Database.PostgreSQL.Simple (Connection)
6039
import GHC.Generics (Generic)
61-
import Lens.Micro (lens)
6240
import Network.HTTP.Req
63-
( HttpConfig,
64-
MonadHttp (getHttpConfig, handleHttpException),
65-
defaultHttpConfig,
41+
( MonadHttp (getHttpConfig, handleHttpException),
6642
)
6743
import Network.HTTP.Types (Status, status404, status409, status500)
6844
import UnliftIO (MonadUnliftIO, throwIO)
@@ -82,60 +58,27 @@ import Web.Scotty.Trans
8258
status,
8359
)
8460

85-
data App = App
86-
{ appConfig :: Config,
87-
appLogger :: Logger,
88-
appHttpConfig :: HttpConfig,
89-
appDbPool :: Pool Connection
90-
}
91-
92-
instance HasLogger App where
93-
loggerL = lens appLogger $ \x y -> x {appLogger = y}
94-
95-
instance HasCartConfig App where
96-
getBookingUrl = configBookingUrl . appConfig
97-
getBookingDelay = configBookingDelay . appConfig
98-
getPaymentUrl = configPaymentUrl . appConfig
99-
getPaymentDelay = configPaymentDelay . appConfig
100-
101-
instance HasDbPool App where
102-
getDbPool = appDbPool
103-
104-
appInit :: IO App
105-
appInit = do
106-
config <- configInit
107-
logger <- newLoggerEnv
108-
dbPool <- dbInit $ configDatabaseUrl config
109-
let app =
110-
App
111-
{ appConfig = config,
112-
appLogger = logger,
113-
appHttpConfig = defaultHttpConfig,
114-
appDbPool = dbPool
115-
}
116-
pure app
117-
118-
newtype AppM a = AppM
119-
{ unAppM :: ReaderT App (LoggingT IO) a
61+
newtype App a = App
62+
{ unApp :: ReaderT AppEnv (LoggingT IO) a
12063
}
121-
deriving (Functor, Applicative, Monad, MonadIO, MonadReader App, MonadUnliftIO)
64+
deriving (Functor, Applicative, Monad, MonadIO, MonadReader AppEnv, MonadUnliftIO)
12265

123-
instance MonadLogger AppM where
66+
instance MonadLogger App where
12467
monadLoggerLog loc logSource logLevel msg =
125-
AppM $ lift $ monadLoggerLog loc logSource logLevel msg
68+
App $ lift $ monadLoggerLog loc logSource logLevel msg
12669

12770
instance (ScottyError e, MonadLogger m) => MonadLogger (ActionT e m) where
12871
monadLoggerLog loc logSource logLevel msg =
12972
lift $ monadLoggerLog loc logSource logLevel msg
13073

131-
instance MonadHttp AppM where
74+
instance MonadHttp App where
13275
handleHttpException = throwIO
133-
getHttpConfig = asks appHttpConfig
76+
getHttpConfig = asks appEnvHttpConfig
13477

135-
runApp :: AppM a -> IO a
78+
runApp :: App a -> IO a
13679
runApp m = do
137-
app <- appInit
138-
runLoggerLoggingT app $ runReaderT (unAppM m) app
80+
app <- appEnvInit
81+
runLoggerLoggingT app $ runReaderT (unApp m) app
13982

14083
data CartPurchaseResponse = CartPurchaseResponse
14184
{ cartPurchaseResponseCartId :: CartId,
@@ -148,7 +91,7 @@ data CartPurchaseResponse = CartPurchaseResponse
14891
instance ToJSON CartPurchaseResponse where
14992
toJSON = defaultToJSON "cartPurchaseResponse"
15093

151-
postCartPurchaseHandler :: ActionT AppError AppM ()
94+
postCartPurchaseHandler :: ActionT AppError App ()
15295
postCartPurchaseHandler = do
15396
cartId <- CartId <$> param "cartId"
15497
cartStatusMaybe <- lift $ getCartStatus cartId
@@ -164,10 +107,10 @@ postCartPurchaseHandler = do
164107
raiseAppError status409 "Cart locked"
165108
Just CartStatusOpen -> do
166109
logInfo $ "Cart purchase starting" :# ["cart_id" .= cartId]
167-
let purchase :: AppM CartPurchaseResponse
110+
let purchase :: App CartPurchaseResponse
168111
purchase = do
169112
(bookingId, paymentId) <- concurrently (processBooking cartId) (processPayment cartId)
170-
purchaseDelay <- asks (configPurchaseDelay . appConfig)
113+
purchaseDelay <- asks (configPurchaseDelay . appEnvConfig)
171114
liftIO $ threadDelay purchaseDelay
172115
markCartAsPurchased cartId
173116
pure $
@@ -178,10 +121,10 @@ postCartPurchaseHandler = do
178121
cartPurchaseResponsePaymentId = paymentId
179122
}
180123

181-
action :: AppM (Either Text CartPurchaseResponse)
124+
action :: App (Either Text CartPurchaseResponse)
182125
action = Right <$> withCart cartId purchase
183126

184-
handleError :: CartException -> AppM (Either Text CartPurchaseResponse)
127+
handleError :: CartException -> App (Either Text CartPurchaseResponse)
185128
handleError (CartException msg) = pure $ Left msg
186129

187130
result <- lift $ catch action handleError
@@ -222,7 +165,7 @@ handleNotFound = do
222165
msg = "Path not found"
223166
json $ object ["error" .= msg]
224167

225-
application :: ScottyT AppError AppM ()
168+
application :: ScottyT AppError App ()
226169
application = do
227170
defaultHandler handleException
228171
post "/cart/:cartId/purchase" postCartPurchaseHandler

0 commit comments

Comments
 (0)