[work on basic url handling. make home page less dour about unsatisfactory state of existing happs documentation thomashartman1@gmail.com**20080620191716] addfile ./src/ControllerBasic.hs hunk ./src/ControllerBasic.hs 1 +module ControllerBasic where + +import HAppS.Server +import Misc +import View +import Text.StringTemplate + + +{- +ServerPartTs take a request to a response, approximately like so +Request -> WebT -> Result -> Response + +Request handlers are functions from a request to a WebT, WebT is a wrapper over a Result, +and a Response is extracted from a Result by monadic machinery which you don't need +to understand for now. + +ServerPartT :: (Request -> WebT m a) -> ServerPartT m a +WebT :: m (Result a) -> WebT m a +data Result a + = NoHandle | Ok (Response -> Response) a | Escape Response + +-} + +-- For everything below, the m monad is always IO and a result type is always Response, +-- so nail down types with more precision, which I have found helps with debugging +-- and understanding what's going on. +type TutHandler = ServerPartT IO Response +type TutWebT = WebT IO Response + + +simpleHandlers :: [TutHandler] +simpleHandlers = debugFilter [ + + ServerPartT $ \rq -> do + ru <- (return . rqURL) rq + if ru == "simpleRequestHandler1" + then ( ( return . toResponse ) "matched simpleRequestHandler1" :: TutWebT) + else noHandle + + , exactdir "simpleRequestHandler2" [ msgToSp "simpleRequesthandler2" ] + + , exactdir "exactdirAndmsgToSp/anotherResponse" + [ msgToSp "Another response generated using exactdir and msgToSp"] + + , (exactdir "ioaction" + [ ioMsgToSp (return "This is an IO value.\ + \It could just as easily be the result of a file read operation,\ + \or a database lookup." :: IO String) ] ) + + , (exactdir "ioaction2" + [ ioMsgToSp $ do slurp <- readFile "src/Main.hs" + return $ "Let's try reading the Main.hs file: .....\n" ++ slurp ]) + + , (exactdir "htmlAttemptWrong" + [msgToSp "first try at displaying red formatted html (wrong)"]) + , (exactdir "htmlAttemptRight" + [ ( msgToSp . HtmlString ) "second attempt at displaying red formatted html (right)"]) + + , exactdir "exactdirdemo" [ msgToSp "Your request matched the exactdir request handler" ] + , dir "dirdemo" [ msgToSp "dir match. subpages will work" ] + + + , exactdir "testtgio" [withTemplate "myhomepage" (setAttribute "favoriteAnimal" "giraffe") ] + + + + + -- fileserve handles all possible requests, not that this is necessarily a good thing + -- , msgToSp "Sorry, we can't find that page." + + ] hunk ./static/tutorial.css 3 -body{font: 76% arial,sans-serif} +body{font: 85% arial,sans-serif} hunk ./templates/basic-url-handling.st 5 -
Before explaining the theory of request handling in HAppS, let's look at some simple examples.
+$! Before explaining the theory of request handling in HAppS, !$ Let's look at some simple examples.
hunk ./templates/basic-url-handling.st 10 -Unfortunately, the HAppS documentation is sorely lacking. Without good documentation, even something as trivial as hello world can seem to take a Ph.D. in HAppSology.
+$!Unfortunately, the HAppS documentation is sorely lacking. Without good documentation, even something as trivial as hello world can seem to take a Ph.D. in HAppSology.
hunk ./templates/home.st 10 -haskell, in web applications. +haskell, in web applications. !$ + +I created this tutorial to popularize the use of my favorite language, haskell, in web applications.
hunk ./templates/home.st 20 -No Ph.D. required. ®
+$!No Ph.D. required. ®
!$