telegram-to-discord/config/config.go

84 lines
1.5 KiB
Go
Raw Permalink Normal View History

2025-03-23 15:35:24 +03:00
package config
import (
"github.com/disgoorg/snowflake/v2"
log "github.com/sirupsen/logrus"
"os"
"strconv"
)
type Config struct {
2026-03-05 16:20:47 +03:00
AppConf AppConfig
TgConf TelegramConfig
DsConf DiscordConfig
HttpConf HttpConfig
2025-03-23 15:35:24 +03:00
}
type AppConfig struct {
LogLvl string
}
type TelegramConfig struct {
Token string
ChatID int64
}
type DiscordConfig struct {
Token string
GuildID snowflake.ID
ChannelID snowflake.ID
}
2026-03-05 16:20:47 +03:00
type HttpConfig struct {
Host string
Port string
GinMode string
}
// prod config
2025-03-23 15:35:24 +03:00
func NewConfig() *Config {
return &Config{
AppConfig{
2026-03-05 16:20:47 +03:00
LogLvl: getEnv("APP_LOG_LEVEL", "debug"),
2025-03-23 15:35:24 +03:00
},
TelegramConfig{
Token: getEnv("TELEGRAM_TOKEN", ""),
ChatID: convertChatID(getEnv("TELEGRAM_CHANNEL_ID", "")),
},
DiscordConfig{
Token: getEnv("DISCORD_TOKEN", ""),
GuildID: convertID(getEnv("GUILD_ID", "")),
ChannelID: convertID(getEnv("CHANNEL_ID", "")),
},
2026-03-05 16:20:47 +03:00
HttpConfig{
Host: getEnv("HTTP_HOST", "0.0.0.0"),
Port: getEnv("HTTP_PORT", "8080"),
GinMode: getEnv("GIN_MODE", "debug"),
},
2025-03-23 15:35:24 +03:00
}
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func convertID(channelIDStr string) snowflake.ID {
channelIDUint, err := strconv.ParseUint(channelIDStr, 10, 64)
if err != nil {
log.Fatal("Cannot convert channel ID to snowflake ID")
}
return snowflake.ID(channelIDUint)
}
func convertChatID(str string) int64 {
id, err := strconv.ParseInt(str, 10, 64)
if err != nil {
log.Fatal("Cannot convert string to int64")
}
return id
}