-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathRoutes.hs
69 lines (65 loc) · 2.62 KB
/
Routes.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Routes
(routeResponses) where
import Control.Monad (MonadPlus (mplus), msum)
import Controllers.Course as CoursesController (retrieveCourse, index, courseInfo, depts)
import Controllers.Graph as GraphsController
( graphResponse, index, getGraphJSON, graphImageResponse )
import Controllers.Generate as GenerateController (generateResponse, findAndSavePrereqsResponse)
import Controllers.Timetable as TimetableController
import Database.CourseInsertion (saveGraphJSON)
import Database.CourseQueries (retrievePost)
import Happstack.Server
( serveDirectory,
seeOther,
dir,
noTrailingSlash,
nullDir,
Browsing(DisableBrowsing),
ServerPart,
ServerPartT,
Response,
ToMessage(toResponse) )
import Response
( drawResponse,
aboutResponse,
notFoundResponse,
searchResponse,
postResponse,
loadingResponse)
routeResponses :: String -> ServerPartT IO Response
routeResponses staticDir =
msum (map strictMatchDir strictRoutes ++
[dir "static" $ serveDirectory DisableBrowsing [] staticDir,
nullDir >> seeOther ("graph" :: String) (toResponse ("Redirecting to /graph" :: String)),
notFoundResponse])
strictRoutes :: [(String, ServerPart Response)]
strictRoutes = [
("grid", TimetableController.gridResponse),
("graph", GraphsController.graphResponse),
("graph-generate", GenerateController.findAndSavePrereqsResponse),
("image", graphImageResponse),
("timetable-image", TimetableController.exportTimetableImageResponse),
("timetable-pdf", TimetableController.exportTimetablePDFResponse),
("post", retrievePost),
("post-progress", postResponse),
("draw", drawResponse),
("about", aboutResponse),
("graphs", GraphsController.index),
("timesearch", searchResponse),
("generate", generateResponse),
("get-json-data", getGraphJSON),
("course", CoursesController.retrieveCourse),
("courses", CoursesController.index),
("course-info", CoursesController.courseInfo),
("depts", CoursesController.depts),
("calendar", TimetableController.calendarResponse),
("loading", loadingResponse),
("save-json", saveGraphJSON)
]
strictMatchDir :: (String, ServerPart Response) -> ServerPartT IO Response
strictMatchDir (pathname, response) =
mplus (do noTrailingSlash -- enforce no trailing slash in the URI
dir pathname nullDir -- enforce that no segments occur after pathname
response)
(do dir pathname nullDir -- if a trailing slash exists, redirect
seeOther ("/" ++ pathname) (toResponse ("Redirecting to /" ++ pathname)))