0

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
]
Mia.M
  • 97
  • 1
  • 1
  • 8
  • 1
    I'm not sure I understand what you are asking. Perhaps this tutorial will help: https://www.schoolofhaskell.com/user/stepcut/the-happstack-crashcourse – ErikR Jun 15 '16 at 02:14
  • I mean in each route, there supposed to be a request and a response. Is that correct? – Mia.M Jun 15 '16 at 19:55

1 Answers1

0

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.

ErikR
  • 51,541
  • 9
  • 73
  • 124
  • I see. Thank you so much! I know there is nothing special about having the word 'Response' in the name of the handlers. However, I am wondering if we can use the name of the handler to stand for the response that returned by itself. – Mia.M Jun 15 '16 at 20:52
  • Just name your handlers however you want - the name of the handler doesn't matter. How about an up vote? – ErikR Jun 15 '16 at 21:06
  • Thanks! I already did, but my reputation is still under 15. So the up vote I gave will remain unseen. – Mia.M Jun 18 '16 at 03:37