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

116 lines
3.5 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"
)
2023-02-05 13:06:04 +00:00
var config safety.Var[Config]
func GetConfig() Config {
return config.Load()
}
2022-08-27 13:21:05 +00:00
type Config struct {
2023-02-05 13:06:04 +00:00
Ssl Ssl `yaml:"ssl"`
Mysql Mysql `yaml:"mysql"`
Mail Mail `yaml:"mail"`
CacheTime CacheTime `yaml:"cacheTime"`
DigestWordCount int `yaml:"digestWordCount"`
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum"`
MaxRequestNum int64 `yaml:"maxRequestNum"`
SingleIpSearchNum int64 `yaml:"singleIpSearchNum"`
Gzip bool `yaml:"gzip"`
PostCommentUrl string `yaml:"postCommentUrl"`
TrustIps []string `yaml:"trustIps"`
TrustServerNames []string `yaml:"trustServerNames"`
Theme string `yaml:"theme"`
PostOrder string `yaml:"postOrder"`
UploadDir string `yaml:"uploadDir"`
Pprof string `yaml:"pprof"`
}
type CacheTime struct {
2023-02-05 13:29:09 +00:00
CacheControl time.Duration `yaml:"cacheControl"`
2023-01-31 16:58:42 +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"`
PostListCacheTime time.Duration `yaml:"postListCacheTime"`
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"`
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"`
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"`
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"`
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
ThemeHeaderImagCacheTime time.Duration `yaml:"themeHeaderImagCacheTime"`
2023-02-05 13:06:04 +00:00
SleepTime []time.Duration `yaml:"sleepTime"`
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
}
2023-02-05 13:06:04 +00:00
config.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"`
}