|
| 1 | +package session |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +/* we make use of Redis hashes for this application */ |
| 10 | + |
| 11 | +/* store session into Redis database */ |
| 12 | +func (m *Manager) saveSession(username string) error { |
| 13 | + ctx := context.Background() |
| 14 | + |
| 15 | + /* find the session in session map */ |
| 16 | + session, ok := m.sessionsMap[username] |
| 17 | + if !ok { |
| 18 | + return fmt.Errorf("username not found in session") |
| 19 | + } |
| 20 | + |
| 21 | + /* session key for redis */ |
| 22 | + key := fmt.Sprintf("session:%s", session.ID) |
| 23 | + |
| 24 | + /* serialize the session with relevant information */ |
| 25 | + sessionSerialized := session.serializeSessionForRedis() |
| 26 | + |
| 27 | + if err := m.redis.HSet(ctx, key, sessionSerialized).Err(); err != nil { |
| 28 | + return fmt.Errorf("failed to save session to Redis: %w", err) |
| 29 | + } |
| 30 | + |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +/* update expiry time in session */ |
| 35 | +func (m *Manager) updateSessionExpiry(username string) error { |
| 36 | + |
| 37 | + /* |
| 38 | + function expects that new expiry time is already set in the session |
| 39 | + */ |
| 40 | + |
| 41 | + ctx := context.Background() |
| 42 | + |
| 43 | + /* find the session in session map */ |
| 44 | + session, ok := m.sessionsMap[username] |
| 45 | + if !ok { |
| 46 | + return fmt.Errorf("username not found in session") |
| 47 | + } |
| 48 | + |
| 49 | + /* create a key for Redis operation */ |
| 50 | + key := fmt.Sprintf("session:%s", session.ID) |
| 51 | + |
| 52 | + /* convert the expiry time to */ |
| 53 | + formattedExpiry := session.Expiry.Format(time.RFC3339) |
| 54 | + |
| 55 | + /* update just the expiry field */ |
| 56 | + err := m.redis.HSet(ctx, key, "expiry", formattedExpiry).Err() |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("failed to update session expiry in Redis: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +/* TODO: Sessions must be marked expired when main.go exits */ |
| 65 | + |
| 66 | +/* update status of the session - update and set expired operations will be done with this */ |
| 67 | +func (m *Manager) updateSessionStatus(username string, status Status) error { |
| 68 | + |
| 69 | + ctx := context.Background() |
| 70 | + |
| 71 | + /* find the session in session map */ |
| 72 | + session, ok := m.sessionsMap[username] |
| 73 | + if !ok { |
| 74 | + return fmt.Errorf("username not found in session") |
| 75 | + } |
| 76 | + |
| 77 | + /* create a key for Redis operation */ |
| 78 | + key := fmt.Sprintf("session:%s", session.ID) |
| 79 | + |
| 80 | + /* update the session status */ |
| 81 | + err := m.redis.HSet(ctx, key, "status", status).Err() |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("failed to mark session as expired in Redis: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
0 commit comments