The fn package

[maintain]
Warnings:

A Haskell web framework where web handlers are just plain functions where parameters are typed arguments.

#!/usr/bin/env stack
-- stack --resolver lts-3.10 --install-ghc runghc --package fn
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid ((<>))
import Data.Text (Text)
import Network.Wai (Request, defaultRequest, Response)
import Network.Wai.Handler.Warp (run)
import Web.Fn

data Ctxt = Ctxt { _req :: Request }
instance RequestContext Ctxt where
  getRequest = _req
  setRequest c r = c { _req = r }

initializer :: IO Ctxt
initializer = return (Ctxt defaultRequest)

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.

The name comes from the fact that Fn emphasizes functions, and has no Fn monad (necessary context, as well as parameters, are passed as arguments, and the return value, which is plain-old IO, specifies whether routing should continue on).


[Skip to ReadMe]

Properties

Versions0.0.0.0, 0.0.0.1, 0.1.0.0, 0.1.1.0, 0.1.2.0, 0.1.3.0, 0.1.3.1, 0.1.4.0, 0.2.0.0, 0.2.0.1, 0.2.0.2, 0.3.0.0, 0.3.0.1
Dependenciesbase (>=4.7 && <5), blaze-builder, bytestring, http-types, text, wai (>=3), wai-extra (>=3) [details]
LicenseISC
Copyright2015 Daniel Patterson
AuthorDaniel Patterson <dbp@dbpmail.net>
Maintainerdbp@dbpmail.net
CategoryWeb
Home pagehttp://github.com/dbp/fn#readme
Source repositoryhead: git clone https://github.com/dbp/fn
UploadedThu Nov 5 21:24:20 UTC 2015 by DanielPatterson

Modules

[Index]

Downloads

Maintainers' corner

For package maintainers and hackage trustees

Readme for fn-0.0.0.1

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, but a minimal application is the following:


{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell   #-}

import           Control.Lens
import           Data.Monoid
import           Data.Text                (Text)
import qualified Data.Text                as T
import           Network.HTTP.Types
import           Network.Wai
import           Network.Wai.Handler.Warp
import qualified Network.Wai.Util         as W
import           Web.Fn

data Ctxt = Ctxt { _req :: Request
                 }

makeLenses ''Ctxt

instance RequestContext Ctxt where
  requestLens = req

initializer :: IO Ctxt
initializer = return (Ctxt defaultRequest)

main :: IO ()
main = do context <- initializer
          run 8000 $ toWAI context app

app :: Ctxt -> IO Response
app ctxt =
  route ctxt [ end ==> index
             , path "foo" // segment // path "baz" /? param "id" ==> handler]
    `fallthrough` notFoundText "Page not found."

index :: IO (Maybe Response)
index = okText "This is the index page! Try /foo/bar/baz?id=10"

handler :: Text -> Int -> Ctxt -> IO (Maybe Response)
handler fragment i _ = okText (fragment <> " - " <> T.pack (show i))