wp-go/internal/pkg/config/config.go

104 lines
3.2 KiB
Go
Raw Normal View History

2022-11-04 02:38:59 +00:00
package config
2022-08-27 13:21:05 +00:00
import (
"fmt"
"github.com/fthvgb1/wp-go/safety"
2022-08-27 13:21:05 +00:00
"gopkg.in/yaml.v2"
2022-11-05 07:07:08 +00:00
"os"
2022-08-27 13:21:05 +00:00
"time"
)
2022-11-15 08:36:21 +00:00
var Conf safety.Var[Config]
2022-08-27 13:21:05 +00:00
type Config struct {
2023-01-28 15:12:46 +00:00
Ssl Ssl `yaml:"ssl"`
2022-09-29 09:23:02 +00:00
Mysql Mysql `yaml:"mysql"`
2022-10-17 12:04:29 +00:00
Mail Mail `yaml:"mail"`
2022-09-29 09:23:02 +00:00
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"`
2023-01-20 11:58:45 +00:00
Theme string `yaml:"theme"`
2023-01-22 10:01:04 +00:00
PostOrder string `yaml:"postOrder"`
2022-08-27 13:21:05 +00:00
}
2023-01-28 15:12:46 +00:00
type Ssl struct {
Cert string `yaml:"cert"`
Key string `yaml:"key"`
}
2022-10-17 12:04:29 +00:00
type Mail struct {
User string `yaml:"user"`
Alias string `yaml:"alias"`
Pass string `yaml:"pass"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Ssl bool `yaml:"ssl"`
}
2022-08-27 13:21:05 +00:00
type Mysql struct {
Dsn Dsn `yaml:"dsn"`
Pool Pool `yaml:"pool"`
}
2022-11-05 07:07:08 +00:00
func InitConfig(conf string) error {
if conf == "" {
conf = "config.yaml"
}
file, err := os.ReadFile(conf)
2022-08-27 13:21:05 +00:00
if err != nil {
return err
}
2022-11-15 08:36:21 +00:00
var c Config
err = yaml.Unmarshal(file, &c)
2022-08-27 13:21:05 +00:00
if err != nil {
return err
}
2022-11-15 08:36:21 +00:00
Conf.Store(c)
2022-08-27 13:21:05 +00:00
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"`
}
2022-11-15 08:36:21 +00:00
func (m Dsn) GetDsn() string {
2022-08-27 13:21:05 +00:00
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"`
}