ANN: Wai routes 0.7

15 views
Skip to first unread message

Anupam Jain

unread,
Jun 9, 2015, 10:16:00 PM6/9/15
to web-...@haskell.org, haske...@googlegroups.com, yeso...@googlegroups.com
Hi all,

I have just released v0.7 of wai-routes, the typesafe urls framework
for wai. After letting the code rot for several months, I finally
managed to carve out some time to do a bit of housekeeping. The code
now compiles with ghc 7.8 and 7.10, and is compatible with wai-3.0.
The dependency on yesod-routes has also been removed, as that package
was deprecated, and its code has been folded into wai-routes.

I also took the opportunity to add a few long pending features -
1. Subsites! Simpler than the corresponding concept in Yesod, though
probably less powerful as well. This is still an evolving experimental
feature.
2. Ability to get information about the currently executing route,
including the Route Attributes (which is exactly the same thing as in
Yesod).

Get it at -
[http://hackage.haskell.org/package/wai-routes]
[https://github.com/ajnsit/wai-routes].

Feedback would be greatly appreciated, especially from people using
wai-routes in their projects!

-- Anupam

Anupam Jain

unread,
Jun 10, 2015, 3:19:06 PM6/10/15
to yeso...@googlegroups.com, web-...@haskell.org, haske...@googlegroups.com
You are right. The example in the README is meant to be more of a
template which gives you a flavor of what the code looks like. It also
has chunks of code missing (and with ellipses in its place) that would
need to be filled in before it would compile. The code in the
examples/ directory on github has working code examples with cabal
files (https://github.com/ajnsit/wai-routes/blob/master/examples/Subsites.hs).
I am working on improving the documentation, and adding more examples.

To answer your other questions -

1. I just released v0.7 today. Docs on Hackage are built periodically,
and we should have proper documentation generated soon.

2. The TH code generates a "resource" for your route which is like an
abstract representation of your route declaration. It can be reused in
other parts of the code. Notably it is useful when you want to
separate the declaration for the Route datatype and the dispatching
code. In your example, since the resource is created but not used or
exported, it generates a warning. For now you can safely ignore it.

3. "HandlerS sub master" is the type of a handler for a Subsite "sub"
running within a master site "master". Unfortunately that type is not
very easy to work with. "HandlerM" is a Monadic type that provides
easy access to all wai-routes functionality, and can then be converted
to a HandlerS using runHandlerM. When you don't have a subsite, the
type is simply "HandlerM sub master", which is aliased to "Handler
master". That's why in your example, the handlers all have the type
"Handler MyRoute".

4. "RouteM" is another Monadic type which makes it easy to compose
multiple routes and middleware together to create a final wai
application. This along with "nested routes", and "subsites", helps
you better organise your application.

HTH

-- Anupam



On Wed, Jun 10, 2015 at 8:38 PM, Njagi Mwaniki
<mwaniki...@gmail.com> wrote:
> It could be just me but the example in your README is totally broken. It
> needs at least the extensions like QuasiQotes and TemplateHaskell. Also
> where does DB (used in your example) come from?
>
> Anyway using other examples from your README I got a minimal working example
> here: https://gist.github.com/urbanslug/58a584c002f54ad75c02
>
> I have a few questions regarding the types.
>
> * The haddocks on hackage don't work yet, do they?
> * in myRoute L21 I get the warning `Defined but not used:
> ‘resourcesMyRoute’` can you help with that?
> * I'm confused about this type signature "runHandlerM :: HandlerM sub master
> () -> HandlerS sub master"
> * What are HandlerM and RouteM and sub and master? They are in many type
> signatures and I don't get them.
>
> Thank you.
> --
> You received this message because you are subscribed to the Google Groups
> "Yesod Web Framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to yesodweb+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Anupam Jain

unread,
Jun 12, 2015, 12:10:38 PM6/12/15
to yeso...@googlegroups.com, web-...@haskell.org, haske...@googlegroups.com
Glad you figured it out! The html function accepts Bytestring instead
of Text to be compatible with blaze-html. I will probably add a
htmlText function which supports Text instead and works seamlessly
with Hamlet et all.

One minor thing I notices in your code - it converts from Lazy Text ->
Strict Text -> Strict ByteString -> Lazy ByteString
If you use encodeUtf8 from Data.Text.Lazy.Encoding, you can go
directly from Lazy ByteString -> Lazy Text.

-- Anupam


On Fri, Jun 12, 2015 at 5:16 PM, Njagi Mwaniki
<mwaniki...@gmail.com> wrote:
> Never mind. I found a way to make it work here:
> https://github.com/urbanslug/cats/blob/master/Handler/Home.hs
>
>
> On Friday, 12 June 2015 12:08:44 UTC+3, Njagi Mwaniki wrote:
>>
>> Hey, I am trying to use shakesperean templates with this. I just can't get
>> it to work. Even in a quasiqote widget kind of way. What am I doing wrong?
>> or is this not supported at all. I would like to finally use external files
>> for hamlet lucius and julius. I could show you my code but it's just a ball
>> of wool so I won't even bother.

Greg Weber

unread,
Jun 12, 2015, 12:27:04 PM6/12/15
to Anupam Jain, Yesod Web Framework, web-devel, haske...@googlegroups.com
On Fri, Jun 12, 2015 at 9:10 AM, Anupam Jain <ajn...@gmail.com> wrote:
Glad you figured it out! The html function accepts Bytestring instead
of Text to be compatible with blaze-html. I will probably add a
htmlText function which supports Text instead and works seamlessly
with Hamlet et all.

Hamlet itself uses blaze-html so I think you should be able to avoid encodings altogether 
 
You received this message because you are subscribed to the Google Groups "haskell-wai" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskell-wai...@googlegroups.com.
To post to this group, send email to haske...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/haskell-wai/CAKit5aCsbqp_gmhxFzSnKDz8d0eO4tEFRiMgmMcQ0miBusHL8w%40mail.gmail.com.

Greg Weber

unread,
Jun 12, 2015, 12:29:18 PM6/12/15
to Yesod Web Framework, web-devel, haske...@googlegroups.com
To me, wai-routes seems like a full web framework and a re-implementation of Yesod (I am not suggesting that it is a bad thing). Is that its goal?


-- Anupam

Anupam Jain

unread,
Jun 12, 2015, 2:09:53 PM6/12/15
to yeso...@googlegroups.com, web-devel, haske...@googlegroups.com
Ah great question! One that I keep revisiting in my mind every time I
think of adding something to wai-routes. Short answer - No. Long
answer follows.

Wai-routes' stated goal is to "provide a similar level of typesafe URL
functionality to Wai applications as is available to Yesod
applications". The feature list may make it seem like a larger
framework, but it's really all just features provided by the routing
code from yesod! Writing plain wai apps is tough though, so wai-routes
provides a thin veneer over some of the rough edges, just enough to
make it pleasant.

Sometimes the simplicity of wai-routes makes it easier to use than
Yesod. For example, it is really easy to have multiple independent
route sets that match one after the other, so you can better logically
group your app, instead of putting all routes in one big file. You can
also apply different middleware to the different route sets. I'm
currently not aware of any way to do that in Yesod.

The de-coupling from wai also insulates the user from changes to the
wai api. So the same wai-routes code continued to work when Wai-3.0
changed to a CPS style. ...Though that leaves the user vulnerable to
changes in the wai-routes api... which happens quite more often... So,
forget I said that! :)

-- Anupam

Greg Weber

unread,
Jun 13, 2015, 2:54:20 PM6/13/15
to Anupam Jain, Yesod Web Framework, web-devel, haske...@googlegroups.com
Thanks for the explanation.

I have a small function for applying different middleware to different routes that I put up on a pull request now.

You received this message because you are subscribed to the Google Groups "haskell-wai" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskell-wai...@googlegroups.com.
To post to this group, send email to haske...@googlegroups.com.

Anupam Jain

unread,
Jun 28, 2015, 11:01:12 PM6/28/15
to yeso...@googlegroups.com, haske...@googlegroups.com, web-devel
On Sun, Jun 28, 2015 at 10:18 AM, Njagi Mwaniki
<mwaniki...@gmail.com> wrote:
> Another question for you. How can I or recreate the results of functions
> like widgetFile in yesod? I want to use julius, lucius and hamlet in my
> wai-routes app.

You don't need a widgetFile function to use Julius and Lucius in
wai-routes. Take a look at this small example -
https://github.com/ajnsit/wai-routes/blob/master/examples/Hamlet.hs -
on how to render Cassius from an external file. The process for
including Julius or Lucius should be fairly similar to Cassius.

Note that Wai-routes 0.7.1 includes a function called `showRouteQuery`
(http://hackage.haskell.org/package/wai-routes-0.7.1/docs/Network-Wai-Middleware-Routes-Routes.html#v:showRouteQuery)
which is a route renderer in the format that Shakespearean templates
expect.

-- Anupam
Reply all
Reply to author
Forward
0 new messages