MyNixOS website logo
Description

A functional web framework.

A Haskell web framework where you write plain old functions.

Provided you havestackinstalled, you can run this example like a shell script (it'll listen on port 3000):

#!/usr/bin/env stack
-- stack --resolver lts-5.5 --install-ghc runghc --package fn --package warp
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid ((<>))
import Data.Text (Text)
import Network.Wai (Response)
import Network.Wai.Handler.Warp (run)
import Web.Fn
data Ctxt = Ctxt { _req :: FnRequest }
instance RequestContext Ctxt where
getRequest = _req
setRequest c r = c { _req = r }
initializer :: IO Ctxt
initializer = return (Ctxt defaultFnRequest)
main :: IO ()
main = do ctxt <- initializer
run 3000 $ toWAI ctxt site
site :: Ctxt -> IO Response
site ctxt = route ctxt [ end ==> indexH
, path "echo" // param "msg" ==> echoH
, path "echo" // segment ==> echoH
]
`fallthrough` notFoundText "Page not found."
indexH :: Ctxt -> IO (Maybe Response)
indexH _ = okText "Try visiting /echo?msg=hello or /echo/hello"
echoH :: Ctxt -> Text -> IO (Maybe Response)
echoH _ msg = okText $ "Echoing '" <> msg <> "'."

Fn lets you write web code that just looks like normal Haskell code.

  • An application has some "context", which must contain a Request, but can contain other data as well, like database connection pools, etc. This context will be passed to each of your handlers, updated with the current HTTP Request.

  • Routes are declared to capture parameters and/or segments of the url, and then routed to handler functions that have the appropriate number and type of arguments. These functions return IO (Maybe Response), where Nothing indicates to Fn that you want it to keep looking for matching routes.

  • All handlers just use plain old IO, which means it is easy to call them from GHCi, forkIO, etc.

  • All of this is a small wrapper around the WAI interface, so you have the flexilibility to do anything you need to do with HTTP.

The name comes from the fact that Fn emphasizes functions (over monads), where all necessary data is passed via function arguments, and control flow is mediated by return values.

Fn (eff-enn) - a functional web framework.

Or, how to do away with the monad transformers, and just use plain functions.

Example

See the example application in the repository for a full usage including database access, heist templates, sessions, etc.

Metadata

Version

0.3.0.2

License

Platforms (75)

    Darwin
    FreeBSD
    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-freebsd
  • 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-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows