11package config
22
33import (
4+ "github.com/caarlos0/env/v11"
5+ "github.com/joho/godotenv"
46 "gopkg.in/yaml.v3"
57 "log"
68 "os"
@@ -11,64 +13,80 @@ var cf *Config
1113
1214type Config struct {
1315 General struct {
14- BotToken string `yaml:"bot_token"`
15- Language string `yaml:"language"`
16- WorkerNum int `yaml:"worker_num"`
17- DownloadWorkerNum int `yaml:"download_worker_num"`
18- AdminUID int64 `yaml:"admin_uid"`
19- UserDailyLimit int `yaml:"user_daily_limit"`
20- ProcessWaitQueueMaxSize int `yaml:"process_wait_queue_max_size"`
21- ProcessTimeout int `yaml:"process_timeout"`
22- SupportTGSFile bool `yaml:"support_tgs_file"`
23- MaxAmountPerReq int `yaml:"max_amount_per_req"`
24- } `yaml:"general"`
16+ BotToken string `yaml:"bot_token" env:"BOT_TOKEN,required"`
17+ Language string `yaml:"language" env:"LANGUAGE" envDefault:"zh-hans"`
18+ WorkerNum int `yaml:"worker_num" env:"WORKER_NUM" envDefault:"2"`
19+ DownloadWorkerNum int `yaml:"download_worker_num" env:"DOWNLOAD_WORKER_NUM" envDefault:"3"`
20+ AdminUID int64 `yaml:"admin_uid" env:"ADMIN_UID" envDefault:"0"`
21+ UserDailyLimit int `yaml:"user_daily_limit" env:"USER_DAILY_LIMIT" envDefault:"10"`
22+ ProcessWaitQueueMaxSize int `yaml:"process_wait_queue_max_size" env:"PROCESS_WAIT_QUEUE_MAX_SIZE" envDefault:"50"`
23+ ProcessTimeout int `yaml:"process_timeout" env:"PROCESS_TIMEOUT" envDefault:"60"`
24+ SupportTGSFile bool `yaml:"support_tgs_file" env:"SUPPORT_TGS_FILE" envDefault:"false"`
25+ MaxAmountPerReq int `yaml:"max_amount_per_req" env:"MAX_AMOUNT_PER_REQ" envDefault:"100"`
26+ } `yaml:"general" envPrefix:"GENERAL_"`
27+
2528 Community struct {
26- Enable bool `yaml:"enable"`
27- ForceChannelSub bool `yaml:"force_channel_sub"`
28- RewardOnSub bool `yaml:"reward_on_sub"`
29- Channel struct {
30- Username string `yaml:"username"`
31- } `yaml:"channel"`
29+ Enable bool `yaml:"enable" env:"ENABLE" envDefault:"true"`
30+ ForceChannelSub bool `yaml:"force_channel_sub" env:"FORCE_CHANNEL_SUB" envDefault:"true"`
31+ RewardOnSub bool `yaml:"reward_on_sub" env:"REWARD_ON_SUB" envDefault:"true"`
32+
33+ Channel struct {
34+ Username string `yaml:"username" env:"USERNAME"`
35+ } `yaml:"channel" envPrefix:"CHANNEL_"`
36+
3237 Reward struct {
33- ExtraDownloadTimes int `yaml:"extra_download_times"`
34- } `yaml:"reward"`
35- } `yaml:"community"`
38+ ExtraDownloadTimes int `yaml:"extra_download_times" env:"EXTRA_DOWNLOAD_TIMES" envDefault:"3"`
39+ } `yaml:"reward" envPrefix:"REWARD_"`
40+ } `yaml:"community" envPrefix:"COMMUNITY_"`
41+
3642 Cache struct {
37- Enabled bool `yaml:"enabled"`
38- StorageDir string `yaml:"storage_dir"`
39- MaxDiskUsage int `yaml:"max_disk_usage"`
40- CacheExpire int `yaml:"cache_expire"`
41- CacheCleanInterval int `yaml:"cache_clean_interval"`
42- } `json:"cache"`
43+ Enabled bool `yaml:"enabled" env:"ENABLED" envDefault:"false"`
44+ StorageDir string `yaml:"storage_dir" env:"STORAGE_DIR" envDefault:"./storage/cache"`
45+ MaxDiskUsage int `yaml:"max_disk_usage" env:"MAX_DISK_USAGE" envDefault:"1024"`
46+ CacheExpire int `yaml:"cache_expire" env:"CACHE_EXPIRE" envDefault:"86400"`
47+ CacheCleanInterval int `yaml:"cache_clean_interval" env:"CACHE_CLEAN_INTERVAL" envDefault:"1800"`
48+ } `yaml:"cache" envPrefix:"CACHE_"`
49+
4350 Logger struct {
44- Report bool `yaml:"report"`
45- ReportUrl string `yaml:"report_url"`
46- ReportQueryKey string `yaml:"report_query_key"`
47- } `yaml:"logger"`
51+ Report bool `yaml:"report" env:"REPORT" envDefault:"false"`
52+ ReportUrl string `yaml:"report_url" env:"REPORT_URL"` // 若遵循 GoLint 可改名为 ReportURL
53+ ReportQueryKey string `yaml:"report_query_key" env:"REPORT_QUERY_KEY"`
54+ } `yaml:"logger" envPrefix:"LOGGER_"`
55+
4856 Redis struct {
49- Server string `yaml:"server"`
50- Port string `yaml:"port"`
51- TLS bool `yaml:"tls"`
52- Password string `yaml:"password"`
53- DB int `yaml:"db"`
54- } `yaml:"redis"`
57+ Server string `yaml:"server" env:"SERVER" envDefault:"localhost" `
58+ Port string `yaml:"port" env:"PORT" envDefault:"6379" `
59+ TLS bool `yaml:"tls" env:"TLS" envDefault:"false" `
60+ Password string `yaml:"password" env:"PASSWORD" `
61+ DB int `yaml:"db" env:"DB" envDefault:"0" `
62+ } `yaml:"redis" envPrefix:"REDIS_" `
5563}
5664
5765func Init () {
58- _ , err := os .Stat ("./config.yaml" )
59- if err != nil && os .IsNotExist (err ) {
60- log .Fatalln ("config.yaml not exist!!" )
61- }
62-
63- data , err := os .ReadFile ("./config.yaml" )
64- if err != nil {
65- log .Fatalln ("failed to load config.yaml" , err )
66- }
66+ if ! isExist ("./config.yaml" ) {
67+ if ! isExist ("./.env" ) {
68+ log .Fatalln ("config.yaml and .env not exist!!" )
69+ }
70+ //env
71+ log .Println ("Loading configuration from: .env" )
72+ _ = godotenv .Load ()
73+ cf = new (Config )
74+ if err := env .Parse (cf ); err != nil {
75+ log .Fatalln ("failed to parse .env" , err )
76+ }
77+ } else {
78+ //yaml
79+ log .Println ("Loading configuration from: config.yaml" )
80+ data , err := os .ReadFile ("./config.yaml" )
81+ if err != nil {
82+ log .Fatalln ("failed to load config.yaml" , err )
83+ }
6784
68- cf = new (Config )
69- err = yaml .Unmarshal (data , cf )
70- if err != nil {
71- log .Fatalln ("failed to parse config.yaml" , err )
85+ cf = new (Config )
86+ err = yaml .Unmarshal (data , cf )
87+ if err != nil {
88+ log .Fatalln ("failed to parse config.yaml" , err )
89+ }
7290 }
7391
7492 //validate config
@@ -93,3 +111,11 @@ func Init() {
93111func Get () * Config {
94112 return cf
95113}
114+
115+ func isExist (path string ) bool {
116+ _ , err := os .Stat (path )
117+ if err != nil && os .IsNotExist (err ) {
118+ return false
119+ }
120+ return true
121+ }
0 commit comments