feat: adding redis to save sessions
This commit is contained in:
parent
bde58a0eab
commit
0591a28f8a
|
@ -1,2 +1,3 @@
|
||||||
.direnv
|
.direnv
|
||||||
/.go/
|
/.go/
|
||||||
|
/dump.rdb
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
pkgs.nodePackages.tailwindcss
|
pkgs.nodePackages.tailwindcss
|
||||||
pkgs.nodePackages.prettier
|
pkgs.nodePackages.prettier
|
||||||
pkgs.gnumake
|
pkgs.gnumake
|
||||||
|
pkgs.redis
|
||||||
];
|
];
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
export GOPATH=$PWD/.go
|
export GOPATH=$PWD/.go
|
||||||
|
|
6
go.mod
6
go.mod
|
@ -1,3 +1,9 @@
|
||||||
module sunshine.industries/some-automoderation
|
module sunshine.industries/some-automoderation
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
github.com/redis/go-redis/v9 v9.2.1 // indirect
|
||||||
|
)
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
|
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
|
||||||
|
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
17
main.go
17
main.go
|
@ -1,24 +1,37 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
|
||||||
"sunshine.industries/some-automoderation/routes"
|
"sunshine.industries/some-automoderation/routes"
|
||||||
"sunshine.industries/some-automoderation/sessions"
|
"sunshine.industries/some-automoderation/sessions"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ctx = context.Background()
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var port int
|
var port int
|
||||||
flag.IntVar(&port, "port", 8080, "Port on which the server should start")
|
flag.IntVar(&port, "port", 8080, "Port on which the server should start")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
var redisPort int
|
||||||
|
flag.IntVar(&redisPort, "redisPort", 7777, "Port on which server should connect to redis db")
|
||||||
|
|
||||||
|
rdb := redis.NewClient(&redis.Options{
|
||||||
|
Addr: fmt.Sprintf("localhost:%d", redisPort),
|
||||||
|
Password: "",
|
||||||
|
DB: 0,
|
||||||
|
})
|
||||||
|
|
||||||
fmt.Printf("Server will start on port %d\n", port)
|
fmt.Printf("Server will start on port %d\n", port)
|
||||||
|
|
||||||
sessions := sessions.DummySM{}
|
sessions := sessions.RedisSM{ Rdb: rdb, }
|
||||||
routes.RegisterRoutes(&sessions)
|
routes.RegisterRoutes(sessions)
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"sunshine.industries/some-automoderation/sessions"
|
"sunshine.industries/some-automoderation/sessions"
|
||||||
)
|
)
|
||||||
|
|
||||||
func registerLoginRoutes(templateFs *embed.FS, sessions *sessions.DummySM) {
|
func registerLoginRoutes(templateFs *embed.FS, sessionSM sessions.SessionManagement) {
|
||||||
// login page
|
// login page
|
||||||
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
var templFile = "templates/login.gohtml"
|
var templFile = "templates/login.gohtml"
|
||||||
|
@ -36,9 +36,12 @@ func registerLoginRoutes(templateFs *embed.FS, sessions *sessions.DummySM) {
|
||||||
roomId := 1 // would be taken from rooms interface from redis
|
roomId := 1 // would be taken from rooms interface from redis
|
||||||
// would be either taken from room info on correct person pass or created
|
// would be either taken from room info on correct person pass or created
|
||||||
personId := 111
|
personId := 111
|
||||||
sessions.Save(int64(roomId), int64(personId))
|
id, err := sessionSM.Save(int64(roomId), int64(personId))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("/login/submit > error saving session %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "room things %s & %s, personal things %s and %s", rn, rp, pn, pp)
|
fmt.Fprintf(w, "is is %d. room things %s & %s, personal things %s and %s", id, rn, rp, pn, pp)
|
||||||
// i suppose here i'll need to
|
// i suppose here i'll need to
|
||||||
// a) check if room password OK
|
// a) check if room password OK
|
||||||
// b) get room data
|
// b) get room data
|
||||||
|
|
|
@ -15,7 +15,7 @@ var templateFs embed.FS
|
||||||
//go:embed static
|
//go:embed static
|
||||||
var staticFilesFs embed.FS
|
var staticFilesFs embed.FS
|
||||||
|
|
||||||
func RegisterRoutes(sessions *sessions.DummySM) {
|
func RegisterRoutes(sessions sessions.SessionManagement) {
|
||||||
// login page
|
// login page
|
||||||
registerLoginRoutes(&templateFs, sessions)
|
registerLoginRoutes(&templateFs, sessions)
|
||||||
|
|
||||||
|
|
|
@ -1,27 +1,69 @@
|
||||||
package sessions
|
package sessions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SessionData struct {
|
type SessionData struct {
|
||||||
sessionId int64
|
SessionId int64 `redis:"session_id"`
|
||||||
roomId int64
|
RoomId int64 `redis:"room_id"`
|
||||||
personId int64
|
PersonId int64 `redis:"person_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionManagement interface {
|
type SessionManagement interface {
|
||||||
Get(sessionId int64) SessionData
|
Get(sessionId int64) SessionData
|
||||||
Save(roomId int64, personId int64) int64
|
Save(roomId int64, personId int64) (int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DummySM struct {}
|
var ctx = context.Background()
|
||||||
|
|
||||||
func (d DummySM)Get(sessionId int64) SessionData {
|
const sessionPrefix = "session"
|
||||||
|
|
||||||
|
func sessionIdToKey(sessionId int64) string {
|
||||||
|
return fmt.Sprintf("%s:%d", sessionPrefix, sessionId)
|
||||||
|
}
|
||||||
|
|
||||||
|
type RedisSM struct {
|
||||||
|
Rdb *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (redisSM RedisSM) Get(sessionId int64) SessionData {
|
||||||
|
var foundSession SessionData
|
||||||
|
err := redisSM.Rdb.HGetAll(ctx, sessionIdToKey(sessionId)).Scan(foundSession)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("> error reading %d", sessionId)
|
||||||
|
return SessionData{}
|
||||||
|
}
|
||||||
|
log.Printf("> successfully found %d %+v", sessionId, foundSession)
|
||||||
|
return foundSession
|
||||||
|
}
|
||||||
|
func (redisSM RedisSM) Save(roomId int64, personId int64) (int64, error) {
|
||||||
|
randId := rand.Int63()
|
||||||
|
newSession := SessionData{
|
||||||
|
SessionId: randId,
|
||||||
|
RoomId: roomId,
|
||||||
|
PersonId: personId,
|
||||||
|
}
|
||||||
|
err := redisSM.Rdb.HSet(ctx, sessionIdToKey(randId), newSession).Err()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
return randId, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type DummySM struct{}
|
||||||
|
|
||||||
|
func (d DummySM) Get(sessionId int64) SessionData {
|
||||||
log.Printf("get dummy session by %d", sessionId)
|
log.Printf("get dummy session by %d", sessionId)
|
||||||
return SessionData{}
|
return SessionData{}
|
||||||
}
|
}
|
||||||
func (d DummySM)Save(roomId int64, personId int64) int64 {
|
func (d DummySM) Save(roomId int64, personId int64) (int64, error) {
|
||||||
log.Printf("save dummy session with %d %d", roomId, personId)
|
log.Printf("save dummy session with %d %d", roomId, personId)
|
||||||
return 1
|
return 1, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue