Files
kurious/internal/common/config/ydb.go
2024-01-08 18:31:35 +03:00

65 lines
1.3 KiB
Go

package config
import (
"encoding/json"
"time"
"git.loyso.art/frx/kurious/internal/common/errors"
)
type YCAuth interface {
isYCAuth()
}
type YCAuthCAKeysFile struct{ Path string }
func (YCAuthCAKeysFile) isYCAuth() {}
type YCAuthIAMToken struct{ Token string }
func (YCAuthIAMToken) isYCAuth() {}
type YCAuthNone struct{}
func (YCAuthNone) isYCAuth() {}
type YDB struct {
DSN string
Auth YCAuth
ShutdownDuration time.Duration
DebugYDB bool
}
func (ydb *YDB) UnmarshalJSON(data []byte) error {
type ydbConfig struct {
DSN string `json:"dsn"`
CAKeysFile *string `json:"ca_keys_file_path"`
StaticIAMToken *string `json:"static_iam_token"`
ShutdownDuration Duration `json:"duration"`
}
var imcfg ydbConfig
err := json.Unmarshal(data, &imcfg)
if err != nil {
return err
}
ydb.DSN = imcfg.DSN
ydb.ShutdownDuration = imcfg.ShutdownDuration.Std()
if imcfg.CAKeysFile != nil && imcfg.StaticIAMToken != nil {
return errors.NewValidationError("ca_keys_file_path", "could not be set together with static_iam_token field")
} else if imcfg.CAKeysFile != nil {
ydb.Auth = YCAuthCAKeysFile{
Path: *imcfg.CAKeysFile,
}
} else if imcfg.StaticIAMToken != nil {
ydb.Auth = YCAuthIAMToken{
Token: *imcfg.StaticIAMToken,
}
} else {
ydb.Auth = YCAuthNone{}
}
return nil
}