Skip to content

Environment variable

rk-boot can distinguish environments with environment variable of DOMAIN

Overview#

There can be multiple environments in real life, and we hope to use different config files in different environments.

rk-boot support multiple ConfigEntry with same name where DOMAIN is used to distinguish environments.

Concept#

How rk-boot choose entries?

rk-boot will use environment variable of DOMAIN to distinguish environment

Quick start#

1.Install#

$ go get github.com/rookie-ninja/rk-boot/v2
$ go get github.com/rookie-ninja/rk-grpc/v2

2.Create config files#

config/test.yaml

---
value: test

config/prod.yaml

---
value: prod

config/default.yaml

---
value: default

3.Create boot.yaml#

config:
  - name: my-config
    domain: "test"
    path: "config/test.yaml"
  - name: my-config
    domain: "prod"
    path: "config/prod.yaml"
  - name: my-config
    domain: "*"
    path: "config/default.yaml"
grpc:
  - name: greeter
    port: 8080
#   gwPort: 8081                  # Optional, default: gateway port will be the same as grpc port if not provided
    enabled: true

4.ENV:DOMAIN=test#

package main

import (
  "context"
  "fmt"
  "github.com/rookie-ninja/rk-boot/v2"
  "github.com/rookie-ninja/rk-entry/v2/entry"
  "os"
)

func main() {
  os.Setenv("DOMAIN", "test")

  // Create a new boot instance.
  boot := rkboot.NewBoot()

  fmt.Println(rkentry.GlobalAppCtx.GetConfigEntry("my-config").GetString("value"))

  // Bootstrap
  boot.Bootstrap(context.TODO())

  boot.WaitForShutdownSig(context.TODO())
}

Output

test

5.No ENV#

package main

import (
  "context"
  "fmt"
  "github.com/rookie-ninja/rk-boot/v2"
  "github.com/rookie-ninja/rk-entry/v2/entry"
  "os"
)

func main() {
  // Create a new boot instance.
  boot := rkboot.NewBoot()

  fmt.Println(rkentry.GlobalAppCtx.GetConfigEntry("my-config").GetString("value"))

  // Bootstrap
  boot.Bootstrap(context.TODO())

  boot.WaitForShutdownSig(context.TODO())
}

Output

default

Cheers#