feat: migrate codes from previous project
This commit is contained in:
46
internal/config/config.go
Normal file
46
internal/config/config.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func LoadConfig() (*Config, error) {
|
||||
v := viper.New()
|
||||
|
||||
v.AutomaticEnv()
|
||||
|
||||
v.SetConfigName("config")
|
||||
v.SetConfigType("yaml")
|
||||
v.AddConfigPath("./config")
|
||||
v.AddConfigPath(".")
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v.SetConfigName(".env")
|
||||
v.SetConfigType("env")
|
||||
|
||||
if err := v.MergeInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, key := range v.AllKeys() {
|
||||
if strings.Contains(key, "_") {
|
||||
nestedKey := strings.Replace(key, "_", ".", 1)
|
||||
|
||||
if !v.IsSet(nestedKey) || v.Get(nestedKey) == "" {
|
||||
v.Set(nestedKey, v.Get(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := v.Unmarshal(&cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
24
internal/config/types.go
Normal file
24
internal/config/types.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
Subscribes []Subscribe `mapstructure:"subscribes"`
|
||||
Feishu Feishu `mapstructure:"feishu"`
|
||||
Silicon Silicon `mapstructure:"silicon"`
|
||||
}
|
||||
|
||||
type Subscribe struct {
|
||||
Name string `mapstructure:"name"`
|
||||
Url string `mapstructure:"url"`
|
||||
Prompt string `mapstructure:"prompt"`
|
||||
Model string `mapstructure:"model"`
|
||||
}
|
||||
|
||||
type Feishu struct {
|
||||
AppID string `mapstructure:"app_id"`
|
||||
AppSecret string `mapstructure:"app_secret"`
|
||||
ChatID string `mapstructure:"chat_id"`
|
||||
}
|
||||
|
||||
type Silicon struct {
|
||||
Token string `mapstructure:"token"`
|
||||
}
|
||||
Reference in New Issue
Block a user