feat: room model, encode json, sample redis write
This commit is contained in:
parent
0591a28f8a
commit
1c62f80064
4
main.go
4
main.go
|
@ -9,6 +9,7 @@ import (
|
|||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"sunshine.industries/some-automoderation/rooms"
|
||||
"sunshine.industries/some-automoderation/routes"
|
||||
"sunshine.industries/some-automoderation/sessions"
|
||||
)
|
||||
|
@ -30,6 +31,9 @@ func main() {
|
|||
|
||||
fmt.Printf("Server will start on port %d\n", port)
|
||||
|
||||
rooms := rooms.RedisRM { Rdb: rdb, }
|
||||
rooms.Test()
|
||||
|
||||
sessions := sessions.RedisSM{ Rdb: rdb, }
|
||||
routes.RegisterRoutes(sessions)
|
||||
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
package rooms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
PersonId int64
|
||||
Name string
|
||||
PasswordHash string
|
||||
}
|
||||
|
||||
type Room struct {
|
||||
RoomId int64
|
||||
AdminIds []int64
|
||||
Name string
|
||||
PasswordHash string
|
||||
Paricipants []Person
|
||||
// TODO hands, for each type of hand?
|
||||
// i guess participants order fixed for now?
|
||||
// and i'll still need 'current' for each hand level
|
||||
}
|
||||
|
||||
// well, it seems that i'd better do marshalling into bytes then
|
||||
// see https://github.com/redis/go-redis/issues/2512
|
||||
func (r *Room) MarshalBinary() (data []byte, err error) {
|
||||
return json.Marshal(r)
|
||||
}
|
||||
func (r *Room) UnmarshalBinary(data []byte) error {
|
||||
return json.Unmarshal(data, r)
|
||||
}
|
||||
|
||||
|
||||
// let's check whether it will be possible to save nested structs
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
type RedisRM struct {
|
||||
Rdb *redis.Client
|
||||
}
|
||||
|
||||
func (redisRM RedisRM) Test() {
|
||||
testRoom := Room{
|
||||
RoomId: 1234,
|
||||
AdminIds: []int64{1111, 2222},
|
||||
Name: "test room",
|
||||
PasswordHash: "abcde",
|
||||
Paricipants: []Person{
|
||||
{PersonId: 1111,
|
||||
Name: "admin1",
|
||||
PasswordHash: "abcde1",
|
||||
},
|
||||
{
|
||||
PersonId: 2222,
|
||||
Name: "admin2",
|
||||
PasswordHash: "abcdeee",
|
||||
},
|
||||
{
|
||||
PersonId: 3333,
|
||||
Name: "nonadmin yay",
|
||||
PasswordHash: "abcdee123",
|
||||
},
|
||||
},
|
||||
}
|
||||
err := redisRM.Rdb.Set(ctx, "room:my-room-1", &testRoom, 0).Err()
|
||||
if err != nil {
|
||||
log.Printf("error writing room: %s", err)
|
||||
} else {
|
||||
log.Printf("successfully written room %d", testRoom.RoomId)
|
||||
}
|
||||
|
||||
var readRoom Room
|
||||
err = redisRM.Rdb.Get(ctx, "room:my-room-1").Scan(&readRoom)
|
||||
if err != nil {
|
||||
log.Printf("error reading room %s", err)
|
||||
} else {
|
||||
log.Printf("successfully read room %+v", readRoom)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue