From 54a5cbc1351156f17af7b91a687b629097aca45c Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 10 Oct 2024 01:26:32 +0300 Subject: [PATCH] Code refactoring --- common/updown-badge-server.spec | 8 +- daemon/daemon.go | 165 +++++++++++++++++++++----------- daemon/server.go | 10 +- go.mod | 14 +-- go.sum | 32 ++++--- 5 files changed, 146 insertions(+), 83 deletions(-) diff --git a/common/updown-badge-server.spec b/common/updown-badge-server.spec index c18d30f..975f50c 100644 --- a/common/updown-badge-server.spec +++ b/common/updown-badge-server.spec @@ -14,7 +14,7 @@ Summary: Service for generating badges for updown.io checks Name: updown-badge-server -Version: 1.3.2 +Version: 1.4.0 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -26,7 +26,7 @@ Source100: checksum.sha512 BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) -BuildRequires: golang >= 1.21 +BuildRequires: golang >= 1.22 Requires: systemd @@ -100,6 +100,10 @@ exit 0 ################################################################################ %changelog +* Thu Oct 10 2024 Anton Novojilov - 1.4.0-0 +- Code refactoring +- Dependencies update + * Mon Jun 24 2024 Anton Novojilov - 1.3.2-0 - Code refactoring - Dependencies update diff --git a/daemon/daemon.go b/daemon/daemon.go index 90b3c4c..b26d18f 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -8,27 +8,30 @@ package daemon // ////////////////////////////////////////////////////////////////////////////////// // import ( + "fmt" "os" "runtime" - "strings" "time" - "github.com/essentialkaos/ek/v12/cache" - "github.com/essentialkaos/ek/v12/fmtc" - "github.com/essentialkaos/ek/v12/knf" - "github.com/essentialkaos/ek/v12/log" - "github.com/essentialkaos/ek/v12/options" - "github.com/essentialkaos/ek/v12/signal" - "github.com/essentialkaos/ek/v12/support" - "github.com/essentialkaos/ek/v12/support/deps" - "github.com/essentialkaos/ek/v12/terminal" - "github.com/essentialkaos/ek/v12/terminal/tty" - "github.com/essentialkaos/ek/v12/usage" - - knfv "github.com/essentialkaos/ek/v12/knf/validators" - knff "github.com/essentialkaos/ek/v12/knf/validators/fs" - knfn "github.com/essentialkaos/ek/v12/knf/validators/network" - knfr "github.com/essentialkaos/ek/v12/knf/validators/regexp" + "github.com/essentialkaos/ek/v13/cache" + "github.com/essentialkaos/ek/v13/cache/memory" + "github.com/essentialkaos/ek/v13/errutil" + "github.com/essentialkaos/ek/v13/fmtc" + "github.com/essentialkaos/ek/v13/knf" + "github.com/essentialkaos/ek/v13/log" + "github.com/essentialkaos/ek/v13/options" + "github.com/essentialkaos/ek/v13/signal" + "github.com/essentialkaos/ek/v13/support" + "github.com/essentialkaos/ek/v13/support/deps" + "github.com/essentialkaos/ek/v13/support/services" + "github.com/essentialkaos/ek/v13/terminal" + "github.com/essentialkaos/ek/v13/terminal/tty" + "github.com/essentialkaos/ek/v13/usage" + + knfv "github.com/essentialkaos/ek/v13/knf/validators" + knff "github.com/essentialkaos/ek/v13/knf/validators/fs" + knfn "github.com/essentialkaos/ek/v13/knf/validators/network" + knfr "github.com/essentialkaos/ek/v13/knf/validators/regexp" "github.com/essentialkaos/go-badge" @@ -42,7 +45,7 @@ import ( // Basic service info const ( APP = "UpDownBadgeServer" - VER = "1.3.2" + VER = "1.4.0" DESC = "Service for generating badges for updown.io checks" ) @@ -97,7 +100,7 @@ var optMap = options.Map{ var udAPI *api.API var server *fasthttp.Server -var badgeCache *cache.Cache +var badgeCache cache.Cache var badgeGen *badge.Generator var badgeStyle string var redirectURL string @@ -125,6 +128,7 @@ func Run(gomod []byte) { case options.GetB(OPT_VERB_VER): support.Collect(APP, VER). WithDeps(deps.Extract(gomod)). + WithServices(services.Collect("updown-badge-server")). Print() os.Exit(0) case options.GetB(OPT_HELP): @@ -132,15 +136,33 @@ func Run(gomod []byte) { os.Exit(0) } - loadConfig() - validateConfig() - configureRuntime() - registerSignalHandlers() - setupLogger() + err := errutil.Chain( + loadConfig, + validateConfig, + configureRuntime, + setupSignalHandlers, + setupLogger, + ) - log.Aux(strings.Repeat("-", 80)) + if err != nil { + terminal.Error(err) + os.Exit(1) + } + + log.Divider() log.Aux("%s %s starting…", APP, VER) + err = errutil.Chain( + setupCache, + setupGenerator, + setupAPIClient, + ) + + if err != nil { + log.Crit(err.Error()) + os.Exit(1) + } + start() } @@ -159,31 +181,32 @@ func configureUI() { } // loadConfig reads and parses configuration file -func loadConfig() { +func loadConfig() error { err := knf.Global(options.GetS(OPT_CONFIG)) if err != nil { - log.Crit(err.Error()) - os.Exit(1) + return fmt.Errorf("Can't load configuration: %w", err) } + + return nil } // validateConfig validates configuration file values -func validateConfig() { +func validateConfig() error { errs := knf.Validate([]*knf.Validator{ - {UPDOWN_API_KEY, knfv.Empty, nil}, - {SERVER_PORT, knfv.Empty, nil}, + {UPDOWN_API_KEY, knfv.Set, nil}, + {SERVER_PORT, knfv.Set, nil}, {MAIN_MAX_PROCS, knfv.TypeNum, nil}, {CACHE_PERIOD, knfv.TypeNum, nil}, {SERVER_PORT, knfv.TypeNum, nil}, {BADGE_FONT, knff.Perms, "FRS"}, - {BADGE_STYLE, knfv.NotContains, []string{ + {BADGE_STYLE, knfv.SetToAny, []string{ STYLE_PLASTIC, STYLE_FLAT, STYLE_FLAT_SQUARE, }}, - {UPDOWN_API_KEY, knfv.NotLen, 23}, + {UPDOWN_API_KEY, knfv.LenEquals, 23}, {UPDOWN_API_KEY, knfr.Regexp, "^ro-[0-9A-Za-z]{20}$"}, {MAIN_MAX_PROCS, knfv.Less, MIN_PROCS}, @@ -201,57 +224,75 @@ func validateConfig() { {LOG_DIR, knff.Perms, "DW"}, {LOG_DIR, knff.Perms, "DX"}, - {LOG_LEVEL, knfv.NotContains, []string{ + {LOG_LEVEL, knfv.SetToAnyIgnoreCase, []string{ "debug", "info", "warn", "error", "crit", }}, }) if len(errs) != 0 { - for _, err := range errs { - log.Crit(err.Error()) - } - - os.Exit(1) + return errs[0] } + + return nil } // configureRuntime configures runtime -func configureRuntime() { +func configureRuntime() error { if !knf.HasProp(MAIN_MAX_PROCS) { - return + return nil } runtime.GOMAXPROCS(knf.GetI(MAIN_MAX_PROCS)) + + return nil } -// registerSignalHandlers registers signal handlers -func registerSignalHandlers() { +// setupSignalHandlers registers signal handlers +func setupSignalHandlers() error { signal.Handlers{ signal.TERM: termSignalHandler, signal.INT: intSignalHandler, signal.HUP: hupSignalHandler, }.TrackAsync() + + return nil } // setupLogger configures logger subsystems -func setupLogger() { +func setupLogger() error { err := log.Set(knf.GetS(LOG_FILE), knf.GetM(LOG_MODE, 0644)) if err != nil { - log.Crit(err.Error()) - os.Exit(1) + return fmt.Errorf("Can't setup logger: %w", err) } err = log.MinLevel(knf.GetS(LOG_LEVEL)) if err != nil { - log.Crit(err.Error()) - os.Exit(1) + return fmt.Errorf("Can't setup logger: %w", err) } + + return nil } -// start configures and starts all subsystems -func start() { +// setupCache configures in-memory cache +func setupCache() error { + var err error + + badgeCache, err = memory.New(memory.Config{ + DefaultExpiration: knf.GetD(CACHE_PERIOD, knf.Second), + CleanupInterval: 15 * time.Second, + }) + + if err != nil { + return fmt.Errorf("Can't configure in-memory cache: %w", err) + } + + return nil +} + +// setupGenerator configurates badge generator +func setupGenerator() error { var err error badgeStyle = knf.GetS(BADGE_STYLE, "flat") @@ -260,21 +301,32 @@ func start() { badgeGen, err = badge.NewGenerator(knf.GetS(BADGE_FONT), 11) if err != nil { - log.Crit("Can't load font for badges: %v", err) - shutdown(1) + return fmt.Errorf("Can't create badge generator: %w", err) } + return nil +} + +// setupAPIClient configures updown.io API client +func setupAPIClient() error { udAPI = api.NewClient(knf.GetS(UPDOWN_API_KEY)) udAPI.SetUserAgent(APP, VER) - badgeCache = cache.New(knf.GetD(CACHE_PERIOD, knf.Second), time.Minute) + return nil +} - err = startHTTPServer(knf.GetS(SERVER_IP), knf.GetS(SERVER_PORT)) +// start configures and starts all subsystems +func start() error { + err := startHTTPServer( + knf.GetS(SERVER_IP), + knf.GetS(SERVER_PORT), + ) if err != nil { - log.Crit("Can't start HTTP server: %v", err) - shutdown(1) + return fmt.Errorf("Can't start HTTP server: %w", err) } + + return nil } // intSignalHandler is INT signal handler @@ -306,6 +358,7 @@ func shutdown(code int) { } } + log.Flush() os.Exit(code) } diff --git a/daemon/server.go b/daemon/server.go index a316df3..039333b 100644 --- a/daemon/server.go +++ b/daemon/server.go @@ -11,11 +11,11 @@ import ( "fmt" "strings" - "github.com/essentialkaos/ek/v12/color" - "github.com/essentialkaos/ek/v12/easing" - "github.com/essentialkaos/ek/v12/log" - "github.com/essentialkaos/ek/v12/mathutil" - "github.com/essentialkaos/ek/v12/strutil" + "github.com/essentialkaos/ek/v13/color" + "github.com/essentialkaos/ek/v13/easing" + "github.com/essentialkaos/ek/v13/log" + "github.com/essentialkaos/ek/v13/mathutil" + "github.com/essentialkaos/ek/v13/strutil" "github.com/essentialkaos/go-badge" diff --git a/go.mod b/go.mod index 0aa1215..912c3e0 100644 --- a/go.mod +++ b/go.mod @@ -1,19 +1,19 @@ module github.com/essentialkaos/updown-badge-server -go 1.18 +go 1.22.8 require ( - github.com/essentialkaos/ek/v12 v12.130.0 + github.com/essentialkaos/ek/v13 v13.6.0 github.com/essentialkaos/go-badge v1.4.1 github.com/valyala/fasthttp v1.56.0 ) require ( - github.com/andybalholm/brotli v1.1.0 // indirect - github.com/essentialkaos/depsy v1.3.0 // indirect + github.com/andybalholm/brotli v1.1.1 // indirect + github.com/essentialkaos/depsy v1.3.1 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect - github.com/klauspost/compress v1.17.9 // indirect + github.com/klauspost/compress v1.17.10 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - golang.org/x/image v0.18.0 // indirect - golang.org/x/sys v0.25.0 // indirect + golang.org/x/image v0.21.0 // indirect + golang.org/x/sys v0.26.0 // indirect ) diff --git a/go.sum b/go.sum index 115e6ca..2f902d0 100644 --- a/go.sum +++ b/go.sum @@ -1,24 +1,30 @@ -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/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk= -github.com/essentialkaos/depsy v1.3.0 h1:CN7bRgBU2jGTHSkg/Sh38eDUn7cvmaTp2sxFt2HpFeU= -github.com/essentialkaos/depsy v1.3.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8= -github.com/essentialkaos/ek/v12 v12.130.0 h1:JhcjGZaBIEFfyNReHAHeTgldNv65ruHa9LR7YIVgmn0= -github.com/essentialkaos/ek/v12 v12.130.0/go.mod h1:yGof+LJIkWvQtGINs+2AGv/yeqcfj/brpQ9eYnzMzKs= +github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0= +github.com/essentialkaos/depsy v1.3.1 h1:00k9QcMsdPM4IzDaEFHsTHBD/zoM0oxtB5+dMUwbQa8= +github.com/essentialkaos/depsy v1.3.1/go.mod h1:B5+7Jhv2a2RacOAxIKU2OeJp9QfZjwIpEEPI5X7auWM= +github.com/essentialkaos/ek/v13 v13.6.0 h1:8qb5R/dl5LEov0OfQXBwq3MSbHJjHSY62IKZG4lfy9M= +github.com/essentialkaos/ek/v13 v13.6.0/go.mod h1:S5mCn90ernaPiLzwzV84XN5Sx+Qcdxed6mFPSq8EBDs= github.com/essentialkaos/go-badge v1.4.1 h1:ZtPY1VzukuK51ryYnOrh8vIUBcSdsNJRL8GG+5QxdFg= github.com/essentialkaos/go-badge v1.4.1/go.mod h1:CwkvOc0vEK7OnTPNsl9Jx2nxzPakzZl9/ak6GvRFyQA= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= -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.10 h1:oXAz+Vh0PMUvJczoi+flxpnBEPxoER1IaAnU/NMPtT0= +github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= +github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= 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.56.0 h1:bEZdJev/6LCBlpdORfrLu/WOZXXxvrUQSiyniuaoW8U= github.com/valyala/fasthttp v1.56.0/go.mod h1:sReBt3XZVnudxuLOx4J/fMrJVorWRiWY2koQKgABiVI= -golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ= -golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E= -golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= -golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +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/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s= +golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78= +golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= +golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=