83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"github.com/disgoorg/snowflake/v2"
|
|
log "github.com/sirupsen/logrus"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
AppConf AppConfig
|
|
TgConf TelegramConfig
|
|
DsConf DiscordConfig
|
|
HttpConf HttpConfig
|
|
}
|
|
|
|
type AppConfig struct {
|
|
LogLvl string
|
|
}
|
|
|
|
type TelegramConfig struct {
|
|
Token string
|
|
ChatID int64
|
|
}
|
|
|
|
type DiscordConfig struct {
|
|
Token string
|
|
GuildID snowflake.ID
|
|
ChannelID snowflake.ID
|
|
}
|
|
|
|
type HttpConfig struct {
|
|
Host string
|
|
Port string
|
|
GinMode string
|
|
}
|
|
|
|
// prod config
|
|
func NewConfig() *Config {
|
|
return &Config{
|
|
AppConfig{
|
|
LogLvl: getEnv("APP_LOG_LEVEL", "debug"),
|
|
},
|
|
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", "")),
|
|
},
|
|
|
|
HttpConfig{
|
|
Host: getEnv("HTTP_HOST", "0.0.0.0"),
|
|
Port: getEnv("HTTP_PORT", "8080"),
|
|
GinMode: getEnv("GIN_MODE", "debug"),
|
|
},
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|