Files
aimaren/internal/config/config.go
T
2025-07-26 05:58:59 +00:00

43 lines
1005 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"log"
"strings"
"github.com/spf13/viper"
)
type Config struct {
GCPProjectID string `mapstructure:"gcp_project_id"`
Telegram struct {
Token string `mapstructure:"token"`
ChatIDs []int64 `mapstructure:"chat_ids"`
} `mapstructure:"telegram"`
}
func Load() (Config, error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.SetEnvPrefix("CRAWLER")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
var cfg Config
if err := viper.ReadInConfig(); err != nil {
// If the error is that the file wasn't found, that's okay. Log it and continue.
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Println("️ No 'config.yaml' file found. Relying on environment variables or flags.")
} else {
// For any other error (e.g., malformed YAML), return the error.
return cfg, err
}
}
if err := viper.Unmarshal(&cfg); err != nil {
return cfg, err
}
return cfg, nil
}