Skip to content

Commit 3f83815

Browse files
Updated transactions type
1 parent 1e2fddf commit 3f83815

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

internal/session/handler.go

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (m *Manager) IssueTransaction(w http.ResponseWriter, r *http.Request) {
4949
return
5050
}
5151

52-
tx := &types.Transaction{
52+
tx := types.Transaction{
5353
ID: uuid.New(),
5454
SessionID: session.ID,
5555
Timestamp: time.Now(),
@@ -92,12 +92,14 @@ user/
9292
*/
9393
func (m *Manager) StreamUserSession(w http.ResponseWriter, r *http.Request) {
9494

95+
/* get the username */
9596
username, ok := r.Context().Value(middleware.ContextKeyUsername).(string)
9697
if !ok {
9798
http.Error(w, "Invalid user context", http.StatusInternalServerError)
9899
return
99100
}
100101

102+
/* get the session id */
101103
sessionID, ok := r.Context().Value(middleware.ContextKeySessionID).(string)
102104
if !ok {
103105
http.Error(w, "Invalid session context", http.StatusInternalServerError)
@@ -113,7 +115,7 @@ func (m *Manager) StreamUserSession(w http.ResponseWriter, r *http.Request) {
113115
return
114116
}
115117

116-
/* add a check for sessionID belongs to user */
118+
/* user exists and verified, upgrade the websocket connection */
117119
conn, err := m.upgrader.Upgrade(w, r, nil)
118120
if err != nil {
119121
m.errCh <- fmt.Errorf("websocket upgrade error: %w", err)
@@ -151,12 +153,15 @@ requires user authentication from middleware
151153
user/
152154
*/
153155
func (m *Manager) StreamUserTransactions(w http.ResponseWriter, r *http.Request) {
156+
157+
/* get the username */
154158
username, ok := r.Context().Value(middleware.ContextKeyUsername).(string)
155159
if !ok {
156160
http.Error(w, "Invalid user context", http.StatusInternalServerError)
157161
return
158162
}
159163

164+
/* get the session id */
160165
sessionID, ok := r.Context().Value(middleware.ContextKeySessionID).(string)
161166
if !ok {
162167
http.Error(w, "Invalid session ID context", http.StatusInternalServerError)
@@ -172,7 +177,7 @@ func (m *Manager) StreamUserTransactions(w http.ResponseWriter, r *http.Request)
172177
return
173178
}
174179

175-
/* add a check for sessionid belongs to user */
180+
/* user exists and verified, upgrade the websocket connection */
176181
conn, err := m.upgrader.Upgrade(w, r, nil)
177182
if err != nil {
178183
m.errCh <- fmt.Errorf("websocket upgrade error: %w", err)
@@ -209,15 +214,79 @@ func (m *Manager) StreamUserTransactions(w http.ResponseWriter, r *http.Request)
209214
// requires admin authentication from middleware
210215
// admin/
211216
// */
212-
// func (m *manager) streamallsessions(w http.responsewriter, r *http.request) {
217+
// func (m *Manager) StreamAllSessions(w http.ResponseWriter, r *http.Request) {
218+
//
219+
// /* check if the user is admin */
220+
//
221+
// /* upgrade the connection if user is admin */
222+
// conn, err := m.upgrader.Upgrade(w, r, nil)
223+
// if err != nil {
224+
// m.errCh <- fmt.Errorf("websocket upgrade error: %w", err)
225+
// return
226+
// }
227+
// defer conn.Close()
228+
//
229+
// /*
230+
// context with cancel for web socket handlers
231+
// this is the official context for a websocket connection
232+
// cancelling this means closing components of the websocket handler
233+
// */
234+
// ctx, cancel := context.WithCancel(context.Background())
235+
// defer cancel()
236+
//
237+
// /* sending initial list of all sessions */
238+
// if err := m.sendListofAllSessions(conn, 100); err != nil {
239+
// m.errCh <- fmt.Errorf("error sending initial list of all sessions: %w", err)
240+
// return
241+
// }
242+
//
243+
// /* stream changes in transactions made in redis */
244+
// go m.listenForAllSessionsChanges(ctx, conn)
245+
//
246+
// /* specify the handler context */
247+
// ctxVal := context.WithValue(ctx, "type", CtxStreamAllSessions)
213248
//
249+
// /* handle web socket instructions from client */
250+
// m.handleWebSocketCommands(conn, ctxVal, cancel)
214251
// }
215252
//
216253
// /*
217254
// get all transaction in the system
218255
// requires admin authentication from middleware
219256
// admin/
220257
// */
221-
// func (m *manager) streamalltransactions(w http.responsewriter, r *http.request) {
258+
// func (m *Manager) StreamAllTransactions(w http.ResponseWriter, r *http.Request) {
259+
//
260+
// /* check if the user is admin */
261+
//
262+
// /* upgrade the connection if user is admin */
263+
// conn, err := m.upgrader.Upgrade(w, r, nil)
264+
// if err != nil {
265+
// m.errCh <- fmt.Errorf("websocket upgrade error: %w", err)
266+
// return
267+
// }
268+
// defer conn.Close()
269+
//
270+
// /*
271+
// context with cancel for web socket handlers
272+
// this is the official context for a websocket connection
273+
// cancelling this means closing components of the websocket handler
274+
// */
275+
// ctx, cancel := context.WithCancel(context.Background())
276+
// defer cancel()
277+
//
278+
// /* sending initial list of all sessions */
279+
// if err := m.sendListofAllTransactions(conn, 100); err != nil {
280+
// m.errCh <- fmt.Errorf("error sending initial list of all transactions: %w", err)
281+
// return
282+
// }
283+
//
284+
// /* stream changes in transactions made in redis */
285+
// go m.listenForAllTransactionsChanges(ctx, conn)
286+
//
287+
// /* specify the handler context */
288+
// ctxVal := context.WithValue(ctx, "type", CtxStreamAllTransactions)
222289
//
290+
// /* handle web socket instructions from client */
291+
// m.handleWebSocketCommands(conn, ctxVal, cancel)
223292
// }

0 commit comments

Comments
 (0)