130 lines
4.6 KiB
Go
130 lines
4.6 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
ListenAddr string
|
|
PublicBaseURL string
|
|
MySQLDSN string
|
|
RedisAddr string
|
|
RedisPassword string
|
|
RedisDB int
|
|
AutoMigrate bool
|
|
JavaAuthBaseURL string
|
|
JavaDeviceBaseURL string
|
|
JavaAppBaseURL string
|
|
JavaBridgeBaseURL string
|
|
JavaOtherBaseURL string
|
|
JavaWalletBaseURL string
|
|
BaishunPlatformBaseURL string
|
|
BaishunAppID int64
|
|
BaishunAppName string
|
|
BaishunAppChannel string
|
|
BaishunAppKey string
|
|
BaishunGSP int
|
|
BaishunLaunchCodeTTLSeconds int
|
|
BaishunSSTokenTTLSeconds int
|
|
InternalCallbackSecret string
|
|
RegisterRewardStreamKey string
|
|
RegisterRewardConsumerGroup string
|
|
RegisterRewardConsumerName string
|
|
RegisterRewardBatchSize int64
|
|
RegisterRewardBlock time.Duration
|
|
RegisterRewardMinIdle time.Duration
|
|
Timeout time.Duration
|
|
}
|
|
|
|
func Load() Config {
|
|
return Config{
|
|
ListenAddr: getEnv("INVITE_HTTP_ADDR", ":2900"),
|
|
PublicBaseURL: getEnv("INVITE_PUBLIC_BASE_URL", "http://localhost:2900"),
|
|
MySQLDSN: getEnv("INVITE_MYSQL_DSN", "root:root@tcp(127.0.0.1:3306)/chatapp3?charset=utf8mb4&parseTime=True&loc=Local"),
|
|
RedisAddr: getEnv("INVITE_REDIS_ADDR", "127.0.0.1:6379"),
|
|
RedisPassword: getEnv("INVITE_REDIS_PASSWORD", ""),
|
|
RedisDB: getEnvInt("INVITE_REDIS_DB", 0),
|
|
AutoMigrate: getEnvBool("INVITE_AUTO_MIGRATE", false),
|
|
JavaAuthBaseURL: getEnv("INVITE_JAVA_AUTH_BASE_URL", "http://127.0.0.1:2100"),
|
|
JavaDeviceBaseURL: getEnv("INVITE_JAVA_DEVICE_BASE_URL", "http://127.0.0.1:2400"),
|
|
JavaAppBaseURL: getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400"),
|
|
JavaBridgeBaseURL: getEnv("INVITE_JAVA_BRIDGE_BASE_URL", "http://127.0.0.1:2200"),
|
|
JavaOtherBaseURL: getEnv("GAME_JAVA_OTHER_BASE_URL", getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400")),
|
|
JavaWalletBaseURL: getEnv("GAME_JAVA_WALLET_BASE_URL", "http://127.0.0.1:2300"),
|
|
BaishunPlatformBaseURL: getEnv("BAISHUN_PLATFORM_BASE_URL", ""),
|
|
BaishunAppID: getEnvInt64("BAISHUN_APP_ID", 0),
|
|
BaishunAppName: getEnv("BAISHUN_APP_NAME", ""),
|
|
BaishunAppChannel: getEnv("BAISHUN_APP_CHANNEL", ""),
|
|
BaishunAppKey: getEnv("BAISHUN_APP_KEY", ""),
|
|
BaishunGSP: getEnvInt("BAISHUN_GSP", 101),
|
|
BaishunLaunchCodeTTLSeconds: getEnvInt("BAISHUN_LAUNCH_CODE_TTL_SECONDS", 300),
|
|
BaishunSSTokenTTLSeconds: getEnvInt("BAISHUN_SS_TOKEN_TTL_SECONDS", 86400),
|
|
InternalCallbackSecret: getEnv("GAME_INTERNAL_CALLBACK_SECRET", ""),
|
|
RegisterRewardStreamKey: getEnv("REGISTER_REWARD_STREAM_KEY", "activity:register:reward"),
|
|
RegisterRewardConsumerGroup: getEnv("REGISTER_REWARD_CONSUMER_GROUP", "register-reward"),
|
|
RegisterRewardConsumerName: getEnv("REGISTER_REWARD_CONSUMER_NAME", defaultConsumerName("register-reward")),
|
|
RegisterRewardBatchSize: int64(getEnvInt("REGISTER_REWARD_BATCH_SIZE", 10)),
|
|
RegisterRewardBlock: time.Duration(getEnvInt("REGISTER_REWARD_BLOCK_MILLIS", 5000)) * time.Millisecond,
|
|
RegisterRewardMinIdle: time.Duration(getEnvInt("REGISTER_REWARD_MIN_IDLE_MILLIS", 60000)) * time.Millisecond,
|
|
Timeout: time.Duration(getEnvInt("INVITE_HTTP_TIMEOUT_SECONDS", 8)) * time.Second,
|
|
}
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func getEnvInt(key string, fallback int) int {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func getEnvInt64(key string, fallback int64) int64 {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
parsed, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func getEnvBool(key string, fallback bool) bool {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
parsed, err := strconv.ParseBool(value)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func defaultConsumerName(prefix string) string {
|
|
host, err := os.Hostname()
|
|
if err != nil {
|
|
return prefix
|
|
}
|
|
host = strings.TrimSpace(host)
|
|
if host == "" {
|
|
return prefix
|
|
}
|
|
return prefix + "-" + host
|
|
}
|