Skip to content

Commit 228384f

Browse files
Worked on setting up user session and user transactions streaming
1 parent 4658d81 commit 228384f

File tree

1 file changed

+95
-15
lines changed

1 file changed

+95
-15
lines changed

internal/session/handler.go

Lines changed: 95 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package session
22

33
import (
4+
"context"
45
"encoding/json"
6+
"fmt"
57
"net/http"
68
"time"
79

8-
"github.com/PythonHacker24/linux-acl-management-backend/internal/types"
910
"github.com/google/uuid"
11+
12+
"github.com/PythonHacker24/linux-acl-management-backend/api/middleware"
13+
"github.com/PythonHacker24/linux-acl-management-backend/internal/types"
1014
)
1115

1216
/*
@@ -19,7 +23,7 @@ import (
1923
/* frontend safe handler for issuing transaction */
2024
func (m *Manager) IssueTransaction(w http.ResponseWriter, r *http.Request) {
2125
/* extract username from JWT Token */
22-
username := r.Context().Value("username")
26+
username := r.Context().Value(middleware.ContextKeyUsername)
2327

2428
/* acquire manager lock to access sessions map */
2529
m.mutex.Lock()
@@ -65,26 +69,102 @@ func (m *Manager) IssueTransaction(w http.ResponseWriter, r *http.Request) {
6569
})
6670
}
6771

68-
/*
69-
get single session data
70-
requires user authentication from middleware
71-
user/
72+
type handlerCtxKey string
73+
74+
const (
75+
StreamUserSession handlerCtxKey = "stream_user_session"
76+
StreamUserTransactions handlerCtxKey = "stream_user_transactions"
77+
StreamAllSessions handlerCtxKey = "stream_all_sessions"
78+
StreamAllTransactions handlerCtxKey = "stream_all_transactions"
79+
)
80+
81+
/*
82+
get single session data
83+
requires user authentication from middleware
84+
user/
7285
*/
86+
func (m *Manager) StreamUserSession(w http.ResponseWriter, r *http.Request) {
87+
88+
/* username := r.Context().Value(middleware.ContextKeyUsername) */
89+
sessionID := r.Context().Value(middleware.ContextKeySessionID)
90+
91+
/* add a check for sessionID belongs to user */
92+
conn, err := m.upgrader.Upgrade(w, r, nil)
93+
if err != nil {
94+
m.errCh <- fmt.Errorf("websocket upgrade error: %w", err)
95+
return
96+
}
97+
defer conn.Close()
98+
99+
/* context with cancel for web socket handlers */
100+
ctx, cancel := context.WithCancel(context.Background())
101+
defer cancel()
102+
103+
/* sending initial session data */
104+
if err := m.sendCurrentSession(conn, sessionID); err != nil {
105+
// log.Printf("Error sending initial session: %v", err)
106+
m.errCh <- fmt.Errorf("error sending initial session: %w", err)
107+
return
108+
}
109+
110+
/* stream changes in session made in Redis */
111+
go m.listenForSessionChanges(ctx, conn, sessionID)
112+
113+
ctxVal := context.WithValue(ctx, "type", StreamUserSession)
114+
115+
/* handle web socket instructions from client */
116+
m.handleWebSocketCommands(conn, ctxVal, cancel)
117+
}
73118

74119
/*
75-
get user transactions information
76-
requires user authentication from middleware
77-
user/
120+
get user transactions information
121+
requires user authentication from middleware
122+
user/
78123
*/
124+
func (m *Manager) StreamUserTransactions(w http.ResponseWriter, r *http.Request) {
125+
/* username := r.Context().Value(middleware.ContextKeyUsername) */
126+
sessionID := r.Context().Value(middleware.ContextKeySessionID)
127+
128+
/* add a check for sessionID belongs to user */
129+
conn, err := m.upgrader.Upgrade(w, r, nil)
130+
if err != nil {
131+
m.errCh <- fmt.Errorf("websocket upgrade error: %w", err)
132+
return
133+
}
134+
defer conn.Close()
135+
136+
/* context with cancel for web socket handlers */
137+
ctx, cancel := context.WithCancel(context.Background())
138+
defer cancel()
139+
140+
/* sending initial list of transactions data */
141+
if err := m.sendCurrentTransactions(conn, sessionID); err != nil {
142+
// log.Printf("Error sending initial session: %v", err)
143+
m.errCh <- fmt.Errorf("error sending initial session: %w", err)
144+
return
145+
}
146+
147+
/* stream changes in transactions made in Redis */
148+
go m.listenForTransactionsChanges(ctx, conn, sessionID)
149+
150+
/* handle web socket instructions from client */
151+
m.handleWebSocketCommands(conn, cancel)
152+
}
79153

80154
/*
81-
get all sessions in the system
82-
requires admin authentication from middleware
83-
admin/
155+
get all sessions in the system
156+
requires admin authentication from middleware
157+
admin/
84158
*/
159+
func (m *Manager) StreamAllSessions(w http.ResponseWriter, r *http.Request) {
160+
161+
}
85162

86163
/*
87-
get all transaction in the system
88-
requires admin authentication from middleware
89-
admin/
164+
get all transaction in the system
165+
requires admin authentication from middleware
166+
admin/
90167
*/
168+
func (m *Manager) StreamAllTransactions(w http.ResponseWriter, r *http.Request) {
169+
170+
}

0 commit comments

Comments
 (0)