From f9e9ef2dd142a30419a2ee7890fe25e000d2ebbc Mon Sep 17 00:00:00 2001 From: ngn Date: Fri, 22 Nov 2024 16:56:02 +0300 Subject: [PATCH] add update interval option --- README.md | 1 + go.mod | 8 ++++---- go.sum | 18 ++++++++++-------- lib/config.go | 13 ++++++------- main.go | 37 ++++++++++++++++++++++++------------- 5 files changed, 45 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 9c512b3..0b612d2 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ is stored in the `config.json` file. Here is the configuration for tracking official MatterLinux 24 repos: ```json { + "interval": 5, "pools": [ { "display": "base (stable)", diff --git a/go.mod b/go.mod index 697d899..3995958 100644 --- a/go.mod +++ b/go.mod @@ -9,17 +9,17 @@ require ( ) require ( - github.com/andybalholm/brotli v1.1.0 // indirect + github.com/andybalholm/brotli v1.1.1 // indirect github.com/gofiber/template v1.8.3 // indirect github.com/gofiber/utils v1.1.0 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/klauspost/compress v1.17.9 // indirect + github.com/klauspost/compress v1.17.11 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.55.0 // indirect + github.com/valyala/fasthttp v1.57.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect - golang.org/x/sys v0.24.0 // indirect + golang.org/x/sys v0.27.0 // indirect ) diff --git a/go.sum b/go.sum index a6bea7f..bd3cf2e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= -github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= +github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA= github.com/bigkevmcd/go-configparser v0.0.0-20240808124832-fc81059ea0bd h1:MsTk4yo6KVYdulsDscuH4AwiZN1CyuCJAg59EWE7HPQ= github.com/bigkevmcd/go-configparser v0.0.0-20240808124832-fc81059ea0bd/go.mod h1:vzEQfW+A1T+AMJmTIX+SXNLNECHOM7GEinHhw0IjykI= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -14,8 +14,8 @@ github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM= github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= -github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= +github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= @@ -36,14 +36,16 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8= -github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM= +github.com/valyala/fasthttp v1.57.0 h1:Xw8SjWGEP/+wAAgyy5XTvgrWlOD1+TxbbvNADYCm1Tg= +github.com/valyala/fasthttp v1.57.0/go.mod h1:h6ZBaPRlzpZ6O3H5t2gEk1Qi33+TmLvfwgLLp0t9CpE= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= +github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/lib/config.go b/lib/config.go index 56a5175..851ea00 100644 --- a/lib/config.go +++ b/lib/config.go @@ -6,7 +6,8 @@ import ( ) type Config struct { - Pools []Pool `json:"pools"` + Interval int `json:"interval"` + Pools []Pool `json:"pools"` } func (c *Config) Load(list *[]Package, file string) error { @@ -19,15 +20,13 @@ func (c *Config) Load(list *[]Package, file string) error { return err } + // default config values + c.Interval = 5 + c.Pools = []Pool{} + if err = json.Unmarshal(content, c); err != nil { return err } - for _, p := range c.Pools { - if err = p.Load(list); err != nil { - return err - } - } - return nil } diff --git a/main.go b/main.go index 941d679..3d0a5de 100644 --- a/main.go +++ b/main.go @@ -35,29 +35,42 @@ type tracker struct { Config lib.Config Last time.Time List []lib.Package - Tick time.Ticker + Tick *time.Ticker } func (t *tracker) Reload() error { t.List = []lib.Package{} - err := t.Config.Load(&t.List, "config.json") t.Last = time.Now() - return err + + for _, p := range t.Config.Pools { + if err := p.Load(&t.List); err != nil { + return err + } + } + + return nil } func (t *tracker) Loop() { - var err error - - if err = t.Reload(); err != nil { - log.Error("Failed to update packages: %s", err.Error()) + if err := t.Config.Load(&t.List, "config.json"); err != nil { + log.Error("Failed to load the configuration: %s", err.Error()) + return } + if err := t.Reload(); err != nil { + log.Error("Failed to update packages: %s", err.Error()) + return + } + + t.Tick = time.NewTicker(time.Minute * time.Duration(t.Config.Interval)) + for { select { case <-t.Tick.C: - if err = t.Reload(); err != nil { + if err := t.Reload(); err != nil { log.Error("Failed to update packages: %s", err.Error()) } + case <-t.Channel: t.Tick.Stop() return @@ -72,14 +85,13 @@ func (t *tracker) Stop() { func main() { var ( tracker tracker - engine *html.Engine app *fiber.App + err error ) - engine = html.New("./templates", ".html") app = fiber.New(fiber.Config{ DisableStartupMessage: true, - Views: engine, + Views: html.New("./templates", ".html"), }) app.Static("/", "./public") @@ -102,8 +114,7 @@ func main() { log.Info("Starting MatterLinux package tracker on port 9877") - err := app.Listen(":9877") - if err != nil { + if err = app.Listen(":9877"); err != nil { log.Error("Error starting server: %s", err.Error()) }