Skip to content

Commit d9d4668

Browse files
authored
Merge pull request #10 from eir-forsakring/v2.0.2
v2.0.2
2 parents fa46211 + 5cd48ed commit d9d4668

6 files changed

+86
-30
lines changed

ChangeLog.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
# Changelog for aws-lambda-haskell-runtime-wai
22

3+
## 2.0.2
4+
5+
* Make registering multiple wai handlers possible (via `waiHandler`).
6+
* Make registering multiple handlers with multiple Wai Applications possible (via `runMultipleWaiApplications`).
7+
* Use version `4.1.1` of `aws-lambda-haskell-runtime`.
8+
39
## 2.0.1
410

5-
* Using version `4.1.0` of `aws-lambda-haskell-runtime` that fixes [#101](https://github.com/theam/aws-lambda-haskell-runtime/issues/101).
11+
* Use version `4.1.0` of `aws-lambda-haskell-runtime` that fixes [#101](https://github.com/theam/aws-lambda-haskell-runtime/issues/101).
612

713
## 2.0.0
814

9-
* Using version [`4.0.0`](https://github.com/theam/aws-lambda-haskell-runtime/pull/97) of `aws-lambda-haskell-runtime`.
15+
* Use version [`4.0.0`](https://github.com/theam/aws-lambda-haskell-runtime/pull/97) of `aws-lambda-haskell-runtime`.
1016
* New handler types that allow you to support ALB or even API Gateway + ALB at once.
1117

1218
## 1.0.3

aws-lambda-haskell-runtime-wai.cabal

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.33.0.
3+
-- This file has been generated from package.yaml by hpack version 0.34.4.
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 7ac059dff419276eecdd9bc3935ba4601d6b8e0ea70b452264ca490df133487a
7+
-- hash: 4f70c32a7c39e85a18faf7ec170f65c5e46116d8f23661e3aa358ca1b1a913b9
88

99
name: aws-lambda-haskell-runtime-wai
10-
version: 2.0.1
10+
version: 2.0.2
1111
synopsis: Run wai applications on AWS Lambda
1212
description: Please see the README on GitHub at <https://github.com/eir-forsakring/aws-lambda-haskell-runtime-wai#readme>
1313
category: AWS

package.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: aws-lambda-haskell-runtime-wai
2-
version: 2.0.1
2+
version: 2.0.2
33
github: "eir-forsakring/aws-lambda-haskell-runtime-wai"
44
license: BSD3
55
author: "Dobromir Nikolov"

src/Aws/Lambda/Wai.hs

+57-15
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ module Aws.Lambda.Wai
1414
ALBWaiHandler,
1515
ignoreALBPathPart,
1616
ignoreNothing,
17+
waiHandler,
18+
runMultipleWaiApplications,
1719
)
1820
where
1921

2022
import Aws.Lambda
2123
import Control.Concurrent.MVar
24+
import Control.Monad (forM, forM_)
2225
import Data.Aeson
2326
import Data.Aeson.Types
2427
import Data.Bifunctor (Bifunctor (bimap))
@@ -27,6 +30,7 @@ import Data.ByteString (ByteString)
2730
import qualified Data.ByteString as BS
2831
import qualified Data.ByteString.Lazy as BL
2932
import qualified Data.CaseInsensitive as CI
33+
import Data.HashMap.Strict (HashMap)
3034
import qualified Data.HashMap.Strict as HMap
3135
import Data.IORef
3236
import qualified Data.IP as IP
@@ -48,6 +52,8 @@ type ApiGatewayWaiHandler = ApiGatewayRequest Text -> Context Application -> IO
4852

4953
type ALBWaiHandler = ALBRequest Text -> Context Application -> IO (Either (ALBResponse Text) (ALBResponse Text))
5054

55+
type GenericWaiHandler = Value -> Context Application -> IO (Either Value Value)
56+
5157
newtype ALBIgnoredPathPortion = ALBIgnoredPathPortion {unALBIgnoredPathPortion :: Text}
5258

5359
data WaiLambdaProxyType
@@ -62,21 +68,57 @@ runWaiAsProxiedHttpLambda ::
6268
IO ()
6369
runWaiAsProxiedHttpLambda options ignoredAlbPath handlerName mkApp =
6470
runLambdaHaskellRuntime options mkApp id $
65-
addStandaloneLambdaHandler handlerName $ \(request :: Value) context ->
66-
case parse parseIsAlb request of
67-
Success isAlb -> do
68-
if isAlb
69-
then case fromJSON @(ALBRequest Text) request of
70-
Success albRequest ->
71-
bimap toJSON toJSON <$> albWaiHandler ignoredAlbPath albRequest context
72-
Error err -> error $ "Could not parse the request as a valid ALB request: " <> err
73-
else case fromJSON @(ApiGatewayRequest Text) request of
74-
Success apiGwRequest ->
75-
bimap toJSON toJSON <$> apiGatewayWaiHandler apiGwRequest context
76-
Error err -> error $ "Could not parse the request as a valid API Gateway request: " <> err
77-
Error err ->
78-
error $
79-
"Could not parse the request as a valid API Gateway or ALB proxy request: " <> err
71+
addStandaloneLambdaHandler handlerName (waiHandler ignoredAlbPath)
72+
73+
runMultipleWaiApplications ::
74+
DispatcherOptions ->
75+
HashMap HandlerName (Maybe ALBIgnoredPathPortion, IO Application) ->
76+
IO ()
77+
runMultipleWaiApplications options handlersAndApps = do
78+
runLambdaHaskellRuntime options initializeApplications id $
79+
forM_ (HMap.keys handlersAndApps) $ \handler ->
80+
addStandaloneLambdaHandler handler $ \request context ->
81+
multiApplicationWaiHandler handler request context
82+
where
83+
initializeApplications :: IO (HashMap HandlerName (Maybe ALBIgnoredPathPortion, Application))
84+
initializeApplications = do
85+
HMap.fromList
86+
<$> forM
87+
(HMap.toList handlersAndApps)
88+
(\(handler, (alb, mkApp)) -> mkApp >>= \app -> return (handler, (alb, app)))
89+
90+
multiApplicationWaiHandler ::
91+
HandlerName ->
92+
Value ->
93+
Context (HashMap HandlerName (Maybe ALBIgnoredPathPortion, Application)) ->
94+
IO (Either Value Value)
95+
multiApplicationWaiHandler handlerName request context = do
96+
appMay <- HMap.lookup handlerName <$> readIORef (customContext context)
97+
case appMay of
98+
Just (ignoredAlbPart, app) -> do
99+
applicationRef <- newIORef app
100+
waiHandler ignoredAlbPart request (context {customContext = applicationRef})
101+
Nothing ->
102+
fail $ "No application was registered for handler '" <> T.unpack (unHandlerName handlerName) <> "'."
103+
104+
waiHandler ::
105+
Maybe ALBIgnoredPathPortion ->
106+
GenericWaiHandler
107+
waiHandler ignoredAlbPath request context =
108+
case parse parseIsAlb request of
109+
Success isAlb -> do
110+
if isAlb
111+
then case fromJSON @(ALBRequest Text) request of
112+
Success albRequest ->
113+
bimap toJSON toJSON <$> albWaiHandler ignoredAlbPath albRequest context
114+
Error err -> error $ "Could not parse the request as a valid ALB request: " <> err
115+
else case fromJSON @(ApiGatewayRequest Text) request of
116+
Success apiGwRequest ->
117+
bimap toJSON toJSON <$> apiGatewayWaiHandler apiGwRequest context
118+
Error err -> error $ "Could not parse the request as a valid API Gateway request: " <> err
119+
Error err ->
120+
error $
121+
"Could not parse the request as a valid API Gateway or ALB proxy request: " <> err
80122
where
81123
parseIsAlb :: Value -> Parser Bool
82124
parseIsAlb = withObject "Request" $ \obj -> do

stack.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#
1818
# resolver: ./custom-snapshot.yaml
1919
# resolver: https://example.com/snapshots/2018-01-01.yaml
20-
resolver: lts-16.12
20+
resolver: nightly-2021-04-06
2121

2222
# User packages to be built.
2323
# Various formats can be used as shown in the example below.
@@ -40,7 +40,8 @@ packages:
4040
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
4141
#
4242
extra-deps:
43-
- aws-lambda-haskell-runtime-4.1.0@sha256:4fbf9855326ed0173c3ef27c006412c8da46f84b0566fa92a44ad0e988432d82,2916
43+
- aws-lambda-haskell-runtime-4.1.1@sha256:c3b3452d3a7f36950c22760fe44b51d49b43624fa3a0253e5bfc3d94809bb8fd,3024
44+
- safe-exceptions-checked-0.1.0@sha256:ae142f284c6b102f0e7606a69a5719f4f72771d9cc353af72fa4484326689100,1603
4445
# Override default flag values for local packages and extra-deps
4546
# flags: {}
4647

stack.yaml.lock

+14-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@
55

66
packages:
77
- completed:
8-
hackage: aws-lambda-haskell-runtime-4.1.0@sha256:4fbf9855326ed0173c3ef27c006412c8da46f84b0566fa92a44ad0e988432d82,2916
8+
hackage: aws-lambda-haskell-runtime-4.1.1@sha256:c3b3452d3a7f36950c22760fe44b51d49b43624fa3a0253e5bfc3d94809bb8fd,3024
99
pantry-tree:
1010
size: 1453
11-
sha256: c01167c71d6e25a33b822629d5efc50818589e33e5341ee1bea1bb4e2c59f9c6
11+
sha256: b7e8426edb0b5d06713a26362308abbec03379e1f3ad9c6d63973088ad75cf62
1212
original:
13-
hackage: aws-lambda-haskell-runtime-4.1.0@sha256:4fbf9855326ed0173c3ef27c006412c8da46f84b0566fa92a44ad0e988432d82,2916
13+
hackage: aws-lambda-haskell-runtime-4.1.1@sha256:c3b3452d3a7f36950c22760fe44b51d49b43624fa3a0253e5bfc3d94809bb8fd,3024
14+
- completed:
15+
hackage: safe-exceptions-checked-0.1.0@sha256:ae142f284c6b102f0e7606a69a5719f4f72771d9cc353af72fa4484326689100,1603
16+
pantry-tree:
17+
size: 298
18+
sha256: d1f8ba50967547f26cdc37cbc64bdc85af57d856986876a6a5767a61977bc299
19+
original:
20+
hackage: safe-exceptions-checked-0.1.0@sha256:ae142f284c6b102f0e7606a69a5719f4f72771d9cc353af72fa4484326689100,1603
1421
snapshots:
1522
- completed:
16-
size: 532377
17-
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/12.yaml
18-
sha256: f914cfa23fef85bdf895e300a8234d9d0edc2dbec67f4bc9c53f85867c50eab6
19-
original: lts-16.12
23+
size: 576531
24+
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/nightly/2021/4/6.yaml
25+
sha256: 6ce168aed6ba23cfe148061587ba1c0f24a09d4cad8b1a5a29b21878e33d6ef4
26+
original: nightly-2021-04-06

0 commit comments

Comments
 (0)