able to get product
This commit is contained in:
24
internal/common/config/duration.go
Normal file
24
internal/common/config/duration.go
Normal file
@ -0,0 +1,24 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
|
||||
type Duration time.Duration
|
||||
|
||||
func (d *Duration) UnmarshalJSON(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
*d = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
duration, err := time.ParseDuration(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*d = Duration(duration)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Duration) Std() time.Duration {
|
||||
return time.Duration(d)
|
||||
}
|
||||
@ -1,8 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"git.loyso.art/frx/kurious/internal/common/errors"
|
||||
)
|
||||
|
||||
type LogFormat uint8
|
||||
@ -12,6 +15,19 @@ const (
|
||||
LogFormatJSON
|
||||
)
|
||||
|
||||
func (f *LogFormat) UnmarshalText(data []byte) error {
|
||||
switch format := string(bytes.ToLower(data)); format {
|
||||
case "json":
|
||||
*f = LogFormatJSON
|
||||
case "text":
|
||||
*f = LogFormatText
|
||||
default:
|
||||
return errors.NewValidationError("format", "unsupported value "+format)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type LogLevel uint8
|
||||
|
||||
const (
|
||||
@ -21,12 +37,29 @@ const (
|
||||
LogLevelError
|
||||
)
|
||||
|
||||
type LogConfig struct {
|
||||
Level LogLevel
|
||||
Format LogFormat
|
||||
func (lvl *LogLevel) UnmarshalText(data []byte) error {
|
||||
switch level := string(bytes.ToLower(data)); level {
|
||||
case "debug", "":
|
||||
*lvl = LogLevelDebug
|
||||
case "info":
|
||||
*lvl = LogLevelInfo
|
||||
case "warn":
|
||||
*lvl = LogLevelWarn
|
||||
case "error":
|
||||
*lvl = LogLevelError
|
||||
default:
|
||||
return errors.NewValidationError("level", "unsupported value "+level)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewSLogger(config LogConfig) *slog.Logger {
|
||||
type Log struct {
|
||||
Level LogLevel `json:"level"`
|
||||
Format LogFormat `json:"format"`
|
||||
}
|
||||
|
||||
func NewSLogger(config Log) *slog.Logger {
|
||||
var level slog.Level
|
||||
switch config.Level {
|
||||
case LogLevelDebug:
|
||||
|
||||
63
internal/common/config/ydb.go
Normal file
63
internal/common/config/ydb.go
Normal file
@ -0,0 +1,63 @@
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@ -5,6 +5,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ErrNotFound SimpleError = "not found"
|
||||
ErrNotImplemented SimpleError = "not implemented"
|
||||
ErrUnexpectedStatus SimpleError = "unexpected status"
|
||||
)
|
||||
|
||||
32
internal/common/xlog/cronlogger.go
Normal file
32
internal/common/xlog/cronlogger.go
Normal file
@ -0,0 +1,32 @@
|
||||
package xlog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type cronlogger struct {
|
||||
basectx context.Context
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func WrapSLogger(ctx context.Context, log *slog.Logger) cronlogger {
|
||||
return cronlogger{
|
||||
basectx: ctx,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
func (l cronlogger) Info(msg string, keysAndValues ...any) {
|
||||
attrs := mapKeysAndValues(keysAndValues...)
|
||||
l.log.LogAttrs(l.basectx, slog.LevelInfo, msg, attrs...)
|
||||
}
|
||||
|
||||
func (l cronlogger) Error(err error, msg string, keysAndValues ...any) {
|
||||
attrs := append(mapKeysAndValues(keysAndValues...), slog.Any("err", err))
|
||||
l.log.LogAttrs(l.basectx, slog.LevelError, msg, attrs...)
|
||||
}
|
||||
|
||||
func mapKeysAndValues(keysAndValues ...any) []slog.Attr {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user