able to get product

This commit is contained in:
Gitea
2023-11-30 00:39:51 +03:00
parent 606b94e35b
commit 414dc87091
19 changed files with 2204 additions and 77 deletions

View 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)
}

View File

@ -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:

View 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
}