forked from cyverse-de/user-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
212 lines (178 loc) · 5.51 KB
/
sessions.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
// UserSessionsApp is an implementation of the App interface created to manage
// user sessions.
type UserSessionsApp struct {
sessions sDB
router *mux.Router
}
// NewSessionsApp returns a new *UserSessionsApp
func NewSessionsApp(db sDB, router *mux.Router) *UserSessionsApp {
sessionsApp := &UserSessionsApp{
sessions: db,
router: router,
}
sessionsApp.router.HandleFunc("/sessions/", sessionsApp.Greeting).Methods("GET")
sessionsApp.router.HandleFunc("/sessions/{username}", sessionsApp.GetRequest).Methods("GET")
sessionsApp.router.HandleFunc("/sessions/{username}", sessionsApp.PutRequest).Methods("PUT")
sessionsApp.router.HandleFunc("/sessions/{username}", sessionsApp.PostRequest).Methods("POST")
sessionsApp.router.HandleFunc("/sessions/{username}", sessionsApp.DeleteRequest).Methods("DELETE")
return sessionsApp
}
// Greeting prints out a greeting to the writer from user-sessions.
func (u *UserSessionsApp) Greeting(writer http.ResponseWriter, r *http.Request) {
fmt.Fprintf(writer, "Hello from user-sessions.\n")
}
func (u *UserSessionsApp) getUserSessionForRequest(username string, wrap bool) ([]byte, error) {
sessions, err := u.sessions.getSessions(username)
if err != nil {
return nil, fmt.Errorf("Error getting sessions for username %s: %s", username, err)
}
var retval UserSessionRecord
if len(sessions) >= 1 {
retval = sessions[0]
}
response, err := convertSessions(&retval, wrap)
if err != nil {
return nil, fmt.Errorf("Error generating response for username %s: %s", username, err)
}
var jsoned []byte
if len(response) > 0 {
jsoned, err = json.Marshal(response)
if err != nil {
return nil, fmt.Errorf("Error generating session JSON for user %s: %s", username, err)
}
} else {
jsoned = []byte("{}")
}
return jsoned, nil
}
// GetRequest handles writing out a user's session as a response.
func (u *UserSessionsApp) GetRequest(writer http.ResponseWriter, r *http.Request) {
var (
username string
userExists bool
err error
ok bool
v = mux.Vars(r)
)
if username, ok = v["username"]; !ok {
badRequest(writer, "Missing username in URL")
return
}
log.WithFields(log.Fields{
"service": "sessions",
}).Info("Getting user session for ", username)
if userExists, err = u.sessions.isUser(username); err != nil {
badRequest(writer, fmt.Sprintf("Error checking for username %s: %s", username, err))
return
}
if !userExists {
badRequest(writer, fmt.Sprintf("User %s does not exist", username))
return
}
jsoned, err := u.getUserSessionForRequest(username, false)
if err != nil {
errored(writer, err.Error())
}
writer.Write(jsoned)
}
// PutRequest handles creating a new user session.
func (u *UserSessionsApp) PutRequest(writer http.ResponseWriter, r *http.Request) {
u.PostRequest(writer, r)
}
// PostRequest handles modifying an existing user session.
func (u *UserSessionsApp) PostRequest(writer http.ResponseWriter, r *http.Request) {
var (
username string
userExists bool
hasSession bool
err error
ok bool
v = mux.Vars(r)
)
if username, ok = v["username"]; !ok {
badRequest(writer, "Missing username in URL")
return
}
if userExists, err = u.sessions.isUser(username); err != nil {
badRequest(writer, fmt.Sprintf("Error checking for username %s: %s", username, err))
return
}
if !userExists {
badRequest(writer, fmt.Sprintf("User %s does not exist", username))
return
}
if hasSession, err = u.sessions.hasSessions(username); err != nil {
errored(writer, fmt.Sprintf("Error checking session for user %s: %s", username, err))
return
}
var checked map[string]interface{}
bodyBuffer, err := ioutil.ReadAll(r.Body)
if err != nil {
errored(writer, fmt.Sprintf("Error reading body: %s", err))
return
}
if err = json.Unmarshal(bodyBuffer, &checked); err != nil {
errored(writer, fmt.Sprintf("Error parsing request body: %s", err))
return
}
bodyString := string(bodyBuffer)
if !hasSession {
if err = u.sessions.insertSession(username, bodyString); err != nil {
errored(writer, fmt.Sprintf("Error inserting session for user %s: %s", username, err))
return
}
} else {
if err = u.sessions.updateSession(username, bodyString); err != nil {
errored(writer, fmt.Sprintf("Error updating session for user %s: %s", username, err))
return
}
}
jsoned, err := u.getUserSessionForRequest(username, true)
if err != nil {
errored(writer, err.Error())
return
}
writer.Write(jsoned)
}
// DeleteRequest handles deleting a user session.
func (u *UserSessionsApp) DeleteRequest(writer http.ResponseWriter, r *http.Request) {
var (
username string
userExists bool
hasSession bool
err error
ok bool
v = mux.Vars(r)
)
if username, ok = v["username"]; !ok {
badRequest(writer, "Missing username in URL")
return
}
if userExists, err = u.sessions.isUser(username); err != nil {
badRequest(writer, fmt.Sprintf("Error checking for username %s: %s", username, err))
return
}
if !userExists {
badRequest(writer, fmt.Sprintf("User %s does not exist", username))
return
}
if hasSession, err = u.sessions.hasSessions(username); err != nil {
errored(writer, fmt.Sprintf("Error checking session for user %s: %s", username, err))
return
}
if !hasSession {
return
}
if err = u.sessions.deleteSession(username); err != nil {
errored(writer, fmt.Sprintf("Error deleting session for user %s: %s", username, err))
}
}