theam / aws-lambda-haskell-runtime

⚡Haskell runtime for AWS Lambda
https://theam.github.io/aws-lambda-haskell-runtime/
Other
269 stars 48 forks source link

Version 4.0.0 - API redesign - no TH + native ALB support #97

Closed dnikolovv closed 3 years ago

dnikolovv commented 3 years ago

Since https://github.com/theam/aws-lambda-haskell-runtime/pull/81 went stale and this is a needed feature, after a discussion with @NickSeagull we decided that I could tackle it.

This is version 4.0.0 - the API is completely redesigned and you no longer need template Haskell. Also, you can write your handler code in any monad.

You can now do this:

initializeConfig :: IO Config
initializeConfig = implementation

main :: IO ()
main = do
  let options = defaultDispatcherOptions

  -- "id" is being used as `m a -> IO a`
  runLambdaHaskellRuntime options initializeConfig id $ do
    addALBHandler "handler" $ \(request :: ALBRequest someRequestType) (context :: IORef Config) ->

  -- or if you want to use API Gateway
  runLambdaHaskellRuntime options initializeConfig id $ do
    addAPIGatewayHandler "handler" $ \(request :: ApiGateway someRequestType) (context :: IORef Config) ->

  -- or if you want to use a standalone lambda
  runLambdaHaskellRuntime options initializeConfig id $ do
    addStandaloneLambdaHandler "handler" $ \(request :: someRequestType) (context :: IORef Config) ->

Using aws-lambda-haskell-runtime-wai, you can now support both API Gateway and ALB like so:

mkApp :: IO Wai.Application
mkApp = implementation

main :: IO ()
main = do
  let options = defaultDispatcherOptions

  runWaiAsLambda
    options
    "handler"
    -- If you want to load-balance multiple lambdas using the same balancer, for each lambda you can
    -- specify the ignored path part so your routes don't break.
    (ignoreALBPathPart "my-lambda")
    mkApp
NickSeagull commented 3 years ago

Hey, what's the status on this?

freuk commented 3 years ago

This change looks great, I'm using it and am looking forwards to having it in the upcoming release if it gets merged. Thanks.

dnikolovv commented 3 years ago

The initial template has been updated and I'm halfway through the docs. Should finish it this Saturday.

dnikolovv commented 3 years ago

@NickSeagull The docs are really not perfect (or well done), but I guess we can roll with it and update those at a later stage.

NickSeagull commented 3 years ago

Thanks a lot!!!! ❤️