wp-go/app/pkg/config/config.go

156 lines
6.0 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 (
"encoding/json"
"errors"
2022-08-27 13:21:05 +00:00
"fmt"
"github.com/fthvgb1/wp-go/safety"
2022-08-27 13:21:05 +00:00
"gopkg.in/yaml.v2"
"io"
"net/http"
2022-11-05 07:07:08 +00:00
"os"
"path/filepath"
"strings"
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-04-07 14:59:07 +00:00
Ssl Ssl `yaml:"ssl" json:"ssl"`
Mysql Mysql `yaml:"mysql" json:"mysql"`
Mail Mail `yaml:"mail" json:"mail"`
CacheTime CacheTime `yaml:"cacheTime" json:"cacheTime"`
2023-05-04 11:46:06 +00:00
PluginPath string `yaml:"pluginPath" json:"pluginPath"`
ExternScript []string `json:"externScript" yaml:"externScript"`
2023-04-07 14:59:07 +00:00
DigestWordCount int `yaml:"digestWordCount" json:"digestWordCount,omitempty"`
2023-04-18 13:05:37 +00:00
DigestAllowTag string `yaml:"digestAllowTag" json:"digestAllowTag"`
2023-04-07 14:59:07 +00:00
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum" json:"maxRequestSleepNum,omitempty"`
MaxRequestNum int64 `yaml:"maxRequestNum" json:"maxRequestNum,omitempty"`
SingleIpSearchNum int64 `yaml:"singleIpSearchNum" json:"singleIpSearchNum,omitempty"`
Gzip bool `yaml:"gzip" json:"gzip,omitempty"`
PostCommentUrl string `yaml:"postCommentUrl" json:"postCommentUrl,omitempty"`
TrustIps []string `yaml:"trustIps" json:"trustIps,omitempty"`
TrustServerNames []string `yaml:"trustServerNames" json:"trustServerNames,omitempty"`
Theme string `yaml:"theme" json:"theme,omitempty"`
PostOrder string `yaml:"postOrder" json:"postOrder,omitempty"`
UploadDir string `yaml:"uploadDir" json:"uploadDir,omitempty"`
Pprof string `yaml:"pprof" json:"pprof,omitempty"`
ListPagePlugins []string `yaml:"listPagePlugins" json:"listPagePlugins,omitempty"`
PaginationStep int `yaml:"paginationStep" json:"paginationStep,omitempty"`
ShowQuerySql bool `yaml:"showQuerySql" json:"showQuerySql,omitempty"`
Plugins []string `yaml:"plugins" json:"plugins,omitempty"`
LogOutput string `yaml:"logOutput" json:"logOutput,omitempty"`
WpDir string `yaml:"wpDir" json:"wpDir"`
2023-02-05 13:06:04 +00:00
}
type CacheTime struct {
2023-04-07 14:59:07 +00:00
CacheControl time.Duration `yaml:"cacheControl" json:"cacheControl,omitempty"`
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime" json:"recentPostCacheTime,omitempty"`
CategoryCacheTime time.Duration `yaml:"categoryCacheTime" json:"categoryCacheTime,omitempty"`
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime" json:"archiveCacheTime,omitempty"`
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime" json:"contextPostCacheTime,omitempty"`
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime" json:"recentCommentsCacheTime,omitempty"`
DigestCacheTime time.Duration `yaml:"digestCacheTime" json:"digestCacheTime,omitempty"`
PostListCacheTime time.Duration `yaml:"postListCacheTime" json:"postListCacheTime,omitempty"`
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime" json:"searchPostCacheTime,omitempty"`
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime" json:"monthPostCacheTime,omitempty"`
PostDataCacheTime time.Duration `yaml:"postDataCacheTime" json:"postDataCacheTime,omitempty"`
PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime" json:"postCommentsCacheTime,omitempty"`
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime" json:"crontabClearCacheTime,omitempty"`
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime" json:"maxPostIdCacheTime,omitempty"`
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime" json:"userInfoCacheTime,omitempty"`
CommentsCacheTime time.Duration `yaml:"commentsCacheTime" json:"commentsCacheTime,omitempty"`
SleepTime []time.Duration `yaml:"sleepTime" json:"sleepTime,omitempty"`
2022-08-27 13:21:05 +00:00
}
2023-01-28 15:12:46 +00:00
type Ssl struct {
2023-04-07 14:59:07 +00:00
Cert string `yaml:"cert" json:"cert,omitempty"`
Key string `yaml:"key" json:"key,omitempty"`
2023-01-28 15:12:46 +00:00
}
2022-10-17 12:04:29 +00:00
type Mail struct {
2023-04-07 14:59:07 +00:00
User string `yaml:"user" json:"user,omitempty"`
Alias string `yaml:"alias" json:"alias,omitempty"`
Pass string `yaml:"pass" json:"pass,omitempty"`
Host string `yaml:"host" json:"host,omitempty"`
Port int `yaml:"port" json:"port,omitempty"`
Ssl bool `yaml:"ssl" json:"ssl,omitempty"`
2022-10-17 12:04:29 +00:00
}
2022-08-27 13:21:05 +00:00
type Mysql struct {
2023-04-07 14:59:07 +00:00
Dsn Dsn `yaml:"dsn" json:"dsn"`
Pool Pool `yaml:"pool" json:"pool"`
2022-08-27 13:21:05 +00:00
}
2022-11-05 07:07:08 +00:00
func InitConfig(conf string) error {
if conf == "" {
conf = "config.yaml"
}
var file []byte
var err error
if strings.Contains(conf, "http") {
get, err := http.Get(conf)
if err != nil {
return err
}
file, err = io.ReadAll(get.Body)
} else {
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
switch strings.ToLower(filepath.Ext(conf)) {
case ".yaml":
err = yaml.Unmarshal(file, &c)
case ".json":
err = json.Unmarshal(file, &c)
default:
err = yaml.Unmarshal(file, &c)
2023-05-31 15:12:16 +00:00
if err == nil {
break
}
err = json.Unmarshal(file, &c)
if err == nil {
break
}
return errors.New("invalid suffix config file")
}
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 {
2023-04-07 14:59:07 +00:00
Host string `yaml:"host" json:"host,omitempty"`
Port string `yaml:"port" json:"port,omitempty"`
Db string `yaml:"db" json:"db,omitempty"`
User string `yaml:"user" json:"user,omitempty"`
Password string `yaml:"password" json:"password,omitempty"`
Charset string `yaml:"charset" json:"charset,omitempty"`
2022-08-27 13:21:05 +00:00
}
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 {
2023-04-07 14:59:07 +00:00
ConnMaxIdleTime time.Duration `yaml:"connMaxIdleTime" json:"connMaxIdleTime,omitempty"`
MaxOpenConn int `yaml:"maxOpenConn" json:"maxOpenConn,omitempty"`
MaxIdleConn int `yaml:"maxIdleConn" json:"maxIdleConn,omitempty"`
ConnMaxLifetime time.Duration `yaml:"connMaxLifetime" json:"connMaxLifetime,omitempty"`
2022-08-27 13:21:05 +00:00
}