-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
156 lines (134 loc) · 4.33 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
156
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"sort"
"time"
"github.com/TrevorSStone/goriot"
"github.com/go-martini/martini"
"github.com/lab-D8/lol-at-pitt/ols"
"github.com/lab-D8/lol-at-pitt/site"
"github.com/lab-D8/oauth2"
"github.com/martini-contrib/render"
"os"
)
// Register is used for derp
type Register struct {
Id string
Name string
Next string
}
func main() {
file, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
log.SetOutput(file)
log.Println("Success?")
m := martini.Classic()
goriot.SetAPIKey(LeagueApiKey)
goriot.SetLongRateLimit(LongLeagueLimit, 10*time.Minute)
goriot.SetSmallRateLimit(ShortLeagueLimit, 10*time.Second)
// Setup middleware to be attached to the controllers on every call.
if Debug {
InitDebugMiddleware(m)
} else {
InitMiddleware(m)
}
teamHandler := func(renderer render.Render) {
teams := ols.GetTeamsDAO().All()
renderer.JSON(200, teams)
}
individualTeamHandler := func(params martini.Params, renderer render.Render) {
team := ols.GetTeamsDAO().Load(params["name"])
renderer.JSON(200, team)
}
m.Get("/admin", func(renderer render.Render) {
renderer.HTML(200, "admin", 1)
})
m.Get("/error", func(urls url.Values, renderer render.Render) {
renderer.HTML(200, "error", urls.Get("status"))
})
m.Get("/players/:name", func(params martini.Params, renderer render.Render) {
normalizedName := params["name"]
fmt.Println(normalizedName)
player := ols.GetPlayersDAO().LoadNormalizedIGN(normalizedName)
fmt.Println(player)
displayTeam := ols.GetTeamsDAO().LoadPlayerDisplay(player.Id)
fmt.Println(displayTeam)
renderer.HTML(200, "team", displayTeam)
})
m.Get("/results", func(renderer render.Render) {
players := ols.GetPlayersDAO().All()
allPlayerDisplay := []ols.PlayerDraftResult{}
for _, player := range players {
team := ols.GetTeamsDAO().LoadPlayer(player.Id)
playerDisplay := ols.PlayerDraftResult{Ign: player.Ign, Team: team.Name, NormalizedIgn: player.NormalizedIgn}
allPlayerDisplay = append(allPlayerDisplay, playerDisplay)
}
renderer.HTML(200, "drafted", allPlayerDisplay)
})
m.Get("/teams", teamHandler)
m.Get("/team/:name", individualTeamHandler)
m.Get("/", func(renderer render.Render) {
renderer.HTML(200, "main", 1)
})
m.Get("/register", LoginRequired, func(urls url.Values, renderer render.Render) {
renderer.HTML(200, "register", Register{Next: urls.Get("next")})
})
m.Get("/oauth2error", func(token oauth2.Tokens, renderer render.Render) {
renderer.JSON(200, token)
})
m.Get("/rankings", func(renderer render.Render) {
players := ols.GetPlayersDAO().All()
sort.Sort(players)
renderer.HTML(200, "rank", players)
})
//m.Get("/draft-socket/:drafter", sockets.JSON(Message{}), func(params martini.Params, receiver <-chan *Message, sender chan<- *Message, done <-chan bool, disconnect chan<- int, errorChannel <-chan error) {
//})
m.Get("/register/complete", LoginRequired, func(urls url.Values, renderer render.Render, token oauth2.Tokens, w http.ResponseWriter, r *http.Request) {
summonerName := urls.Get("summoner")
teamName := urls.Get("team")
if token.Expired() {
http.Redirect(w, r, "/error?status=InvalidFacebook", 302)
return
}
id, err := GetId(token.Access())
if err != nil {
renderer.Status(404)
return
}
normalizedSummonerName := goriot.NormalizeSummonerName(summonerName)[0]
player := ols.GetPlayersDAO().LoadNormalizedIGN(normalizedSummonerName)
if player.Id == 0 {
http.Redirect(w, r, "/error?status=No Player Found", 302)
return
}
user := ols.GetUserDAO().GetUserFB(id)
// User is registered registered
if user.LeagueId != 0 {
http.Redirect(w, r, "/error?status=Already Registered User", 302)
return
}
user = site.User{LeagueId: player.Id, FacebookId: id}
log.Println("User registered:", user)
team := ols.GetTeamsDAO().LoadPlayerByCaptain(player.Id)
newTeam := team
if team.Name != "" && teamName != "" {
newTeam.Name = teamName
ols.GetTeamsDAO().Update(team, newTeam)
}
ols.GetUserDAO().Save(user)
//next := urls.Get("next")
log.Println("[REGISTER]: ", teamName)
renderer.HTML(200, "register_complete", 1)
})
initFunnyRouter(m)
SocketRouter(m)
err = http.ListenAndServe(":6060", m) // Nginx needs to redirect here, so we don't need sudo priv to test.
if err != nil {
log.Println(err)
}
}