Skip to content
This repository has been archived by the owner on Nov 12, 2017. It is now read-only.

Commit

Permalink
nested routing in post
Browse files Browse the repository at this point in the history
  • Loading branch information
scvalex committed Nov 27, 2011
1 parent 11b5696 commit 5dfca5d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
12 changes: 11 additions & 1 deletion Handler/Post.hs
Expand Up @@ -7,9 +7,11 @@ module Handler.Post (
) where

import Data.Text ( Text )
import Handler.NotFound ( notFound )
import Logger ( noticeM )
import Network.HTTP.Types ( statusOK )
import Network.Wai ( Application, Request(..), Response(..) )
import Network.Wai.Middleware.Route ( dispatch, (&~~) )
import Text.Blaze ( Html, ToHtml(..) )
import Text.Blaze.Html5 ( (!) )
import qualified Text.Blaze.Html5 as H
Expand All @@ -22,7 +24,15 @@ post :: Application
post req = do
let (Just req') = stripPrefixReq "/post" req
noticeM $ "Handling post " ^-^ (rawPathInfo req')
return (ResponseBuilder statusOK [] $ renderHtmlBuilder postPage)
dispatch [ ("*" &~~ "^/$", noPost)
, ("*" &~~ "^/[0-9]+", particularPost)
] notFound req'

noPost :: Application
noPost _ = return (ResponseBuilder statusOK [] $ renderHtmlBuilder postPage)

particularPost :: Application
particularPost = undefined

postPage :: Html
postPage = do
Expand Down
4 changes: 1 addition & 3 deletions Handler/Resource.hs
@@ -1,9 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}

-- | The 'resource' handler just serves static files from the @.\/r\/@
-- directory. It expects the 'pathInfo' part of the 'Request' to be
-- the path to the file under @.\/r\/@. I.e. any other path prefix
-- should have been stripped already.
-- directory.
module Handler.Resource (
resource
) where
Expand Down
4 changes: 2 additions & 2 deletions Main.hs
Expand Up @@ -8,7 +8,7 @@ import Handler.NotFound ( notFound )
import Handler.Post ( post )
import Handler.Resource ( resource )
import Logger ( setupLogger, noticeM )
import Network.Wai ( Application, Request(..) )
import Network.Wai ( Application )
import Network.Wai.Handler.Warp ( run )
import Network.Wai.Middleware.Route ( dispatch, (&~~) )
import Types ( Conf(..) )
Expand All @@ -26,7 +26,7 @@ main = do

router :: Conf -> Application
router conf =
dispatch [ ((=="/") . rawPathInfo, index conf)
dispatch [ ("*" &~~ "^/$", index conf)
, ("*" &~~ "^/post", post)
, ("GET" &~~ "^/r/", resource)
] notFound
6 changes: 4 additions & 2 deletions Types.hs
Expand Up @@ -9,6 +9,7 @@ module Types (
import Database ( Database )
import Data.String ( IsString(..) )
import Data.Text ( stripPrefix, splitOn )
import qualified Data.Text as T
import Data.Text.Encoding ( decodeUtf8, encodeUtf8 )
import Network.Wai ( Request(..) )

Expand All @@ -17,5 +18,6 @@ data Conf = Conf { getDatabase :: Database }
stripPrefixReq :: String -> Request -> Maybe Request
stripPrefixReq pre req = do
path <- stripPrefix (fromString pre) (decodeUtf8 $ rawPathInfo req)
return $ req { rawPathInfo = encodeUtf8 path
, pathInfo = splitOn "/" path }
let path' = if T.null path then "/" else path
return $ req { rawPathInfo = encodeUtf8 path'
, pathInfo = splitOn "/" path' }

0 comments on commit 5dfca5d

Please sign in to comment.