feat: room manager and pushed to login page route

This commit is contained in:
efim 2023-10-29 15:18:27 +00:00
parent 1c62f80064
commit bd99eaa54d
5 changed files with 51 additions and 51 deletions

View File

@ -32,10 +32,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)
routes.RegisterRoutes(sessions, rooms)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}

View File

@ -3,6 +3,7 @@ package rooms
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/redis/go-redis/v9"
@ -15,9 +16,8 @@ type Person struct {
}
type Room struct {
RoomId int64
Name string // will be unique ID
AdminIds []int64
Name string
PasswordHash string
Paricipants []Person
// TODO hands, for each type of hand?
@ -39,45 +39,32 @@ func (r *Room) UnmarshalBinary(data []byte) error {
var ctx = context.Background()
type RoomManager interface {
Get(roomName string) (Room, error) // but we need Get by name
Save(room Room) error
}
const roomRedisPrefix = "room"
func roomNameToRedisId(roomName string) string {
return fmt.Sprintf("%s:%s", roomRedisPrefix, roomName)
}
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()
func (redisRM RedisRM) Get(roomName string) (Room, error) {
var readRoom Room
err := redisRM.Rdb.Get(ctx, roomNameToRedisId(roomName)).Scan(&readRoom)
if err != nil {
log.Printf("error writing room: %s", err)
} else {
log.Printf("successfully written room %d", testRoom.RoomId)
log.Printf("error reading room with id %s : %s", roomName, err)
return Room{}, err
}
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)
}
return readRoom, nil
}
func (redisRM RedisRM) Save(room Room) error {
err := redisRM.Rdb.Set(ctx, roomNameToRedisId(room.Name), room, 0).Err() // maybe even set expiration?
return err
}

View File

@ -7,10 +7,15 @@ import (
"log"
"net/http"
"sunshine.industries/some-automoderation/rooms"
"sunshine.industries/some-automoderation/sessions"
)
func registerLoginRoutes(templateFs *embed.FS, sessionSM sessions.SessionManagement) {
func registerLoginRoutes(
templateFs *embed.FS,
sessionSM sessions.SessionManagement,
rooms rooms.RoomManager,
) {
// login page
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
var templFile = "templates/login.gohtml"
@ -33,15 +38,23 @@ func registerLoginRoutes(templateFs *embed.FS, sessionSM sessions.SessionManagem
pn := r.PostFormValue("personalName")
pp := r.PostFormValue("personalPassword")
roomId := 1 // would be taken from rooms interface from redis
room, err := rooms.Get(rn)
if err != nil {
log.Printf("/login/submit error getting room %s", rn)
// return i guess
} else {
log.Printf("/login/submit found room %+v", room)
}
roomId := "room-name-actually" // would be taken from rooms interface from redis
// would be either taken from room info on correct person pass or created
personId := 111
id, err := sessionSM.Save(int64(roomId), int64(personId))
id, err := sessionSM.Save(roomId, int64(personId))
if err != nil {
log.Printf("/login/submit > error saving session %s", err)
}
fmt.Fprintf(w, "is is %d. room things %s & %s, personal things %s and %s", id, rn, rp, pn, pp)
fmt.Fprintf(w, "is is %d. room things %s & %s, personal things %s and %s. \n found room %+v", id, rn, rp, pn, pp, room)
// i suppose here i'll need to
// a) check if room password OK
// b) get room data

View File

@ -6,6 +6,7 @@ import (
"log"
"net/http"
"sunshine.industries/some-automoderation/rooms"
"sunshine.industries/some-automoderation/sessions"
)
@ -15,9 +16,9 @@ var templateFs embed.FS
//go:embed static
var staticFilesFs embed.FS
func RegisterRoutes(sessions sessions.SessionManagement) {
func RegisterRoutes(sessions sessions.SessionManagement, rooms rooms.RoomManager) {
// login page
registerLoginRoutes(&templateFs, sessions)
registerLoginRoutes(&templateFs, sessions, rooms)
// main page template
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

View File

@ -11,13 +11,13 @@ import (
type SessionData struct {
SessionId int64 `redis:"session_id"`
RoomId int64 `redis:"room_id"`
RoomId string `redis:"room_id"`
PersonId int64 `redis:"person_id"`
}
type SessionManagement interface {
Get(sessionId int64) SessionData
Save(roomId int64, personId int64) (int64, error)
Save(roomId string, personId int64) (int64, error)
}
var ctx = context.Background()
@ -42,7 +42,7 @@ func (redisSM RedisSM) Get(sessionId int64) SessionData {
log.Printf("> successfully found %d %+v", sessionId, foundSession)
return foundSession
}
func (redisSM RedisSM) Save(roomId int64, personId int64) (int64, error) {
func (redisSM RedisSM) Save(roomId string, personId int64) (int64, error) {
randId := rand.Int63()
newSession := SessionData{
SessionId: randId,
@ -63,7 +63,7 @@ func (d DummySM) Get(sessionId int64) SessionData {
log.Printf("get dummy session by %d", sessionId)
return SessionData{}
}
func (d DummySM) Save(roomId int64, personId int64) (int64, error) {
func (d DummySM) Save(roomId string, personId int64) (int64, error) {
log.Printf("save dummy session with %d %d", roomId, personId)
return 1, nil
}