Skip to content
This repository was archived by the owner on Jul 5, 2025. It is now read-only.

Conversation

@aaronschweig
Copy link
Member

This PR adds the ability to handle configuration in microservices centrally.

It basically extracts the main configuration points for a microservice in a CommonServiceConfig and generates cmd flags for it, which can be overriden from the env.

It also allows for extension via custom configuration for which also flags are generated.

It also sets up a viper instance ready to use for consumption with the configurations.

It can be used e.g.

// config.go
type Config struct {
	// CommonServiceConfig
	FGA struct {
		Target string `mapstructure:"fga-target"`
	} `mapstructure:",squash"`
	RuntimeKubeconfigPath      string `mapstructure:"runtime-kubeconfig-path"`
	APIExportEndpointSliceName string `mapstructure:"api-export-endpoint-slice-name"`
}
// service.go

var cfg config.Config
var v *viper.Viper

func init() {
	v = config.NewConfigFor(&cfg)

	cobra.OnInitialize(initConfig)

	initializerCmd.Flags().AddGoFlagSet(flag.CommandLine)

	v.BindPFlags(initializerCmd.Flags())
}

func initConfig() {
	// Parse environment variables into the Config struct
	if err := v.Unmarshal(&cfg); err != nil {
		log.Fatalf("Unable to decode into struct: %v", err)
	}

	if err := v.ReadInConfig(); err == nil {
		fmt.Println("Using config file:", v.ConfigFileUsed())
	}

	fmt.Printf("Loaded configuration: %+v\n", cfg)
}

@aaronschweig aaronschweig requested a review from a team as a code owner April 2, 2025 12:16
@github-actions github-actions bot added the fix label Apr 2, 2025
Copy link
Member

@nexus49 nexus49 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@github-actions github-actions bot added the chore label Apr 2, 2025
@aaronschweig aaronschweig merged commit 9a518a1 into main Apr 2, 2025
9 checks passed
@aaronschweig aaronschweig deleted the feat/common-service-config branch April 2, 2025 14:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants