Update session.go
This commit is contained in:
+2
-29
@@ -5,44 +5,17 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/session/v2"
|
"github.com/gofiber/session/v2"
|
||||||
pgprovider "github.com/gofiber/session/v2/provider/postgres"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SessionConfig struct {
|
type SessionConfig struct {
|
||||||
*session.Session
|
*session.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup configura el almacén de sesiones en PostgreSQL con expiración de 1 año.
|
// Setup configura el almacén de sesiones en memoria con expiración de 1 año.
|
||||||
// Si el provider PostgreSQL falla (credenciales, red, etc.), usa memoria como fallback.
|
|
||||||
func (s *SessionConfig) Setup(db DatabaseDriver) error {
|
func (s *SessionConfig) Setup(db DatabaseDriver) error {
|
||||||
port := int64(db.Port)
|
|
||||||
if port == 0 {
|
|
||||||
port = 5432
|
|
||||||
}
|
|
||||||
|
|
||||||
provider, err := pgprovider.New(pgprovider.Config{
|
|
||||||
Host: db.Host,
|
|
||||||
Port: port,
|
|
||||||
Username: db.Username,
|
|
||||||
Password: db.Password,
|
|
||||||
Database: db.DBName,
|
|
||||||
TableName: "sessions",
|
|
||||||
MaxOpenConns: 20,
|
|
||||||
MaxIdleConns: 10,
|
|
||||||
ConnMaxLifetime: 5 * time.Minute,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("[SESSION] PostgreSQL provider error, usando memoria: %v", err)
|
|
||||||
s.Session = session.New(session.Config{
|
|
||||||
Expiration: 365 * 24 * time.Hour,
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
s.Session = session.New(session.Config{
|
s.Session = session.New(session.Config{
|
||||||
Expiration: 365 * 24 * time.Hour,
|
Expiration: 365 * 24 * time.Hour,
|
||||||
Provider: provider,
|
|
||||||
})
|
})
|
||||||
log.Println("[SESSION] Sesiones persistentes en PostgreSQL (tabla: sessions)")
|
log.Println("[SESSION] Sesiones en memoria (expiran en 1 año o al reiniciar)")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user