forked from drbawb/babou
/
router.go
155 lines (130 loc) · 3.87 KB
/
router.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package app
import (
admin "github.com/drbawb/babou/app/admin"
controllers "github.com/drbawb/babou/app/controllers"
filters "github.com/drbawb/babou/app/filters"
web "github.com/drbawb/babou/lib/web"
mux "github.com/gorilla/mux"
log "log"
http "net/http"
)
func LoadRoutes(s *Server) *mux.Router {
r := mux.NewRouter()
web.Router = r
// Shorthand for controllers
home := controllers.NewHomeController()
login := controllers.NewLoginController()
torrent := controllers.NewTorrentController()
eventChain := filters.EventChain(s.AppBridge)
// Handle admin routes
adminPanel := r.PathPrefix("/admin").Subrouter()
adminPanel, err := admin.LoadRoutes(adminPanel)
if err != nil {
log.Fatalf("Error loading sub-application: /admin, because: %s \n", err.Error())
}
// Shows public homepage, redirects to private site if valid session can be found.
r.HandleFunc("/",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(home, "index")).
Name("homeIndex")
r.HandleFunc("/faq",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(home, "faq")).
Name("aboutUs")
// Displays a login form.
r.HandleFunc("/login",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(login, "index")).
Methods("GET").
Name("loginIndex")
r.HandleFunc("/login",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(login, "session")).
Methods("POST").
Name("loginSession")
r.HandleFunc("/logout",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(login, "logout")).
Methods("GET").
Name("loginDelete")
// Displays a registration form
r.HandleFunc("/register",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(login, "new")).
Methods("GET").
Name("loginNew")
// Handles a new user's registration request.
r.HandleFunc("/register",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(login, "create")).
Methods("POST").
Name("loginCreate")
// Handle torrent routes:
r.HandleFunc("/torrents",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Chain(eventChain).
Resolve(torrent, "index")).
Methods("GET").
Name("torrentIndex")
// (Redirects to /torrents/tv/episodes)
r.HandleFunc("/torrents/tv",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Chain(eventChain).
Resolve(torrent, "latestEpisodes")).
Methods("GET").
Name("tvLatest")
// Display recently modified episodes of television.
r.HandleFunc("/torrents/tv/episodes",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Chain(eventChain).
Resolve(torrent, "latestEpisodes")).
Methods("GET").
Name("tvEpisodes")
// Display recently modified series of television
r.HandleFunc("/torrents/tv/series",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Chain(eventChain).
Resolve(torrent, "latestSeries")).
Methods("GET").
Name("tvSeries")
r.HandleFunc("/torrents/new",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(torrent, "new")).
Methods("GET").
Name("torrentNew")
r.HandleFunc("/torrents/create",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(torrent, "create")).
Methods("POST").
Name("torrentCreate")
r.HandleFunc("/torrents/download/{torrentId}",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Resolve(torrent, "download")).
Methods("GET").
Name("torrentDownload")
r.HandleFunc("/torrents/delete/{torrentId}",
filters.BuildDefaultChain().
Chain(filters.AuthChain(false)).
Chain(eventChain).
Resolve(torrent, "delete")).
Methods("GET").
Name("torrentDelete")
// Catch-All: Displays all public assets.
r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/",
web.DisableDirectoryListing(http.FileServer(http.Dir("assets/")))))
return r
}