File tree Expand file tree Collapse file tree 17 files changed +78
-29
lines changed Expand file tree Collapse file tree 17 files changed +78
-29
lines changed Original file line number Diff line number Diff line change 1+ -- | Main entry point for the layer
12module Main
23 ( main
34 ) where
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -17,8 +17,11 @@ import qualified Aws.Lambda.Meta.Main as Main
1717import qualified Aws.Lambda.Meta.Run as Run
1818import qualified Aws.Lambda.Runtime.IPC as IPC
1919
20+ {-| Generates a @main@ function to be used with the
21+ AWS Lambda layer.
22+ -}
2023configureLambda :: Meta. DecsQ
2124configureLambda = do
2225 main <- Main. generate
2326 run <- Run. generate
24- return $ main <> [run]
27+ return ( main <> [run])
Original file line number Diff line number Diff line change 1+ {-| Helper functions to make code generation easier -}
12module Aws.Lambda.Meta.Common
23 ( declarationName
34 , expressionName
Original file line number Diff line number Diff line change 1+ {-| Discovery of AWS Lambda handlers
2+ A handler is basically a function that has a type definition that
3+ starts with "handler :: ".
4+ -}
15module Aws.Lambda.Meta.Discover
2- (handlers ) where
6+ ( handlers
7+ ) where
38
49import qualified Control.Monad as Monad
510import Data.Function ((&) )
@@ -10,6 +15,13 @@ import qualified Data.Text as Text
1015import Path
1116import qualified Path.IO as PathIO
1217
18+ {-| Returns a list of handler paths that look like
19+
20+ @src/Foo/Bar/Quux.handler@
21+
22+ It is the path to the source file, but changing the
23+ extension for ".handler"
24+ -}
1325handlers :: IO [Text ]
1426handlers = do
1527 (_, files) <- PathIO. listDirRecurRel [reldir |.|]
Original file line number Diff line number Diff line change 1+ {-| Dispatcher generation -}
12module Aws.Lambda.Meta.Dispatch
23 ( generate
34 , decodeObj
@@ -13,16 +14,27 @@ import qualified Language.Haskell.TH as Meta
1314
1415import Aws.Lambda.Meta.Common
1516
17+ {-| Helper function that the dispatcher will use to
18+ decode the JSON that comes as an AWS Lambda event into the
19+ appropriate type expected by the handler.
20+ -}
1621decodeObj :: FromJSON a => String -> a
1722decodeObj x =
1823 case (eitherDecode $ LazyByteString. pack x) of
1924 Left e -> error e
2025 Right v -> v
2126
27+ {-| Generates the dispatcher out of a list of
28+ handler names in the form @src/Foo/Bar.handler@
29+
30+ This dispatcher has a case for each of the handlers that calls
31+ the appropriate qualified function. In the case of the example above,
32+ the dispatcher will call @Foo.Bar.handler@.
33+ -}
2234generate :: [Text ] -> Meta. ExpQ
23- generate fileNames = do
35+ generate handlerNames = do
2436 caseExp <- expressionName " functionHandler"
25- matches <- traverse handlerCase fileNames
37+ matches <- traverse handlerCase handlerNames
2638 unmatched <- unmatchedCase
2739 pure $ Meta. CaseE caseExp (matches <> [unmatched])
2840
Original file line number Diff line number Diff line change 1+ {-| main function generation for interoperation with the layer -}
12module Aws.Lambda.Meta.Main
23 ( LambdaOptions (.. )
34 , generate
@@ -11,13 +12,15 @@ import qualified Options.Generic as Options
1112
1213import Aws.Lambda.Meta.Common
1314
15+ -- | Options that the generated main expects
1416data LambdaOptions = LambdaOptions
1517 { eventObject :: ! String
1618 , contextObject :: ! String
1719 , functionHandler :: ! String
1820 , executionUuid :: ! String
1921 } deriving (Generic , Options.ParseRecord )
2022
23+ -- | Generate the main function that the layer will call
2124generate :: Meta. DecsQ
2225generate = [d |
2326 $(declarationName "main") = getRecord "" >>= run
Original file line number Diff line number Diff line change @@ -8,6 +8,12 @@ import Aws.Lambda.Meta.Common
88import qualified Aws.Lambda.Meta.Discover as Discover
99import qualified Aws.Lambda.Meta.Dispatch as Dispatch
1010
11+ {-| Generate the run function
12+
13+ It will create a dispatcher that is a huge @case@ expression that
14+ expects the name of the handler provided by AWS Lambda, and will
15+ execute the appropriate user function
16+ -}
1117generate :: Meta. DecQ
1218generate = do
1319 handlers <- Meta. runIO Discover. handlers
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ import qualified Aws.Lambda.Runtime.Error as Error
1212import qualified Aws.Lambda.Runtime.IPC as IPC
1313import qualified Aws.Lambda.Runtime.Publish as Publish
1414
15+ -- | Runs the user @haskell_lambda@ executable and posts back the
16+ -- results
1517runLambda
1618 :: Http. Manager
1719 -> IO ()
You can’t perform that action at this time.
0 commit comments