Skip to content

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Aug 9, 2012
1 parent 48efb13 commit fd05a4d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ dist
*.o *.o
*.hi *.hi
report.html report.html
example/Main
example/Routes.hs
29 changes: 29 additions & 0 deletions example/Application.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@
{-# LANGUAGE OverloadedStrings #-} -- Whatever, it's just an example

module Application where

import Network.Wai
import Network.HTTP.Types (ok200, notFound404)

import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.ByteString.Lazy as LZ

import Data.Monoid (mappend)

textToUTF8 txt = LZ.fromChunks [T.encodeUtf8 txt]

showUTF8 :: (Show a) => a -> LZ.ByteString
showUTF8 = textToUTF8 . T.pack . show

home _ = return $ responseLBS ok200 [("Content-Type", "text/plain")] "Hello World"

test val _ = return $ responseLBS ok200 [("Content-Type", "text/plain")] (textToUTF8 val)

on404 _ = return $ responseLBS notFound404 [("Content-Type", "text/plain")] "Not Found"

test2 :: Integer -> Application
test2 val _ = return $ responseLBS ok200 [("Content-Type", "text/plain")] (showUTF8 val)

test3 val env = return $ responseLBS ok200 [("Content-Type", "text/plain")] (textToUTF8 val `mappend` "\n\n" `mappend` showUTF8 (pathInfo env))
13 changes: 13 additions & 0 deletions example/Main.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,13 @@
module Main where

import Network.Wai
import Network.Wai.Handler.Warp (run)
import Network.Wai.Middleware.RequestLogger (logStdoutDev)

import Network.Wai.Dispatch
import Application
import Routes

main = do
putStrLn "Running..."
run 3000 (logStdoutDev $ dispatch on404 routes)
5 changes: 5 additions & 0 deletions example/Makefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
Main: Main.hs Application.hs Routes.hs
ghc $^

Routes.hs: routes
../dist/build/routeGenerator/routeGenerator $^ Application > $@
4 changes: 4 additions & 0 deletions example/routes
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
GET / => home
GET /test/: => test
GET /test2/: => test2
GET /test3/:/* => test3

0 comments on commit fd05a4d

Please sign in to comment.