packages feed

aws-lambda-haskell-runtime-wai 2.0.1 → 2.0.2

raw patch · 3 files changed

+68/−20 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Aws.Lambda.Wai: runMultipleWaiApplications :: DispatcherOptions -> HashMap HandlerName (Maybe ALBIgnoredPathPortion, IO Application) -> IO ()
+ Aws.Lambda.Wai: waiHandler :: Maybe ALBIgnoredPathPortion -> GenericWaiHandler

Files

ChangeLog.md view
@@ -1,12 +1,18 @@ # Changelog for aws-lambda-haskell-runtime-wai +## 2.0.2++* Make registering multiple wai handlers possible (via `waiHandler`).+* Make registering multiple handlers with multiple Wai Applications possible (via `runMultipleWaiApplications`).+* Use version `4.1.1` of `aws-lambda-haskell-runtime`.+ ## 2.0.1 -* Using version `4.1.0` of `aws-lambda-haskell-runtime` that fixes [#101](https://github.com/theam/aws-lambda-haskell-runtime/issues/101).+* Use version `4.1.0` of `aws-lambda-haskell-runtime` that fixes [#101](https://github.com/theam/aws-lambda-haskell-runtime/issues/101).  ## 2.0.0 -* Using version [`4.0.0`](https://github.com/theam/aws-lambda-haskell-runtime/pull/97) of `aws-lambda-haskell-runtime`.+* Use version [`4.0.0`](https://github.com/theam/aws-lambda-haskell-runtime/pull/97) of `aws-lambda-haskell-runtime`. * New handler types that allow you to support ALB or even API Gateway + ALB at once.  ## 1.0.3
aws-lambda-haskell-runtime-wai.cabal view
@@ -1,13 +1,13 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.33.0.+-- This file has been generated from package.yaml by hpack version 0.34.4. -- -- see: https://github.com/sol/hpack ----- hash: 0c921b944f50d20ea63e14b4ae81436263237616e3ef9faeea58316b51b17c4c+-- hash: 9a2e4373736f601efe4a2c23f0e22b8d6aef897ce2873b0a432430c7eb4a4a4f  name:           aws-lambda-haskell-runtime-wai-version:        2.0.1+version:        2.0.2 synopsis:       Run wai applications on AWS Lambda description:    Please see the README on GitHub at <https://github.com/eir-forsakring/aws-lambda-haskell-runtime-wai#readme> category:       AWS
src/Aws/Lambda/Wai.hs view
@@ -14,11 +14,14 @@     ALBWaiHandler,     ignoreALBPathPart,     ignoreNothing,+    waiHandler,+    runMultipleWaiApplications,   ) where  import Aws.Lambda import Control.Concurrent.MVar+import Control.Monad (forM, forM_) import Data.Aeson import Data.Aeson.Types import Data.Bifunctor (Bifunctor (bimap))@@ -27,6 +30,7 @@ import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as BL import qualified Data.CaseInsensitive as CI+import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as HMap import Data.IORef import qualified Data.IP as IP@@ -48,6 +52,8 @@  type ALBWaiHandler = ALBRequest Text -> Context Application -> IO (Either (ALBResponse Text) (ALBResponse Text)) +type GenericWaiHandler = Value -> Context Application -> IO (Either Value Value)+ newtype ALBIgnoredPathPortion = ALBIgnoredPathPortion {unALBIgnoredPathPortion :: Text}  data WaiLambdaProxyType@@ -62,21 +68,57 @@   IO () runWaiAsProxiedHttpLambda options ignoredAlbPath handlerName mkApp =   runLambdaHaskellRuntime options mkApp id $-    addStandaloneLambdaHandler handlerName $ \(request :: Value) context ->-      case parse parseIsAlb request of-        Success isAlb -> do-          if isAlb-            then case fromJSON @(ALBRequest Text) request of-              Success albRequest ->-                bimap toJSON toJSON <$> albWaiHandler ignoredAlbPath albRequest context-              Error err -> error $ "Could not parse the request as a valid ALB request: " <> err-            else case fromJSON @(ApiGatewayRequest Text) request of-              Success apiGwRequest ->-                bimap toJSON toJSON <$> apiGatewayWaiHandler apiGwRequest context-              Error err -> error $ "Could not parse the request as a valid API Gateway request: " <> err-        Error err ->-          error $-            "Could not parse the request as a valid API Gateway or ALB proxy request: " <> err+    addStandaloneLambdaHandler handlerName (waiHandler ignoredAlbPath)++runMultipleWaiApplications ::+  DispatcherOptions ->+  HashMap HandlerName (Maybe ALBIgnoredPathPortion, IO Application) ->+  IO ()+runMultipleWaiApplications options handlersAndApps = do+  runLambdaHaskellRuntime options initializeApplications id $+    forM_ (HMap.keys handlersAndApps) $ \handler ->+      addStandaloneLambdaHandler handler $ \request context ->+        multiApplicationWaiHandler handler request context+  where+    initializeApplications :: IO (HashMap HandlerName (Maybe ALBIgnoredPathPortion, Application))+    initializeApplications = do+      HMap.fromList+        <$> forM+          (HMap.toList handlersAndApps)+          (\(handler, (alb, mkApp)) -> mkApp >>= \app -> return (handler, (alb, app)))++multiApplicationWaiHandler ::+  HandlerName ->+  Value ->+  Context (HashMap HandlerName (Maybe ALBIgnoredPathPortion, Application)) ->+  IO (Either Value Value)+multiApplicationWaiHandler handlerName request context = do+  appMay <- HMap.lookup handlerName <$> readIORef (customContext context)+  case appMay of+    Just (ignoredAlbPart, app) -> do+      applicationRef <- newIORef app+      waiHandler ignoredAlbPart request (context {customContext = applicationRef})+    Nothing ->+      fail $ "No application was registered for handler '" <> T.unpack (unHandlerName handlerName) <> "'."++waiHandler ::+  Maybe ALBIgnoredPathPortion ->+  GenericWaiHandler+waiHandler ignoredAlbPath request context =+  case parse parseIsAlb request of+    Success isAlb -> do+      if isAlb+        then case fromJSON @(ALBRequest Text) request of+          Success albRequest ->+            bimap toJSON toJSON <$> albWaiHandler ignoredAlbPath albRequest context+          Error err -> error $ "Could not parse the request as a valid ALB request: " <> err+        else case fromJSON @(ApiGatewayRequest Text) request of+          Success apiGwRequest ->+            bimap toJSON toJSON <$> apiGatewayWaiHandler apiGwRequest context+          Error err -> error $ "Could not parse the request as a valid API Gateway request: " <> err+    Error err ->+      error $+        "Could not parse the request as a valid API Gateway or ALB proxy request: " <> err   where     parseIsAlb :: Value -> Parser Bool     parseIsAlb = withObject "Request" $ \obj -> do