diff --git a/main.go b/main.go index 9f90ce3..4c8a747 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "fmt" "jannex/log-manager/modules/cache" "jannex/log-manager/modules/config" + "jannex/log-manager/modules/loghandler" "jannex/log-manager/modules/structs" "jannex/log-manager/modules/utils" "jannex/log-manager/routers/router" @@ -36,6 +37,12 @@ func init() { utils.ValidatorInit() utils.CreateDirectoryIfNotExists(config.Cfg.LogFolder) + + // checking for deletable logs on startup + loghandler.CheckForDeletableLogs() + + // starting the background log deleter + go loghandler.StartBackgroundLogDeleter() } func main() { diff --git a/modules/config/config.go b/modules/config/config.go index e5c0736..e1160b7 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -2,7 +2,9 @@ package config import ( "fmt" + "log" "os" + "strconv" "github.com/joho/godotenv" ) @@ -14,6 +16,7 @@ type Config struct { Port string LogFolder string SSEServerEnabled bool + DaysToKeepLogs int } func LoadConfig() { @@ -25,10 +28,17 @@ func LoadConfig() { fmt.Println("Load env from system") } + daysToKeepLogs, err := strconv.Atoi(os.Getenv("DAYS_TO_KEEP_LOGS")) + + if err != nil { + log.Fatalf("Failed to load DAYS_TO_KEEP_LOGS from env, err: %s", err.Error()) + } + Cfg = Config{ Host: os.Getenv("HOST"), Port: os.Getenv("PORT"), LogFolder: os.Getenv("LOG_FOLDER"), SSEServerEnabled: os.Getenv("SSE_SERVER_ENABLED") == "true", + DaysToKeepLogs: daysToKeepLogs, } } diff --git a/modules/loghandler/loghandler.go b/modules/loghandler/loghandler.go index e50df29..e32918b 100644 --- a/modules/loghandler/loghandler.go +++ b/modules/loghandler/loghandler.go @@ -9,6 +9,7 @@ import ( "jannex/log-manager/modules/structs" "jannex/log-manager/modules/utils" "os" + "path/filepath" "sort" "strconv" "strings" @@ -162,3 +163,66 @@ func GetAvailableLogTypes() []string { return availableLogTypes } + +func StartBackgroundLogDeleter() { + ticker := time.NewTicker(24 * time.Hour) + + for range ticker.C { + CheckForDeletableLogs() + } +} + +func CheckForDeletableLogs() { + + daysToKeepLogs := config.Cfg.DaysToKeepLogs + + logFolder := config.Cfg.LogFolder + + err := filepath.Walk(logFolder, func(path string, info os.FileInfo, err error) error { + if err != nil { + fmt.Println(err) + return err + } + + + if !info.IsDir() { + // check if the file is older than the daysToKeepLogs + // get the date from the file name + date := strings.Split(info.Name(), ".")[0] + + dateFormat := "2-1-2006" + + fileDate, err := time.Parse(dateFormat, date) + + if err != nil { + fmt.Println(err) + return err + } + + // get the date from the daysToKeepLogs + daysToKeepLogsDate := time.Now().AddDate(0, 0, -daysToKeepLogs) + + // compare the dates + if fileDate.Before(daysToKeepLogsDate) { + // delete the file + err := os.Remove(path) + + if err != nil { + fmt.Println(err) + return err + } + + AddLog(structs.LogBody{ + Type: "system", + Logs: []string{"I "+ utils.GetTime() + "LogManager: Deleted log file " + path + " because it was older than " + strconv.Itoa(daysToKeepLogs) + " days."}, + }) + } + } + + return nil + }) + + if err != nil { + fmt.Println(err) + } +} \ No newline at end of file diff --git a/modules/utils/utils.go b/modules/utils/utils.go index b365939..ba511ac 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "errors" "os" + "time" "github.com/gofiber/fiber/v2" ) @@ -49,3 +50,7 @@ func CreateDirectoryIfNotExists(path string) { os.Mkdir(path, 0755) } } + +func GetTime() string { + return time.Now().Format("15:04:05 02-01-2006") + " " +} \ No newline at end of file