Search code examples
haskellhappstack

What should a Route look like in Happstack code?


runServer :: IO ()
runServer = do
    configureLogger
    staticDir <- getStaticDir
    redirectUrlGraphEmail <- retrieveAuthURL testUrl
    redirectUrlGraphPost <- retrieveAuthURL testPostUrl
    aboutContents <- LazyIO.readFile $ markdownPath ++ "README.md"
    privacyContents <- LazyIO.readFile $ markdownPath ++ "PRIVACY.md"
-- Start the HTTP server
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
    ]

In the list after the function msum( I assume it's a list since it's in []), is everything after dir a route? What should a route look like in happstack code? I am totally lost in this piece of code.


Solution

  • My understanding is that each dir ... line is a route (for a directory.)

    E.g.:

     dir "static" $ serveDirectory DisableBrowsing [] staticDir,
    

    means that for paths like /static/... invoke the handler serveDirectory ...

    If you want to add your own route, add it before the notFoundResponse - otherwise it probably will never get called.