Compogo Viper — это готовая интеграция spf13/viper с фреймворком Compogo. Добавляется одной строкой и автоматически настраивает конфигурацию через файлы и переменные окружения.
go get github.com/Compogo/viperpackage main
import (
"github.com/Compogo/compogo"
"github.com/Compogo/dig"
"github.com/Compogo/viper"
"github.com/Compogo/logrus"
"github.com/Compogo/myapp/service"
)
func main() {
app := compogo.NewApp("myapp",
compogo.WithOsSignalCloser(),
dig.WithDig(),
viper.WithViper(), // ← одна строка
logrus.WithLogrus(),
compogo.WithComponents(
service.Component,
),
)
if err := app.Serve(); err != nil {
panic(err)
}
}В любом компоненте можно запросить:
// 1. Как интерфейс configurator.Configurator
type Service struct {
cfg configurator.Configurator
}
// 2. Как *viper.Decorator (с дополнительными методами)
type Service struct {
dec *viper.Decorator
}
// 3. Как чистый *viper.Viper
type Service struct {
v *viper.Viper
}GetString(key string) string
GetBool(key string) bool
GetInt(key string) int
GetInt8(key string) int8
GetInt16(key string) int16
GetInt32(key string) int32
GetInt64(key string) int64
GetUint(key string) uint
GetUint8(key string) uint8
GetUint16(key string) uint16
GetUint32(key string) uint32
GetUint64(key string) uint64
GetFloat32(key string) float32
GetFloat64(key string) float64
GetTime(key string) time.Time
GetDuration(key string) time.Duration
GetIntSlice(key string) []int
GetStringSlice(key string) []string
GetStringMap(key string) map[string]interface{}
GetStringMapString(key string) map[string]string
GetStringMapStringSlice(key string) map[string][]string
GetSizeInBytes(key string) uintapp:
name: myapp
port: 8080
database:
host: localhost
port: 5432
user: postgres