Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 9, 2024
1 parent 56c8f18 commit 54a5cbc
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 83 deletions.
8 changes: 6 additions & 2 deletions common/updown-badge-server.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -100,6 +100,10 @@ exit 0
################################################################################

%changelog
* Thu Oct 10 2024 Anton Novojilov <[email protected]> - 1.4.0-0
- Code refactoring
- Dependencies update

* Mon Jun 24 2024 Anton Novojilov <[email protected]> - 1.3.2-0
- Code refactoring
- Dependencies update
Expand Down
165 changes: 109 additions & 56 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -125,22 +128,41 @@ 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):
genUsage().Print()
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()
}

Expand All @@ -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},
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -306,6 +358,7 @@ func shutdown(code int) {
}
}

log.Flush()
os.Exit(code)
}

Expand Down
10 changes: 5 additions & 5 deletions daemon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
Loading

0 comments on commit 54a5cbc

Please sign in to comment.