MyNixOS website logo
Description

Please see the README on Github at https://git.localcooking.com/tooling/wai-transformers#readme.

wai-transformers

Simple parameterization of Wai's Application and Middleware types.

Overview

Wai's Application type is defined as follows:

type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived

This is great for the server - we can just flip ($) the middlewares together to get an effectful server. However, what if we want to encode useful information in our middleware chain / end-point application? Something like a ReaderT Env environment, where useful data like a universal salt, current hostname, or global mutable references can be referenced later if it were wedged-in.

The design looks like this:

type ApplicationT m = Request -> (Response -> IO ResponseReceived) -> m ResponseReceived

Now we can encode MTL-style contexts with applications we write

type MiddlewareT m = ApplicationT m -> ApplicationT m


data AuthConfig = AuthConfig
  { authFunction :: Request -> Either AuthException (Response -> Response)
  }

simpleAuth :: ( MonadReader AuthConfig m
              , MonadError AuthException m
              ) => MiddlewareT m
simpleAuth app req resp = do
  auth <- authFunction <$> ask
  case auth req of
    Left e = throwError e
    Right f = app req (resp . f)

simpleAuth' :: Middleware
simpleAuth' app req resp =
  eReceived <- runExceptT $ runReaderT (simpleAuth app req resp) myAuthConfig
  case eReceived of
    Left e = resp $ respondLBS "Unauthorized!"
    Right r = return r
Metadata

Version

0.1.0

Platforms (75)

    Darwin
    FreeBSD 13
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd13
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd13
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows