Simplify startup code

This commit is contained in:
2026-01-26 10:28:20 +01:00
parent 6aca448c18
commit b7d4f60358
2 changed files with 28 additions and 75 deletions

View File

@@ -1,25 +0,0 @@
// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
// All rights reserved. This file is part of cc-metric-store.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package main provides the entry point for the ClusterCockpit metric store server.
// This file defines all command-line flags and their default values.
package main
import "flag"
var (
flagGops, flagVersion, flagDev, flagLogDateTime bool
flagConfigFile, flagLogLevel string
)
func cliInit() {
flag.BoolVar(&flagGops, "gops", false, "Listen via github.com/google/gops/agent (for debugging)")
flag.BoolVar(&flagDev, "dev", false, "Enable development component: Swagger UI")
flag.BoolVar(&flagVersion, "version", false, "Show version information and exit")
flag.BoolVar(&flagLogDateTime, "logdate", false, "Set this flag to add date and time to log messages")
flag.StringVar(&flagConfigFile, "config", "./config.json", "Specify alternative path to `config.json`")
flag.StringVar(&flagLogLevel, "loglevel", "warn", "Sets the logging level: `[debug, info, warn (default), err, crit]`")
flag.Parse()
}

View File

@@ -7,6 +7,7 @@ package main
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
@@ -28,46 +29,17 @@ var (
version string version string
) )
var (
flagGops, flagVersion, flagDev, flagLogDateTime bool
flagConfigFile, flagLogLevel string
)
func printVersion() { func printVersion() {
fmt.Printf("Version:\t%s\n", version) fmt.Printf("Version:\t%s\n", version)
fmt.Printf("Git hash:\t%s\n", commit) fmt.Printf("Git hash:\t%s\n", commit)
fmt.Printf("Build time:\t%s\n", date) fmt.Printf("Build time:\t%s\n", date)
} }
func initGops() error {
if !flagGops && !config.Keys.Debug.EnableGops {
return nil
}
if err := agent.Listen(agent.Options{}); err != nil {
return fmt.Errorf("starting gops agent: %w", err)
}
return nil
}
func initConfiguration() error {
ccconf.Init(flagConfigFile)
cfg := ccconf.GetPackageConfig("main")
if cfg == nil {
return fmt.Errorf("main configuration must be present")
}
config.Init(cfg)
return nil
}
func initSubsystems() error {
// Initialize nats client
natsConfig := ccconf.GetPackageConfig("nats")
if err := nats.Init(natsConfig); err != nil {
cclog.Warnf("initializing (optional) nats client: %s", err.Error())
}
nats.Connect()
return nil
}
func runServer(ctx context.Context) error { func runServer(ctx context.Context) error {
var wg sync.WaitGroup var wg sync.WaitGroup
@@ -131,36 +103,42 @@ func runServer(ctx context.Context) error {
} }
func run() error { func run() error {
cliInit() flag.BoolVar(&flagGops, "gops", false, "Listen via github.com/google/gops/agent (for debugging)")
flag.BoolVar(&flagDev, "dev", false, "Enable development component: Swagger UI")
flag.BoolVar(&flagVersion, "version", false, "Show version information and exit")
flag.BoolVar(&flagLogDateTime, "logdate", false, "Set this flag to add date and time to log messages")
flag.StringVar(&flagConfigFile, "config", "./config.json", "Specify alternative path to `config.json`")
flag.StringVar(&flagLogLevel, "loglevel", "warn", "Sets the logging level: `[debug, info, warn (default), err, crit]`")
flag.Parse()
if flagVersion { if flagVersion {
printVersion() printVersion()
return nil return nil
} }
// Initialize logger
cclog.Init(flagLogLevel, flagLogDateTime) cclog.Init(flagLogLevel, flagLogDateTime)
// Initialize gops agent if flagGops || config.Keys.Debug.EnableGops {
if err := initGops(); err != nil { if err := agent.Listen(agent.Options{}); err != nil {
return err return fmt.Errorf("starting gops agent: %w", err)
}
} }
// Initialize subsystems in dependency order: ccconf.Init(flagConfigFile)
// 1. Load configuration from config.json
// 2. Initialize subsystems like nats
// Load configuration cfg := ccconf.GetPackageConfig("main")
if err := initConfiguration(); err != nil { if cfg == nil {
return err return fmt.Errorf("main configuration must be present")
} }
// Initialize subsystems (nats, etc.) config.Init(cfg)
if err := initSubsystems(); err != nil {
return err natsConfig := ccconf.GetPackageConfig("nats")
} if err := nats.Init(natsConfig); err != nil {
cclog.Warnf("initializing (optional) nats client: %s", err.Error())
}
nats.Connect()
// Run server with context
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()