feat: logout route and button

This commit is contained in:
efim
2023-11-14 04:18:25 +00:00
parent 1297fcf35d
commit 83e81ec011
9 changed files with 130 additions and 22 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"math/rand"
"time"
"sunshine.industries/some-automoderation/rooms"
@@ -19,11 +20,10 @@ type SessionData struct {
type SessionManagement interface {
Get(sessionId int) SessionData
Save(roomName string, personId rooms.PersonId) (int, error)
Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error)
Remove(ctx context.Context, sessionId int) error
}
var ctx = context.Background()
const sessionPrefix = "session"
func sessionIdToKey(sessionId int) string {
@@ -37,7 +37,7 @@ type RedisSM struct {
func (redisSM RedisSM) Get(sessionId int) SessionData {
var foundSession SessionData
redisKey := sessionIdToKey(sessionId)
err := redisSM.Rdb.HGetAll(ctx, redisKey).Scan(&foundSession)
err := redisSM.Rdb.HGetAll(context.TODO(), redisKey).Scan(&foundSession)
if err != nil {
log.Printf("> error reading %s : %s", redisKey, err)
return SessionData{}
@@ -45,7 +45,7 @@ func (redisSM RedisSM) Get(sessionId int) SessionData {
log.Printf("> successfully found %d %+v", sessionId, foundSession)
return foundSession
}
func (redisSM RedisSM) Save(roomName string, personId rooms.PersonId) (int, error) {
func (redisSM RedisSM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error) {
randId := rand.Int()
newSession := SessionData{
SessionId: randId,
@@ -53,6 +53,8 @@ func (redisSM RedisSM) Save(roomName string, personId rooms.PersonId) (int, erro
PersonId: personId,
}
err := redisSM.Rdb.HSet(ctx, sessionIdToKey(randId), newSession).Err()
redisSM.Rdb.Expire(ctx, sessionIdToKey(randId), 24 * time.Hour)
if err != nil {
log.Printf("> error! saving session %+v %s", newSession, err)
return 0, fmt.Errorf("error saving new session: %+v with %s", newSession, err)
@@ -60,13 +62,22 @@ func (redisSM RedisSM) Save(roomName string, personId rooms.PersonId) (int, erro
return randId, nil
}
func (redisSM RedisSM) Remove(ctx context.Context, sessionId int) error {
err := redisSM.Rdb.Del(ctx, sessionIdToKey(sessionId)).Err()
return err
}
type DummySM struct{}
func (d DummySM) Get(sessionId int) SessionData {
log.Printf("get dummy session by %d", sessionId)
return SessionData{}
}
func (d DummySM) Save(roomName string, personId rooms.PersonId) (int, error) {
func (d DummySM) Save(ctx context.Context, roomName string, personId rooms.PersonId) (int, error) {
log.Printf("save dummy session with %s %d", roomName, personId)
return 1, nil
}
func (d DummySM) Remove(ctx context.Context, sessionId int) error {
log.Printf("deleting session %d", sessionId)
return nil
}