For each dir..
, it is a Route
which takes in a Request
and returns a Response
created by the Server
. I am wondering if the Request
and the Response
included in each line of the code. Thanks in advance!
simpleHTTP serverConf $ do
decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096)
msum [ do
nullDir
seeOther "graph" (toResponse "Redirecting to /graph"),
dir "grid" gridResponse,
dir "graph" graphResponse,
dir "image" graphImageResponse,
dir "timetable-image" $ look "courses" >>= \x -> look "session" >>= timetableImageResponse x,
dir "graph-fb" $ seeOther redirectUrlGraphEmail $ toResponse "",
dir "post-fb" $ seeOther redirectUrlGraphPost $ toResponse "",
dir "test" $ look "code" >>= getEmail,
dir "test-post" $ look "code" >>= postToFacebook,
dir "post" postResponse,
dir "draw" drawResponse,
dir "about" $ aboutResponse aboutContents,
dir "privacy" $ privacyResponse privacyContents,
dir "static" $ serveDirectory DisableBrowsing [] staticDir,
dir "course" $ look "name" >>= retrieveCourse,
dir "all-courses" $ liftIO allCourses,
dir "graphs" $ liftIO queryGraphs,
dir "course-info" $ look "dept" >>= courseInfo,
dir "depts" $ liftIO deptList,
dir "timesearch" searchResponse,
dir "calendar" $ lookCookieValue "selected-lectures" >>= calendarResponse,
dir "get-json-data" $ look "graphName" >>= \graphName -> liftIO $ getGraphJSON graphName,
dir "loading" $ look "size" >>= loadingResponse,
dir "save-json" $ look "jsonData" >>= \jsonStr -> look "nameData" >>= \nameStr -> liftIO $ saveGraphJSON jsonStr nameStr,
notFoundResponse
]
Each line is a handler. Think of a handler as a function which takes a request and returns a response.
graphReponse
is a handler which handles the request in a certain way.
dir
modifies the handler so that it doesn't get called unless the request url begins a certain way.
There is nothing special about having the word Response
in the name of the handlers. graphResponse
is just the name of a function - you can use any names for your handler functions.
What you have is a list of handlers, e.g.:
[ handler1,
handler2,
...,
notFoundResponse
]
When a request comes in a check is made to see if the handler1
will handle the request. If not, handler2
is checked and so on. If no handler accepts the request the notFoundHandler
is called which will probably generated a 404 page.
That's why the dir ...
part is important - it prevents a handler from responding unless the url begins a certain way.