forked from mtusi2026/mtusi2026lab3
feat(auth): make session cache optional via SESSION_STORE
This commit is contained in:
@@ -10,11 +10,21 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
SessionStorePostgres = "postgres"
|
||||
SessionStoreRedis = "redis"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig
|
||||
Logging LoggingConfig
|
||||
Database DatabaseConfig
|
||||
Redis RedisConfig
|
||||
Server ServerConfig
|
||||
Logging LoggingConfig
|
||||
Database DatabaseConfig
|
||||
Redis RedisConfig
|
||||
SessionStore string
|
||||
}
|
||||
|
||||
func (c Config) UseCache() bool {
|
||||
return c.SessionStore == SessionStoreRedis
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
@@ -64,6 +74,8 @@ func Load() Config {
|
||||
Redis: RedisConfig{
|
||||
URLFile: getEnv("CACHE_URL_FILE", "/run/secrets/cache_url"),
|
||||
},
|
||||
|
||||
SessionStore: getEnvSessionStore("SESSION_STORE", SessionStorePostgres),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,3 +151,17 @@ func readSecretFile(path string) (string, error) {
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func getEnvSessionStore(key, def string) string {
|
||||
v := strings.ToLower(os.Getenv(key))
|
||||
|
||||
switch v {
|
||||
case "":
|
||||
return def
|
||||
case SessionStorePostgres, SessionStoreRedis:
|
||||
return v
|
||||
default:
|
||||
log.Printf("invalid session store for %s=%q, using default %s", key, v, def)
|
||||
return def
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user