From 22a5b0dac3b02b3bbf9f4debd315693728256ce4 Mon Sep 17 00:00:00 2001 From: Thomas Labarussias Date: Thu, 3 Aug 2023 19:40:19 +0200 Subject: [PATCH] allow to set the TTL for keys with int + unit Signed-off-by: Thomas Labarussias --- README.md | 5 +++-- docs/docs.go | 6 ++++++ docs/swagger.json | 6 ++++++ docs/swagger.yaml | 4 ++++ internal/utils/utils.go | 7 ++++++- main.go | 9 +++++---- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 87854cc..dee22be 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,9 @@ Usage of Falcosidekick-UI: Listen Port (default "2802", environment "FALCOSIDEKICK_UI_PORT") -r string Redis server address (default "localhost:6379", environment "FALCOSIDEKICK_UI_REDIS_URL") --t int - TTL for keys (default "0", environment "FALCOSIDEKICK_UI_TTL") +-t string + TTL for keys, the format is X, + with unit (s, m, h, d, W, M, y)" (default "0", environment "FALCOSIDEKICK_UI_TTL") -u string User in format : (default "admin:admin", environment "FALCOSIDEKICK_UI_USER") -w string diff --git a/docs/docs.go b/docs/docs.go index 1db4db3..237becf 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -401,6 +401,9 @@ const docTemplate = `{ "dev-mode": { "type": "boolean" }, + "disable-auth": { + "type": "boolean" + }, "listen-address": { "description": "DisplayMode string ` + "`" + `json:\"display-mode\"` + "`" + `", "type": "string" @@ -411,6 +414,9 @@ const docTemplate = `{ "log-level": { "type": "string" }, + "redis-password": { + "type": "string" + }, "redis-server": { "type": "string" }, diff --git a/docs/swagger.json b/docs/swagger.json index 838c71a..ebd1ce8 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -397,6 +397,9 @@ "dev-mode": { "type": "boolean" }, + "disable-auth": { + "type": "boolean" + }, "listen-address": { "description": "DisplayMode string `json:\"display-mode\"`", "type": "string" @@ -407,6 +410,9 @@ "log-level": { "type": "string" }, + "redis-password": { + "type": "string" + }, "redis-server": { "type": "string" }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index a884c5c..57748c5 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -8,6 +8,8 @@ definitions: type: string dev-mode: type: boolean + disable-auth: + type: boolean listen-address: description: DisplayMode string `json:"display-mode"` type: string @@ -15,6 +17,8 @@ definitions: type: integer log-level: type: string + redis-password: + type: string redis-server: type: string ttl: diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 7c93234..baea142 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -79,8 +79,13 @@ func ConvertToSeconds(s string) int { return n * 30 * 24 * 60 * 60 case "y", "year", "years": return n * 365 * 24 * 60 * 60 + default: + o, err := strconv.Atoi(s) + if err != nil { + WriteLog("fatal", "invalid TTL") + } + return o } - return 0 } func RemoveDuplicate(input []string) []string { diff --git a/main.go b/main.go index b1247f6..b9664c9 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ func init() { redisserver := utils.GetStringFlagOrEnvParam("r", "FALCOSIDEKICK_UI_REDIS_URL", "localhost:6379", "Redis server address") redispassword := utils.GetStringFlagOrEnvParam("w", "FALCOSIDEKICK_UI_REDIS_PASSWORD", "", "Redis server password") port := utils.GetIntFlagOrEnvParam("p", "FALCOSIDEKICK_UI_PORT", 2802, "Listen Port") - ttl := utils.GetIntFlagOrEnvParam("t", "FALCOSIDEKICK_UI_TTL", 0, "TTL for keys") + ttl := utils.GetStringFlagOrEnvParam("t", "FALCOSIDEKICK_UI_TTL", "0s", "TTL for keys, the format is X, with unit (s, m, h, d, W, M, y)") version := flag.Bool("v", false, "Print version") dev := utils.GetBoolFlagOrEnvParam("x", "FALCOSIDEKICK_UI_DEV", false, "Allow CORS for development") loglevel := utils.GetStringFlagOrEnvParam("l", "FALCOSIDEKICK_UI_LOGLEVEL", "info", "Log Level") @@ -50,8 +50,9 @@ func init() { Listen Port (default "2802", environment "FALCOSIDEKICK_UI_PORT") -r string Redis server address (default "localhost:6379", environment "FALCOSIDEKICK_UI_REDIS_URL") --t int - TTL for keys (default "0", environment "FALCOSIDEKICK_UI_TTL") +-t string + TTL for keys, the format is X, + with unit (s, m, h, d, W, M, y)" (default "0", environment "FALCOSIDEKICK_UI_TTL") -u string User in format : (default "admin:admin", environment "FALCOSIDEKICK_UI_USER") -w string @@ -84,7 +85,7 @@ func init() { config.RedisServer = *redisserver config.RedisPassword = *redispassword config.DevMode = *dev - config.TTL = *ttl + config.TTL = utils.ConvertToSeconds(*ttl) config.LogLevel = *loglevel config.Credentials = *user config.DisableAuth = *disableauth