fix crash on load from nil session info
This commit is contained in:
@@ -9,21 +9,24 @@ import (
|
||||
|
||||
type RedisStore struct {
|
||||
client *redis.Client
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewRedisStore() *RedisStore {
|
||||
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
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: redis_server,
|
||||
Password: "",
|
||||
DB: 0,
|
||||
})
|
||||
opts, err := redis.ParseURL(redis_server)
|
||||
if err != nil {
|
||||
logging.Errorf("Failed to parse redis url %s", err.Error())
|
||||
}
|
||||
|
||||
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)
|
||||
} else {
|
||||
logging.Infof("Successfully connected to redis server %s", redis_server)
|
||||
@@ -31,6 +34,7 @@ func NewRedisStore() *RedisStore {
|
||||
|
||||
store := &RedisStore{
|
||||
client: rdb,
|
||||
ctx: ctx,
|
||||
}
|
||||
return store
|
||||
}
|
||||
@@ -39,6 +43,20 @@ func (m *RedisStore) Create(sessionID string, session *SessionData) (err error)
|
||||
return nil
|
||||
}
|
||||
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
|
||||
}
|
||||
func (m *RedisStore) Update(sessionID string, session *SessionData) error {
|
||||
|
||||
Reference in New Issue
Block a user