forked from kennygrant/gohackernews
/
routes.go
78 lines (65 loc) · 3.22 KB
/
routes.go
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
70
71
72
73
74
75
76
77
78
package app
import (
"github.com/fragmenta/mux"
"github.com/fragmenta/server/log"
// Resource Actions
"github.com/kennygrant/gohackernews/src/comments/actions"
"github.com/kennygrant/gohackernews/src/lib/session"
"github.com/kennygrant/gohackernews/src/stories/actions"
"github.com/kennygrant/gohackernews/src/users/actions"
)
// SetupRoutes creates a new router and adds the routes for this app to it.
func SetupRoutes() *mux.Mux {
router := mux.New()
mux.SetDefault(router)
// Add the home page route
router.Get("/", storyactions.HandleHome)
// Add a route to handle static files
router.Get("/favicon.ico", fileHandler)
router.Get("/files/{path:.*}", fileHandler)
router.Get("/assets/{path:.*}", fileHandler)
// Resource Routes
// Add story routes
router.Get("/index{format:(.xml)?}", storyactions.HandleIndex)
router.Get("/stories/create", storyactions.HandleCreateShow)
router.Post("/stories/create", storyactions.HandleCreate)
router.Get("/stories/code{format:(.xml)?}", storyactions.HandleListCode)
router.Get("/stories/upvoted{format:(.xml)?}", storyactions.HandleListUpvoted)
router.Get("/stories/{id:[0-9]+}/update", storyactions.HandleUpdateShow)
router.Post("/stories/{id:[0-9]+}/update", storyactions.HandleUpdate)
router.Post("/stories/{id:[0-9]+}/destroy", storyactions.HandleDestroy)
router.Post("/stories/{id:[0-9]+}/upvote", storyactions.HandleUpvote)
router.Post("/stories/{id:[0-9]+}/downvote", storyactions.HandleDownvote)
router.Post("/stories/{id:[0-9]+}/flag", storyactions.HandleFlag)
router.Get("/stories/{id:[0-9]+}", storyactions.HandleShow)
router.Get("/stories{format:(.xml)?}", storyactions.HandleIndex)
router.Get("/sitemap.xml", storyactions.HandleSiteMap)
router.Get("/comments", commentactions.HandleIndex)
router.Get("/comments/create", commentactions.HandleCreateShow)
router.Post("/comments/create", commentactions.HandleCreate)
router.Get("/comments/{id:[0-9]+}/update", commentactions.HandleUpdateShow)
router.Post("/comments/{id:[0-9]+}/update", commentactions.HandleUpdate)
router.Post("/comments/{id:[0-9]+}/destroy", commentactions.HandleDestroy)
router.Post("/comments/{id:[0-9]+}/upvote", commentactions.HandleUpvote)
router.Post("/comments/{id:[0-9]+}/downvote", commentactions.HandleDownvote)
router.Post("/comments/{id:[0-9]+}/flag", commentactions.HandleFlag)
router.Get("/comments/{id:[0-9]+}", commentactions.HandleShow)
router.Get("/users", useractions.HandleIndex)
router.Get("/users/create", useractions.HandleCreateShow)
router.Post("/users/create", useractions.HandleCreate)
router.Get("/users/{id:[0-9]+}/update", useractions.HandleUpdateShow)
router.Post("/users/{id:[0-9]+}/update", useractions.HandleUpdate)
router.Post("/users/{id:[0-9]+}/destroy", useractions.HandleDestroy)
router.Get("/users/{id:[0-9]+}", useractions.HandleShow)
router.Get("/u/{name:.*}", useractions.HandleShowName)
router.Get("/users/login", useractions.HandleLoginShow)
router.Post("/users/login", useractions.HandleLogin)
router.Post("/users/logout", useractions.HandleLogout)
// Set the default file handler
router.FileHandler = fileHandler
router.ErrorHandler = errHandler
// Add middleware
router.AddMiddleware(log.Middleware)
router.AddMiddleware(session.Middleware)
return router
}