fn: A functional web framework.
This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.
Warnings:
- Exposed modules use unallocated top-level names: Web
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.
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)
, whereNothing
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, 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
Versions | 0.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, 0.3.0.2 |
---|---|
Change log | CHANGELOG.md |
Dependencies | base (>=4.7 && <5), blaze-builder, bytestring, http-types, text, wai (>=3), wai-extra (>=3) [details] |
License | ISC |
Copyright | 2015 Daniel Patterson |
Author | Daniel Patterson <dbp@dbpmail.net> |
Maintainer | dbp@dbpmail.net |
Category | Web |
Home page | http://github.com/dbp/fn#readme |
Source repo | head: git clone https://github.com/dbp/fn |
Uploaded | by DanielPatterson at 2015-11-05T21:24:20Z |
Modules
[Index]
- Web
Downloads
- fn-0.0.0.1.tar.gz [browse] (Cabal source package)
- Package description (as included in the package)
Maintainer's Corner
Package maintainers
For package maintainers and hackage trustees