Skip to content

Commit

Permalink
Set some default configuration values (#4223)
Browse files Browse the repository at this point in the history
Closes #3946

Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz authored Jan 8, 2025
1 parent b3d8208 commit 451a914
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
23 changes: 23 additions & 0 deletions cmd/hub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/artifacthub/hub/internal/webhook"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

func main() {
Expand All @@ -37,6 +38,7 @@ func main() {
if err != nil {
log.Fatal().Err(err).Msg("configuration setup failed")
}
setCfgDefaults(cfg)
fields := map[string]interface{}{"cmd": "hub"}
if err := util.SetupLogger(cfg, fields); err != nil {
log.Fatal().Err(err).Msg("logger setup failed")
Expand Down Expand Up @@ -157,3 +159,24 @@ func main() {
}
log.Info().Msg("hub server stopped")
}

// setCfgDefaults sets the default values for some configuration options.
func setCfgDefaults(cfg *viper.Viper) {
cfg.SetDefault("server.addr", "0.0.0.0:8000")
cfg.SetDefault("server.allowUserSignUp", true)
cfg.SetDefault("server.baseURL", "http://localhost:8000")
cfg.SetDefault("server.cookie.hashKey", "sample hash key")
cfg.SetDefault("server.csrf.authKey", "sample auth key")
cfg.SetDefault("server.metricsAddr", "0.0.0.0:8001")
cfg.SetDefault("server.shutdownTimeout", 10*time.Second)
cfg.SetDefault("server.webBuildPath", "../../web/build")
cfg.SetDefault("server.widgetBuildPath", "../../widget/build")
cfg.SetDefault("theme.colors.primary", "#417598")
cfg.SetDefault("theme.colors.secondary", "#2D4857")
cfg.SetDefault("theme.images.appleTouchIcon192", "/static/media/logo192_v2.png")
cfg.SetDefault("theme.images.appleTouchIcon512", "/static/media/logo512_v2.png")
cfg.SetDefault("theme.images.openGraphImage", "/static/media/artifactHub_v2.png")
cfg.SetDefault("theme.images.shortcutIcon", "/static/media/logo_v2.png")
cfg.SetDefault("theme.images.websiteLogo", "/static/media/logo/artifacthub-brand-white.svg")
cfg.SetDefault("theme.siteName", "Artifact hub")
}
9 changes: 8 additions & 1 deletion cmd/scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/artifacthub/hub/internal/scanner"
"github.com/artifacthub/hub/internal/util"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

func main() {
Expand All @@ -24,6 +25,7 @@ func main() {
if err != nil {
log.Fatal().Err(err).Msg("configuration setup failed")
}
setCfgDefaults(cfg)
fields := map[string]interface{}{"cmd": "scanner"}
if err := util.SetupLogger(cfg, fields); err != nil {
log.Fatal().Err(err).Msg("logger setup failed")
Expand Down Expand Up @@ -65,7 +67,6 @@ func main() {
if err != nil {
log.Fatal().Err(err).Msg("error getting snapshots to scan")
}
cfg.SetDefault("scanner.concurrency", 1)
limiter := make(chan struct{}, cfg.GetInt("scanner.concurrency"))
var wg sync.WaitGroup
L:
Expand Down Expand Up @@ -105,3 +106,9 @@ L:
ec.Flush()
log.Info().Msg("scanner finished")
}

// setCfgDefaults sets the default values for some configuration options.
func setCfgDefaults(cfg *viper.Viper) {
cfg.SetDefault("scanner.concurrency", 1)
cfg.SetDefault("scanner.trivyURL", "http://localhost:8081")
}
11 changes: 9 additions & 2 deletions cmd/tracker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/artifacthub/hub/internal/tracker"
"github.com/artifacthub/hub/internal/util"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

var (
Expand All @@ -31,6 +32,7 @@ func main() {
if err != nil {
log.Fatal().Err(err).Msg("configuration setup failed")
}
setCfgDefaults(cfg)
fields := map[string]interface{}{"cmd": "tracker"}
if err := util.SetupLogger(cfg, fields); err != nil {
log.Fatal().Err(err).Msg("logger setup failed")
Expand Down Expand Up @@ -92,8 +94,6 @@ func main() {
if err != nil {
log.Fatal().Err(err).Msg("error getting repositories")
}
cfg.SetDefault("tracker.concurrency", 1)
cfg.SetDefault("tracker.repositoryTimeout", 15*time.Minute)
limiter := make(chan struct{}, cfg.GetInt("tracker.concurrency"))
var wg sync.WaitGroup
L:
Expand Down Expand Up @@ -140,3 +140,10 @@ L:
ec.Flush()
log.Info().Msg("tracker finished")
}

// setCfgDefaults sets the default values for some configuration options.
func setCfgDefaults(cfg *viper.Viper) {
cfg.SetDefault("tracker.categoryModelPath", "../../ml/category/model")
cfg.SetDefault("tracker.concurrency", 1)
cfg.SetDefault("tracker.repositoryTimeout", 15*time.Minute)
}
9 changes: 9 additions & 0 deletions internal/util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@ func SetupConfig(cmd string) (*viper.Viper, error) {
cfg.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
cfg.AutomaticEnv()

// Set some defaults
cfg.SetDefault("db.database", "hub")
cfg.SetDefault("db.host", "localhost")
cfg.SetDefault("db.port", "5432")
cfg.SetDefault("db.user", "postgres")
cfg.SetDefault("images.store", "pg")
cfg.SetDefault("log.level", "debug")
cfg.SetDefault("log.pretty", true)

return cfg, nil
}

0 comments on commit 451a914

Please sign in to comment.