[web-devel] [Yesod] ANNOUNCE: wai-routes-0.1

Michael Snoyman michael at snoyman.com
Sat Jan 14 19:47:08 CET 2012


On Fri, Jan 13, 2012 at 7:23 PM, Anupam Jain <ajnsit at gmail.com> wrote:
> Hi,
>
> I couldn't find any library that provides typesafe URLs for plain Wai
> applications so I modified the code in Yesod.Dispatch (yesod-core) and
> created wai-routes. This is my first time writing TH code so I stuck
> to "tweaking" the existing functionality.
>
> http://hackage.haskell.org/package/wai-routes
>
> Currently it allows you to define type safe URLs in a manner similar
> to Yesod. It also provides Middleware to easily insert dispatching
> into your application.
>
> A sample application that provides a JSON API (using aeson) -
>
> ============================================
>    -- A useful type synonym
>    type UserId = Text
>
>    -- Define the JSON instance
>    data User = User { name::Text, uid:: UserId } deriving (Show, Read, Eq)
>    instance ToJSON User where
>      toJSON x = object [ "name" .= (name x), "uid" .= (uid x) ]
>
>    -- Define the handlers
>    getUserR :: UserId -> Application
>    getUserR uid _req =
>      return $ responseLBS statusOK headers json
>      where user = User { name = "Anon Amos", uid = uid }
>            json = encode user
>            headers = [("Content-Type", "application/json")]
>
>    getUsersR :: Application
>    getUsersR _req =
>      return $ responseLBS statusOK headers json
>      where userids = (["anon","john","jane"]::[Text])
>            json = encode userids
>            headers = [("Content-Type", "application/json")]
>
>    -- Generate the routing datatype and the Route instance
>    -- The type generated will be named "UserRoute"
>    mkRoute "User" [parseRoutes|
>      /users UsersR GET
>      /user/#UserId UserR GET
>    |]
>
>    -- Now you can use dispatch function (passing it your route datatype)
>    main :: IO ()
>    main = run 8080 $ dispatch (undefined::UserRoute) $ staticApp
> defaultFileServerSettings
> ============================================
>
>
> Thanks,
> Anupam Jain

Very cool! You might want to look at the code in Yesod 0.10 (on the
Github repo), which has a much more efficient dispatch mechanism. The
yesod-routes package in there is not actually dependent on Yesod in
any way; instead, all of Yesod depends on it. That package itself may
be 95% of what you're looking for in wai-routes.

Michael



More information about the web-devel mailing list