feat: create-room handler, with middleware
This commit is contained in:
@@ -10,21 +10,21 @@ import (
|
||||
)
|
||||
|
||||
type SessionData struct {
|
||||
SessionId int64 `redis:"session_id"`
|
||||
SessionId int `redis:"session_id"`
|
||||
RoomId string `redis:"room_id"`
|
||||
PersonId int64 `redis:"person_id"`
|
||||
PersonId int `redis:"person_id"`
|
||||
}
|
||||
|
||||
type SessionManagement interface {
|
||||
Get(sessionId int64) SessionData
|
||||
Save(roomId string, personId int64) (int64, error)
|
||||
Get(sessionId int) SessionData
|
||||
Save(roomName string, personId int) (int, error)
|
||||
}
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
const sessionPrefix = "session"
|
||||
|
||||
func sessionIdToKey(sessionId int64) string {
|
||||
func sessionIdToKey(sessionId int) string {
|
||||
return fmt.Sprintf("%s:%d", sessionPrefix, sessionId)
|
||||
}
|
||||
|
||||
@@ -32,21 +32,22 @@ type RedisSM struct {
|
||||
Rdb *redis.Client
|
||||
}
|
||||
|
||||
func (redisSM RedisSM) Get(sessionId int64) SessionData {
|
||||
func (redisSM RedisSM) Get(sessionId int) SessionData {
|
||||
var foundSession SessionData
|
||||
err := redisSM.Rdb.HGetAll(ctx, sessionIdToKey(sessionId)).Scan(foundSession)
|
||||
redisKey := sessionIdToKey(sessionId)
|
||||
err := redisSM.Rdb.HGetAll(ctx, redisKey).Scan(&foundSession)
|
||||
if err != nil {
|
||||
log.Printf("> error reading %d", sessionId)
|
||||
log.Printf("> error reading %s : %s", redisKey, err)
|
||||
return SessionData{}
|
||||
}
|
||||
log.Printf("> successfully found %d %+v", sessionId, foundSession)
|
||||
return foundSession
|
||||
}
|
||||
func (redisSM RedisSM) Save(roomId string, personId int64) (int64, error) {
|
||||
randId := rand.Int63()
|
||||
func (redisSM RedisSM) Save(roomName string, personId int) (int, error) {
|
||||
randId := rand.Int()
|
||||
newSession := SessionData{
|
||||
SessionId: randId,
|
||||
RoomId: roomId,
|
||||
RoomId: roomName,
|
||||
PersonId: personId,
|
||||
}
|
||||
err := redisSM.Rdb.HSet(ctx, sessionIdToKey(randId), newSession).Err()
|
||||
@@ -59,11 +60,11 @@ func (redisSM RedisSM) Save(roomId string, personId int64) (int64, error) {
|
||||
|
||||
type DummySM struct{}
|
||||
|
||||
func (d DummySM) Get(sessionId int64) SessionData {
|
||||
func (d DummySM) Get(sessionId int) SessionData {
|
||||
log.Printf("get dummy session by %d", sessionId)
|
||||
return SessionData{}
|
||||
}
|
||||
func (d DummySM) Save(roomId string, personId int64) (int64, error) {
|
||||
log.Printf("save dummy session with %d %d", roomId, personId)
|
||||
func (d DummySM) Save(roomName string, personId int) (int, error) {
|
||||
log.Printf("save dummy session with %s %d", roomName, personId)
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user