GET routes in Fn
At Position we use Fn, a Haskell web framework built by our coworker dbp. We think it is pretty neat!
One of the neatest things about it is how routes and handlers work.
For example, say you had an exciting feature-filled website that can both ADD NUMBERS and BLEND COLORS!
You could write a webapp function like this:
site :: Context -> IO Response site ctxt = route ctxt [ end ==> indexHandler , path "add" // segment // segment ==> addH , path "blend" // segment // segment // segment // segment ==> blendH ] `fallthrough` notFoundText "Page not found."
Focusing first on this route:
path "add" // segment // segment ==> addH
The two segments mean that it matches two path segments after 'add' (e.g. niceapp.com/add/10/20 ) and parses those segments into function parameters of the type specified in the handler:
addH :: Ctxt -> Int -> Int -> IO (Maybe Response) addH _ x y = okText $ pack . show $ x + y
or:
blendH :: Ctxt -> Text -> Text -> IO (Maybe Response) blendH _ "blue" "red" = okText "Purple!" blendH _ _ _ = errorText "I don't know how to blend those. -_-"
Or if you make a Color type that’s an instance of Fn’s `FromParam` type class, you could write something like:
blendH :: Ctxt -> Color -> Color -> IO (Maybe Response) blendH _ Blue Red = okText "Purple!" blendH _ _ _ = errorText "I don't know how to blend those. -_-"












