Serverless

Use httpApi to define a Program that responds to HTTP requests. Take a look at the demos for usage examples.

Table of Contents

Defining a Program

Use httpApi to define a headless Elm program.


httpApi : Serverless.HttpApi config model route msg -> Serverless.Program config model route msg

Create a program from the given HTTP api.


type alias HttpApi config model route msg =

{ configDecoder : Json.Decode.Decoder config, initialModel : model, parseRoute : Url.Url -> Maybe.Maybe route, endpoint : Serverless.Conn.Conn config model route -> ( Serverless.Conn.Conn config model route, Platform.Cmd.Cmd msg ), update : msg -> Serverless.Conn.Conn config model route -> ( Serverless.Conn.Conn config model route, Platform.Cmd.Cmd msg ), requestPort : Serverless.RequestPort (Serverless.Msg msg), responsePort : Serverless.ResponsePort (Serverless.Msg msg) }

Program for an HTTP API.

A Serverless.Program is parameterized by your 5 custom types

  • config is a server load-time record of deployment specific values
  • model is for whatever you need during the processing of a request
  • route represents your application routes
  • msg is your app message type

You must provide the following:

  • configDecoder decodes a JSON value for your custom config type
  • requestPort and responsePort must be defined in your app since an elm library cannot expose ports
  • initialModel is a value to which new connections will set their model
  • parseRoute takes the request/path/and?query=string and parses it into a route
  • endpoint is a function which receives incoming connections
  • update the app update function

Notices that update and endpoint operate on Conn config model route and not just on model.


type alias Program config model route msg =

Platform.Program Serverless.Flags (Serverless.Model config model route) (Serverless.Msg msg)

Serverless program type.

This maps to a headless elm Platform.Program.


Port Types

Since a library cannot expose ports, your application must define two ports with the following signatures. See the Hello World Demo for a usage example.


type alias RequestPort msg =

(Serverless.IO -> msg) -> Platform.Sub.Sub msg

Type of port through which the request is received. Set your request port to this type.

port requestPort : RequestPort msg

type alias ResponsePort msg =

Serverless.IO -> Platform.Cmd.Cmd msg

Type of port through which the request is sent. Set your response port to this type.

port responsePort : ResponsePort msg

Initialization Helpers

Various aspects of Program may not be needed. These functions are provided as a convenient way to opt-out.


noConfig : Json.Decode.Decoder ()

Opt-out of configuration decoding.

main : Serverless.Program () model route msg
main =
    Serverless.httpApi
        { configDecoder = noConfig

        -- ...
        }

noRoutes : Url.Url -> Maybe.Maybe ()

Opt-out of route parsing.

main : Serverless.Program config model () msg
main =
    Serverless.httpApi
        { parseRoute = noRoutes

        -- ...
        }

noSideEffects : () -> Serverless.Conn.Conn config model route -> ( Serverless.Conn.Conn config model route, Platform.Cmd.Cmd () )

Opt-out of side-effects.

main : Serverless.Program config model route ()
main =
    Serverless.httpApi
        { update = noSideEffects

        -- ...
        }