From 557f3db4ed28cf1590f51fd998746889544c4b29 Mon Sep 17 00:00:00 2001 From: Yavolte Date: Wed, 18 Jun 2025 18:47:00 +0800 Subject: [PATCH] add default config --- config/config.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..a109843 --- /dev/null +++ b/config/config.go @@ -0,0 +1,53 @@ +package config + +import ( + "os" + "strconv" +) + +type Database struct { + Driver string `json:"driver" yaml:"driver"` + DSN string `json:"dsn" yaml:"dsn"` +} + +type Redis struct { + Addr string `json:"addr" yaml:"addr"` + Password string `json:"password" yaml:"password"` + DB int `json:"db" yaml:"db"` +} + +type Auth struct { + TTL int64 `json:"ttl" yaml:"ttl"` + Secret string `json:"secret" yaml:"secret"` + AllowUrls []string `json:"allow_urls" yaml:"allowUrls"` +} + +type Translate struct { + ClientKey string `json:"client_key" yaml:"clientKey"` + ServerKey string `json:"server_key" yaml:"serverKey"` + ValidateUrl string `json:"validate_url" yaml:"validateUrl"` +} + +type Config struct { + Auth Auth `json:"auth" yaml:"auth"` + Redis Redis `json:"redis" yaml:"redis"` + Translate Translate `json:"translate" yaml:"translate"` + Database Database `json:"database" yaml:"database"` +} + +func (c *Config) FromEnvironment() { + c.Auth.Secret = os.Getenv("JWT_SECRET") + c.Auth.TTL = 7200 + + c.Database.Driver = os.Getenv("DATABASE_DRIVER") + c.Database.DSN = os.Getenv("DATABASE_DSN") + if c.Database.Driver == "" { + c.Database.Driver = "mysql" + } + + c.Redis.Addr = os.Getenv("REDIS_ADDRESS") + c.Redis.Password = os.Getenv("REDIS_PASSWORD") + if n, err := strconv.Atoi(os.Getenv("REDIS_DB")); err == nil { + c.Redis.DB = n + } +}