fix crash on load from nil session info
This commit is contained in:
@@ -9,21 +9,24 @@ import (
|
|||||||
|
|
||||||
type RedisStore struct {
|
type RedisStore struct {
|
||||||
client *redis.Client
|
client *redis.Client
|
||||||
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRedisStore() *RedisStore {
|
func NewRedisStore() *RedisStore {
|
||||||
logging.Debug("Creating new redis session store")
|
logging.Debug("Creating new redis session store")
|
||||||
|
|
||||||
redis_server := "localhost:6379"
|
// this will be replaced with a URL that can be parsed in the config file
|
||||||
|
redis_server := "redis://localhost:6379/0"
|
||||||
|
|
||||||
// redis values will need to be loaded from the config file
|
opts, err := redis.ParseURL(redis_server)
|
||||||
rdb := redis.NewClient(&redis.Options{
|
if err != nil {
|
||||||
Addr: redis_server,
|
logging.Errorf("Failed to parse redis url %s", err.Error())
|
||||||
Password: "",
|
}
|
||||||
DB: 0,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err := rdb.Ping(context.Background()).Err(); err != nil {
|
rdb := redis.NewClient(opts)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||||
logging.Errorf("Failed to connect to redis server %s", redis_server)
|
logging.Errorf("Failed to connect to redis server %s", redis_server)
|
||||||
} else {
|
} else {
|
||||||
logging.Infof("Successfully connected to redis server %s", redis_server)
|
logging.Infof("Successfully connected to redis server %s", redis_server)
|
||||||
@@ -31,6 +34,7 @@ func NewRedisStore() *RedisStore {
|
|||||||
|
|
||||||
store := &RedisStore{
|
store := &RedisStore{
|
||||||
client: rdb,
|
client: rdb,
|
||||||
|
ctx: ctx,
|
||||||
}
|
}
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
@@ -39,6 +43,20 @@ func (m *RedisStore) Create(sessionID string, session *SessionData) (err error)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (m *RedisStore) Get(sessionID string) (*SessionData, error) {
|
func (m *RedisStore) Get(sessionID string) (*SessionData, error) {
|
||||||
|
hashed := hashSession(sessionID)
|
||||||
|
|
||||||
|
_, err := m.client.Get(m.ctx, hashed).Result()
|
||||||
|
if err == redis.Nil {
|
||||||
|
return nil, ErrSessionNotFound
|
||||||
|
} else if err != nil {
|
||||||
|
logging.Error(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// if time.Now().After(data.ExpiresAt) {
|
||||||
|
// _ = m.Delete(sessionID) // ignore error
|
||||||
|
// return nil, ErrSessionExpired
|
||||||
|
// }
|
||||||
|
// copy := *data
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
func (m *RedisStore) Update(sessionID string, session *SessionData) error {
|
func (m *RedisStore) Update(sessionID string, session *SessionData) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user