Skip to content

Commit 5cd48ed

Browse files
committed
Add runMultipleWaiApplications.
1 parent a75767a commit 5cd48ed

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

Diff for: ChangeLog.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.0.2
44

55
* Make registering multiple wai handlers possible (via `waiHandler`).
6+
* Make registering multiple handlers with multiple Wai Applications possible (via `runMultipleWaiApplications`).
67
* Use version `4.1.1` of `aws-lambda-haskell-runtime`.
78

89
## 2.0.1

Diff for: 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

Diff for: src/Aws/Lambda/Wai.hs

+35-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ module Aws.Lambda.Wai
1414
ALBWaiHandler,
1515
ignoreALBPathPart,
1616
ignoreNothing,
17-
waiHandler
17+
waiHandler,
18+
runMultipleWaiApplications,
1819
)
1920
where
2021

2122
import Aws.Lambda
2223
import Control.Concurrent.MVar
24+
import Control.Monad (forM, forM_)
2325
import Data.Aeson
2426
import Data.Aeson.Types
2527
import Data.Bifunctor (Bifunctor (bimap))
@@ -28,6 +30,7 @@ import Data.ByteString (ByteString)
2830
import qualified Data.ByteString as BS
2931
import qualified Data.ByteString.Lazy as BL
3032
import qualified Data.CaseInsensitive as CI
33+
import Data.HashMap.Strict (HashMap)
3134
import qualified Data.HashMap.Strict as HMap
3235
import Data.IORef
3336
import qualified Data.IP as IP
@@ -67,6 +70,37 @@ runWaiAsProxiedHttpLambda options ignoredAlbPath handlerName mkApp =
6770
runLambdaHaskellRuntime options mkApp id $
6871
addStandaloneLambdaHandler handlerName (waiHandler ignoredAlbPath)
6972

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+
70104
waiHandler ::
71105
Maybe ALBIgnoredPathPortion ->
72106
GenericWaiHandler

Diff for: stack.yaml

+2-1
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.
@@ -41,6 +41,7 @@ packages:
4141
#
4242
extra-deps:
4343
- 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

Diff for: stack.yaml.lock

+11-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ packages:
1111
sha256: b7e8426edb0b5d06713a26362308abbec03379e1f3ad9c6d63973088ad75cf62
1212
original:
1313
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)