/
Yesod.hs
172 lines (157 loc) · 5.35 KB
/
Yesod.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module App.Yesod
( main,
-- Suppress warning for unused top-level bindings generated by TH
resourcesAppEnv,
Widget,
)
where
import App.AppEnv
( AppEnv (appEnvConfig, appEnvHttpConfig, appEnvLogFunc),
appEnvInit,
)
import App.Cart
( BookingId,
CartException (CartException),
CartId (CartId),
CartStatus (CartStatusLocked, CartStatusOpen, CartStatusPurchased),
HasCartConfig
( getBookingDelay,
getBookingUrl,
getPaymentDelay,
getPaymentUrl
),
PaymentId,
getCartStatus,
markCartAsPurchased,
processBooking,
processPayment,
withCart,
)
import App.Config (Config (configPurchaseDelay))
import App.Db (HasDbPool (getDbPool))
import App.Json (defaultToJSON)
import Blammo.Logging
( Message ((:#)),
logInfo,
logWarn,
(.=),
)
import Control.Concurrent (threadDelay)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Aeson (ToJSON (toJSON), object)
import Data.Text (Text)
import GHC.Generics (Generic)
import Network.HTTP.Req
( MonadHttp (getHttpConfig, handleHttpException),
)
import Network.HTTP.Types (Status, status404, status409, status500)
import Network.Wai.Handler.Warp (run)
import UnliftIO (throwIO)
import UnliftIO.Async (concurrently)
import UnliftIO.Exception (catch)
import Yesod.Core
( ErrorResponse (InternalError, NotFound),
MonadHandler,
PathPiece,
ToTypedContent (toTypedContent),
Yesod (errorHandler, makeSessionBackend, messageLoggerSource),
defaultErrorHandler,
getsYesod,
mkYesod,
parseRoutes,
renderRoute,
sendStatusJSON,
toWaiAppPlain,
)
import Yesod.Core.Types
( HandlerData (handlerEnv),
JSONResponse (JSONResponse),
RunHandlerEnv (rheSite),
)
instance HasCartConfig (HandlerData AppEnv AppEnv) where
getBookingUrl = getBookingUrl . rheSite . handlerEnv
getBookingDelay = getBookingDelay . rheSite . handlerEnv
getPaymentUrl = getPaymentUrl . rheSite . handlerEnv
getPaymentDelay = getPaymentDelay . rheSite . handlerEnv
instance HasDbPool (HandlerData AppEnv AppEnv) where
getDbPool = getDbPool . rheSite . handlerEnv
mkYesod
"AppEnv"
[parseRoutes|
/cart/#CartId/purchase CartPurchaseR POST
|]
deriving instance PathPiece CartId
instance MonadHttp Handler where
handleHttpException = throwIO
getHttpConfig = getsYesod appEnvHttpConfig
instance Yesod AppEnv where
messageLoggerSource app _logger loc source level msg = do
let logFunc = appEnvLogFunc app
logFunc loc source level msg
makeSessionBackend _ = return Nothing
errorHandler NotFound = do
let msg :: Text
msg = "Path not found"
pure $ toTypedContent $ object ["error" .= msg]
errorHandler (InternalError msg) = do
pure $ toTypedContent $ object ["error" .= msg]
errorHandler e = defaultErrorHandler e
sendStatusError :: (MonadHandler m) => Status -> Text -> m a
sendStatusError s msg = sendStatusJSON s $ object ["error" .= msg]
data CartPurchaseResponse = CartPurchaseResponse
{ cartPurchaseResponseCartId :: CartId,
cartPurchaseResponsePurchaseDelay :: Int,
cartPurchaseResponseBookingId :: BookingId,
cartPurchaseResponsePaymentId :: PaymentId
}
deriving (Generic)
instance ToJSON CartPurchaseResponse where
toJSON = defaultToJSON "cartPurchaseResponse"
postCartPurchaseR :: CartId -> Handler (JSONResponse CartPurchaseResponse)
postCartPurchaseR cartId = do
cartStatusMaybe <- getCartStatus cartId
case cartStatusMaybe of
Nothing -> do
logWarn $ "Cart does not exist" :# ["cart_id" .= cartId]
sendStatusError status404 "Cart does not exist"
Just CartStatusPurchased -> do
logWarn $ "Cart already purchased" :# ["cart_id" .= cartId]
sendStatusError status409 "Cart already purchased"
Just CartStatusLocked -> do
logWarn $ "Cart locked" :# ["cart_id" .= cartId]
sendStatusError status409 "Cart locked"
Just CartStatusOpen -> do
withCart cartId $ do
logInfo $ "Cart purchase starting" :# ["cart_id" .= cartId]
let action :: Handler (Either Text (BookingId, PaymentId))
action = Right <$> concurrently (processBooking cartId) (processPayment cartId)
handleError :: CartException -> Handler (Either Text (BookingId, PaymentId))
handleError (CartException msg) = pure $ Left msg
result <- catch action handleError
case result of
Left msg -> do
logWarn $ ("Cart purchase failed: " <> msg) :# ["cart_id" .= cartId]
sendStatusError status500 ("Cart purchase failed: " <> msg)
Right (bookingId, paymentId) -> do
purchaseDelay <- getsYesod (configPurchaseDelay . appEnvConfig)
liftIO $ threadDelay (100 * 1000)
markCartAsPurchased cartId
logInfo $ "Cart purchase successful" :# ["cart_id" .= cartId]
pure $
JSONResponse $
CartPurchaseResponse
{ cartPurchaseResponseCartId = cartId,
cartPurchaseResponsePurchaseDelay = purchaseDelay,
cartPurchaseResponseBookingId = bookingId,
cartPurchaseResponsePaymentId = paymentId
}
main :: IO ()
main = do
app <- appEnvInit
waiApp <- toWaiAppPlain app
let port = 3000
run port waiApp