From a82184ee333b20b3e92e93ea2c530f4e965c4892 Mon Sep 17 00:00:00 2001 From: Tordarus Date: Sun, 2 Feb 2025 19:47:56 +0100 Subject: [PATCH] refactor --- go.mod | 2 +- go.sum | 4 ++++ main.go | 50 +++++++++++++++++++++++++------------------------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index a6158a2..c5ec693 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( git.tordarus.net/tordarus/channel v0.1.18 git.tordarus.net/tordarus/envvars v0.0.0-20250114175450-d73e12b838a5 git.tordarus.net/tordarus/gmath v0.0.7 - git.tordarus.net/tordarus/omada-api v0.0.2 + git.tordarus.net/tordarus/omada-api v0.0.4 git.tordarus.net/tordarus/slices v0.0.13 ) diff --git a/go.sum b/go.sum index aa5ac7c..2317f36 100644 --- a/go.sum +++ b/go.sum @@ -8,5 +8,9 @@ git.tordarus.net/tordarus/gmath v0.0.7 h1:tR48idt9AUL0r556ww3ZxByTKJEr6NWCTlhl2i git.tordarus.net/tordarus/gmath v0.0.7/go.mod h1:mO7aPlvNrGVE9UFXEuuACjZgMDsM63l3OcQy6xSQnoE= git.tordarus.net/tordarus/omada-api v0.0.2 h1:Pe7tLRRyPBasEvFam6c+83A3axMcxUKgPDmAXAkJMzQ= git.tordarus.net/tordarus/omada-api v0.0.2/go.mod h1:Ufp8hdXMyrXK7JFHq4WL1WSIlr9L6rhOBfQnqWNpXyM= +git.tordarus.net/tordarus/omada-api v0.0.3 h1:FKZNiqnT8HaNkR35XSokFLf5wfk4IwiEYc4RErlxqiY= +git.tordarus.net/tordarus/omada-api v0.0.3/go.mod h1:Ufp8hdXMyrXK7JFHq4WL1WSIlr9L6rhOBfQnqWNpXyM= +git.tordarus.net/tordarus/omada-api v0.0.4 h1:hr9muVyKhguj6NVgAnKCbGOZ2CpMykzUblxWrJNLgek= +git.tordarus.net/tordarus/omada-api v0.0.4/go.mod h1:Ufp8hdXMyrXK7JFHq4WL1WSIlr9L6rhOBfQnqWNpXyM= git.tordarus.net/tordarus/slices v0.0.13 h1:S/6PtIxAtERwlyQvZAZZaq6RVo2kLrAsrA+19ThRRvY= git.tordarus.net/tordarus/slices v0.0.13/go.mod h1:RgE7A1aSAezIvPUgcbUuMHu0q4xGKoRevT+DC0eJmwI= diff --git a/main.go b/main.go index 09c9ac5..3da47f3 100644 --- a/main.go +++ b/main.go @@ -18,13 +18,14 @@ import ( var ( // flags FlagSiteNames = envvars.StringSlice("SITES", ",", []string{}) - FlagOmadaURL = envvars.String("OMADA_URL", "http://localhost:8088") - FlagOmadaID = envvars.String("OMADA_ID", "") - FlagOmadaClientID = envvars.String("OMADA_CLIENT_ID", "") - FlagOmadaClientSecret = envvars.String("OMADA_CLIENT_SECRET", "") - FlagOmadaUsername = envvars.String("OMADA_USERNAME", "") - FlagOmadaPassword = envvars.String("OMADA_PASSWORD", "") - FlagRefreshInterval = envvars.Duration("REFRESH_INTERVAL", 30*time.Second) + FlagOmadaURL = envvars.String("OMADA_URL", "http://localhost:8088") + FlagOmadaID = envvars.String("OMADA_ID", "") + FlagOmadaClientID = envvars.String("OMADA_CLIENT_ID", "") + FlagOmadaClientSecret = envvars.String("OMADA_CLIENT_SECRET", "") + FlagOmadaUsername = envvars.String("OMADA_USERNAME", "") + FlagOmadaPassword = envvars.String("OMADA_PASSWORD", "") + FlagRefreshInterval = envvars.Duration("REFRESH_INTERVAL", 30*time.Second) + FlagRefreshBeforeExpiration = envvars.Duration("REFRESH_BEFORE", time.Minute) FlagHttpInterface = envvars.String("HTTP_INTERFACE", "") FlagHttpPort = envvars.Uint16("HTTP_PORT", 8080) @@ -50,29 +51,14 @@ func main() { panic(err) } - go func() { - if err := api.AutoRefresh(context.Background()); err != nil { - panic(err) - } - }() - - go func() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if err := json.NewEncoder(w).Encode(CurrentTraffic); err != nil { - w.WriteHeader(http.StatusInternalServerError) - } - }) - - http.ListenAndServe(fmt.Sprintf("%s:%d", FlagHttpInterface, FlagHttpPort), nil) - }() + go api.MustAutoRefresh(context.Background(), FlagRefreshBeforeExpiration) + go StartHttpServer() sites := slices.Filter(channel.ToSlice(api.GetSites()), FilterSitesByName(FlagSiteNames...)) - ticker := time.NewTicker(FlagRefreshInterval) - CalculateSiteTraffic(api, sites, 0) - lastTick := time.Now() + CalculateSiteTraffic(api, sites, 0) for now := range ticker.C { CurrentTraffic = CalculateSiteTraffic(api, sites, now.Sub(lastTick)) lastTick = now @@ -109,3 +95,17 @@ func FilterSitesByName(allowedSiteNames ...string) func(site *omadamodel.Site) b return ok } } + +func StartHttpServer() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if err := json.NewEncoder(w).Encode(CurrentTraffic); err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + }) + + connString := fmt.Sprintf("%s:%d", FlagHttpInterface, FlagHttpPort) + + if err := http.ListenAndServe(connString, nil); err != nil { + panic(err) + } +}