-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouteMain.go
83 lines (69 loc) · 1.63 KB
/
routeMain.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
package main
import (
"log"
"net/http"
"sort"
"github.com/Iteam1337/go-protobuf-wejay/message"
"github.com/Iteam1337/go-protobuf-wejay/types"
"github.com/Iteam1337/go-wejay/tmpl"
"github.com/Iteam1337/go-wejay/utils"
"github.com/google/uuid"
)
type routeMain struct{}
func init() {
router.main = routeMain{}
}
func (route *routeMain) Root(w http.ResponseWriter, r *http.Request) {
exists, _, err := exists(r)
if err == nil && exists {
redirect(w, r, routePathEmpty)
return
}
var cb message.CallbackURLResponse
if err := updServer.NewRequest(
types.ICallbackURL,
&message.CallbackURL{UserId: uuid.New().String()},
&cb,
); err != nil {
http.Error(w, "Couldn't get callback-url", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/html")
tmpl.NewAuth(w, cb.Url)
}
func sortBySize(available []*message.RefRoom) utils.PairList {
pl := make(utils.PairList, len(available))
for i, room := range available {
pl[i] = utils.Pair{
ID: room.Id,
Size: int(room.Size),
}
}
sort.Sort(sort.Reverse(pl))
return pl
}
func (route *routeMain) Empty(w http.ResponseWriter, r *http.Request) {
exists, userID, err := exists(r)
if err != nil {
redirect(w, r, routePathBase)
return
}
if !exists {
redirect(w, r, routePathBase)
return
}
var cb message.UserRoomResponse
if err := updServer.NewRequest(
types.IUserRoom,
&message.UserRoom{UserId: userID},
&cb,
); err != nil {
log.Println(err)
}
if cb.Ok && cb.RoomId != "" {
redirect(w, r, "/room/"+cb.RoomId)
}
rooms, _ := router.room.Available("")
w.Header().Set("Content-Type", "text/html")
tmpl.Empty(w, sortBySize(rooms))
}