wp-go/vars/config.go

80 lines
2.6 KiB
Go
Raw Normal View History

2022-08-27 13:21:05 +00:00
package vars
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"time"
)
var Conf Config
type Config struct {
2022-09-29 09:23:02 +00:00
Mysql Mysql `yaml:"mysql"`
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"`
CategoryCacheTime time.Duration `yaml:"categoryCacheTime"`
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"`
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"`
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"`
DigestCacheTime time.Duration `yaml:"digestCacheTime"`
DigestWordCount int `yaml:"digestWordCount"`
PostListCacheTime time.Duration `yaml:"postListCacheTime"`
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
2022-10-08 11:35:05 +00:00
PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"`
2022-09-29 09:23:02 +00:00
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"`
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"`
SleepTime []time.Duration `yaml:"sleepTime"`
MaxRequestNum int64 `yaml:"maxRequestNum"`
2022-10-02 02:58:48 +00:00
SingleIpSearchNum int64 `yaml:"singleIpSearchNum"`
2022-10-04 03:13:14 +00:00
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"`
2022-10-06 13:35:36 +00:00
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"`
2022-10-08 11:35:05 +00:00
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
2022-10-07 15:48:42 +00:00
Gzip bool `yaml:"gzip"`
2022-10-08 13:13:26 +00:00
PostCommentUrl string `yaml:"postCommentUrl"`
2022-10-09 15:22:18 +00:00
TrustIps []string `yaml:"trustIps"`
2022-10-10 13:07:39 +00:00
TrustServerNames []string `yaml:"trustServerNames"`
2022-08-27 13:21:05 +00:00
}
type Mysql struct {
Dsn Dsn `yaml:"dsn"`
Pool Pool `yaml:"pool"`
}
2022-08-28 14:08:16 +00:00
func InitConfig() error {
2022-08-27 13:21:05 +00:00
file, err := ioutil.ReadFile("config.yaml")
if err != nil {
return err
}
err = yaml.Unmarshal(file, &Conf)
if err != nil {
return err
}
return nil
}
type Dsn struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Db string `yaml:"db"`
User string `yaml:"user"`
Password string `yaml:"password"`
Charset string `yaml:"charset"`
}
func (m *Dsn) GetDsn() string {
if m.Charset == "" {
m.Charset = "utf8"
}
t := "%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local"
return fmt.Sprintf(t, m.User, m.Password, m.Host, m.Port, m.Db, m.Charset)
}
type Pool struct {
ConnMaxIdleTime time.Duration `yaml:"connMaxIdleTime"`
MaxOpenConn int `yaml:"maxOpenConn"`
MaxIdleConn int `yaml:"maxIdleConn"`
ConnMaxLifetime time.Duration `yaml:"connMaxLifetime"`
}