/
middleware.go
162 lines (131 loc) · 3.15 KB
/
middleware.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
157
158
159
160
161
162
package main
import (
"errors"
"fmt"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"log"
"net/http"
)
var ErrUserNotPresent = errors.New("User not present in context")
var ErrFriendsNotPresent = errors.New("Friends not present in context")
var ErrSessionNotPresent = errors.New("No session found")
type contextKey int
// Define keys to retrieve values from context
const userKey contextKey = 0
const friendKey contextKey = 1
const sessionKey contextKey = 2
func GetFrends(r *http.Request) ([]User, error) {
val, ok := context.GetOk(r, friendKey)
if !ok {
return nil, ErrFriendsNotPresent
}
friends, ok := val.([]User)
if !ok {
return nil, ErrFriendsNotPresent
}
return friends, nil
}
func SetFreinds(r *http.Request, val []User) {
context.Set(r, friendKey, val)
}
func (g *Global) loadFriends(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
friends := make([]User, 0, 10)
user, err := GetUser(r)
if err != nil {
log.Fatal(err)
}
if user.Id != -1 {
rows, err := g.db.Query("SELECT user_id_2, Username FROM "+
"Friends INNER JOIN Users "+
"ON Friends.user_id_2=Users.id "+
"WHERE user_id_1 = ?", user.Id)
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var id int
var userName string
err = rows.Scan(&id, &userName)
if err != nil {
log.Fatal(err)
}
friends = append(friends,
User{
UserName: userName,
Id: id,
PhoneNumber: fmt.Sprintf("x%d", id),
Twitter: fmt.Sprintf("%d", id),
})
}
rows.Close()
}
SetFreinds(r, friends)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func GetUser(r *http.Request) (User, error) {
val, ok := context.GetOk(r, userKey)
if !ok {
return User{}, ErrUserNotPresent
}
user, ok := val.(User)
if !ok {
return User{}, ErrUserNotPresent
}
return user, nil
}
func SetUser(r *http.Request, val User) {
context.Set(r, userKey, val)
}
func (g *Global) loadUser(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
session, err := GetSession(r)
if err != nil {
log.Fatal(err)
}
user := User{}
if session.IsNew {
user.Id = -1
} else {
UserName := session.Values["UserName"].(string)
Id := session.Values["Id"].(int)
user = User{
UserName: UserName,
Id: Id,
PhoneNumber: fmt.Sprintf("x%d", Id),
Twitter: fmt.Sprintf("%d", Id),
}
}
context.Set(r, userKey, user)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func GetSession(r *http.Request) (*sessions.Session, error) {
val, ok := context.GetOk(r, sessionKey)
if !ok {
return nil, ErrUserNotPresent
}
session, ok := val.(*sessions.Session)
if !ok {
return nil, ErrUserNotPresent
}
return session, nil
}
func SetSession(r *http.Request, val *sessions.Session) {
context.Set(r, sessionKey, val)
}
func (g *Global) loadSession(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
session, err := g.store.Get(r, "tanks-app")
if err != nil {
log.Fatal(err)
}
SetSession(r, session)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}