-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.go
139 lines (110 loc) · 5.08 KB
/
server.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
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/UniversityRadioYork/2016-site/controllers"
"github.com/UniversityRadioYork/2016-site/structs"
"github.com/UniversityRadioYork/myradio-go"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
)
// Server is the type of the main 2016site web application.
type Server struct {
*negroni.Negroni
}
// NewServer creates a 2016site server based on the config c.
func NewServer(c *structs.Config) (*Server, error) {
s := Server{negroni.Classic()}
var session *myradio.Session
var err error
if c.Server.MyRadioAPI == "" {
session, err = myradio.NewSessionFromKeyFile()
} else {
session, err = myradio.NewSessionFromKeyFileForServer(c.Server.MyRadioAPI)
}
if err != nil {
return &s, err
}
router := mux.NewRouter().StrictSlash(true)
getRouter := router.Methods("GET").Subrouter()
postRouter := router.Methods("POST").Subrouter()
headRouter := router.Methods("HEAD").Subrouter()
// Routes go in here
nfc := controllers.NewNotFoundController(session, c)
router.NotFoundHandler = http.HandlerFunc(nfc.Get)
ic := controllers.NewIndexController(session, c)
getRouter.HandleFunc("/", ic.Get)
postRouter.HandleFunc("/", ic.Post)
sc := controllers.NewSearchController(session, c)
getRouter.HandleFunc("/search/", sc.Get)
showC := controllers.NewShowController(session, c)
getRouter.HandleFunc("/schedule/shows/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/schedule/thisweek/", 301)
})
getRouter.HandleFunc("/schedule/shows/{id:[0-9]+}/", showC.GetShow).Name("show")
getRouter.HandleFunc("/schedule/shows/timeslots/{id:[0-9]+}/", showC.GetTimeslot).Name("timeslot")
getRouter.HandleFunc("/schedule/shows/seasons/{id:[0-9]+}/", showC.GetSeason).Name("season")
getRouter.HandleFunc("/schedule/shows/{id:[0-9]+}/podcast_rss", showC.GetPodcastRss).Name("podcast_rss")
headRouter.HandleFunc("/schedule/shows/{id:[0-9]+}/podcast_rss", showC.GetPodcastRssHead).Name("podcast_rss_head")
getRouter.HandleFunc("/uyco/", showC.GetUyco).Name("uyco")
getRouter.HandleFunc("/schedule/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/schedule/thisweek/", 301)
})
// NOTE: NewScheduleWeekController assumes 'timeslot' is installed BEFORE it is called.
schedWeekC := controllers.NewScheduleWeekController(session, getRouter, c)
getRouter.HandleFunc("/schedule/thisweek/", schedWeekC.GetThisWeek).Name("schedule-thisweek")
getRouter.HandleFunc("/schedule/{year:[1-9][0-9][0-9][0-9]}/w{week:[0-5]?[0-9]}/", schedWeekC.GetByYearWeek).Name("schedule-week")
// This route exists so that day schedule links from the previous website aren't broken.
getRouter.HandleFunc("/schedule/{year:[1-9][0-9][0-9][0-9]}/w{week:[0-5]?[0-9]}/{day:[1-7]}/", schedWeekC.GetByYearWeek).Name("schedule-week-day-compat")
onDemandC := controllers.NewOnDemandController(session, c)
getRouter.HandleFunc("/ontap/", onDemandC.Get)
podcastsC := controllers.NewPodcastController(session, c)
getRouter.HandleFunc("/podcasts/", podcastsC.GetAllPodcasts)
getRouter.HandleFunc("/podcasts/page/{page:[0-9]+}", podcastsC.GetAllPodcasts)
getRouter.HandleFunc("/podcasts/{id:[0-9]+}/", podcastsC.Get)
getRouter.HandleFunc("/podcasts/{id:[0-9]+}/player/", podcastsC.GetEmbed)
// Redirect old podcast URLs
getRouter.HandleFunc("/uryplayer/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/ontap/", 301)
})
getRouter.HandleFunc("/listen/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/about/", 301)
})
getRouter.HandleFunc("/uryplayer/podcasts/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/podcasts/", 301)
})
getRouter.HandleFunc("/uryplayer/podcasts/{id:[0-9]+}/", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
http.Redirect(w, r, fmt.Sprintf("/podcasts/%d/", id), 301)
})
getRouter.HandleFunc("/uryplayer/podcasts/{id:[0-9]+}/player/", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
http.Redirect(w, r, fmt.Sprintf("/podcasts/%d/player/", id), 301)
})
pc := controllers.NewPeopleController(session, c)
getRouter.HandleFunc("/people/{id:[0-9]+}/", pc.Get)
teamC := controllers.NewTeamController(session, c)
getRouter.HandleFunc("/teams/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/about/", http.StatusFound)
})
getRouter.HandleFunc("/teams/{alias}/", teamC.Get)
getinvolvedC := controllers.NewGetInvolvedController(session, c)
getRouter.HandleFunc("/getinvolved/", getinvolvedC.Get)
signupC := controllers.NewSignUpController(session, c)
postRouter.HandleFunc("/signup/", signupC.Post)
aboutC := controllers.NewAboutController(session, c)
getRouter.HandleFunc("/about/", aboutC.Get)
staticC := controllers.NewStaticController(c)
getRouter.HandleFunc("/contact/", staticC.GetContact)
getRouter.HandleFunc("/competitions/", staticC.GetCompetitions)
// Candidate Interview Night Routes
if c.PageContext.CIN {
getRouter.HandleFunc("/cin/", staticC.GetCIN)
}
// End routes
s.UseHandler(router)
return &s, nil
}