Skip to content

Commit

Permalink
Add code highlighting in README (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandregv authored Apr 26, 2024
1 parent 81c8ce6 commit e1d12ec
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,60 +41,60 @@ but may needs multiple Get(s) in Viper and Koanf.
Somewhere, early in an application's life, it will make a decision about which
configuration source(s) (implementation) it actually wants to use. Something like:

```
//go:embed config
var config embed.FS
func main() {
var config konf.Config
// Load configuration from embed file system.
if err := config.Load(fs.New(config, "config/config.json")); err != nil {
// Handle error here.
}
// Load configuration from environment variables.
if err := config.Load(env.New(env.WithPrefix("server"))); err != nil {
// Handle error here.
}
// Watch the changes of configuration.
go func() {
if err := config.Watch(ctx); err != nil {
// Handle error here.
}
}
konf.SetDefault(config)
// ... other setup code ...
```go
//go:embed config
var config embed.FS

func main() {
var config konf.Config

// Load configuration from embed file system.
if err := config.Load(fs.New(config, "config/config.json")); err != nil {
// Handle error here.
}
// Load configuration from environment variables.
if err := config.Load(env.New(env.WithPrefix("server"))); err != nil {
// Handle error here.
}

// Watch the changes of configuration.
go func() {
if err := config.Watch(ctx); err != nil {
// Handle error here.
}
}

konf.SetDefault(config)

// ... other setup code ...
}
```

Outside of this early setup, no other packages need to know about the choice of
configuration source(s). They read configuration in terms of functions in package `konf`:

```
func (app *appObject) Run() {
// Server configuration with default values.
serverConfig := struct {
Host string
Port int
}{
Host: "localhost",
Port: "8080",
}
// Read the server configuration.
if err := konf.Unmarshal("server", &serverConfig); err != nil {
// Handle error here.
}
// Register callbacks while server configuration changes.
konf.OnChange(func() {
// Reconfig the application object.
}, "server")
// ... use cfg in app code ...
```go
func (app *appObject) Run() {
// Server configuration with default values.
serverConfig := struct {
Host string
Port int
}{
Host: "localhost",
Port: "8080",
}
// Read the server configuration.
if err := konf.Unmarshal("server", &serverConfig); err != nil {
// Handle error here.
}

// Register callbacks while server configuration changes.
konf.OnChange(func() {
// Reconfig the application object.
}, "server")

// ... use cfg in app code ...
}
```

## Design
Expand All @@ -116,14 +116,14 @@ The providers for loading configuration from clouds periodically poll the config
It also supports watching the changes of configuration using corresponding notifier.
For example, the `sns` notifier notifies the changes of `appconfig` and `s3` provider:

```
notifier := sns.NewNotifier("konf-test")
notifier.Register(s3Loader, appConfigLoader)
go func() {
if err := notifier.Start(ctx); err != nil {
// handle error
}
}()
```go
notifier := sns.NewNotifier("konf-test")
notifier.Register(s3Loader, appConfigLoader)
go func() {
if err := notifier.Start(ctx); err != nil {
// handle error
}
}()
```

## Understand the configuration
Expand Down

0 comments on commit e1d12ec

Please sign in to comment.