From 7431823ac74f43deedd7ae721f6dccefdd07cdfd Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 1 Jun 2023 15:06:57 +0800 Subject: [PATCH] =?UTF-8?q?config=20json=E8=BD=ACyaml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++- app/mail/mail.go | 2 +- app/pkg/config/config.go | 36 ++++++++++-------- config.example.json | 82 ++++++++++++++++++++++++++++++++++++++++ config.example.yaml | 6 +-- 5 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 config.example.json diff --git a/.gitignore b/.gitignore index 600cf56..a669a91 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea -wp-go.iml -config.yaml +/wp-go.iml +/config.yaml err.log /plugins/ +/config.json \ No newline at end of file diff --git a/app/mail/mail.go b/app/mail/mail.go index 5d7972e..13c9f64 100644 --- a/app/mail/mail.go +++ b/app/mail/mail.go @@ -45,7 +45,7 @@ func SendMail(mailTo []string, subject string, body string, files ...string) err c.Mail.User, c.Mail.Pass, ) - if !c.Mail.Ssl { + if c.Mail.InsecureSkipVerify { d.TLSConfig = &tls.Config{InsecureSkipVerify: true} } err := d.DialAndSend(m) diff --git a/app/pkg/config/config.go b/app/pkg/config/config.go index c4fd23d..13a24e4 100644 --- a/app/pkg/config/config.go +++ b/app/pkg/config/config.go @@ -2,7 +2,6 @@ package config import ( "encoding/json" - "errors" "fmt" "github.com/fthvgb1/wp-go/safety" "gopkg.in/yaml.v2" @@ -74,12 +73,12 @@ type Ssl struct { } type Mail struct { - 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"` + 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"` + InsecureSkipVerify bool `yaml:"insecureSkipVerify" json:"insecureSkipVerify,omitempty"` } type Mysql struct { @@ -110,7 +109,15 @@ func InitConfig(conf string) error { case ".yaml": err = yaml.Unmarshal(file, &c) case ".json": - err = json.Unmarshal(file, &c) + var v map[string]any + err = json.Unmarshal(file, &v) + if err == nil { + marshal, er := yaml.Marshal(v) + if er != nil { + return er + } + err = yaml.Unmarshal(marshal, &c) + } default: err = yaml.Unmarshal(file, &c) if err == nil { @@ -120,7 +127,6 @@ func InitConfig(conf string) error { if err == nil { break } - return errors.New("invalid suffix config file") } if err != nil { @@ -131,12 +137,12 @@ func InitConfig(conf string) error { } type Dsn struct { - 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"` + Host string `yaml:"host" json:"host,omitempty"` + Port json.Number `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"` } func (m Dsn) GetDsn() string { diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..999cdc0 --- /dev/null +++ b/config.example.json @@ -0,0 +1,82 @@ +{ + "mysql": { + "dsn": { + "host": "localhost", + "port": 3306, + "db": "wordpress", + "user": "root", + "password": "root", + "charset": "utf8mb4" + }, + "pool": { + "connMaxIdleTime": 60, + "maxOpenConn": 100, + "maxIdleConn": 10, + "connMaxLifetime": 236 + } + }, + "Mail": { + "user": "xx@163.com", + "alias": "xx", + "pass": null, + "host": "smtp.163.com", + "port": 465, + "insecureSkipVerify": false + }, + "ssl": { + "cert": "", + "key": "" + }, + "cacheTime": { + "cacheControl": "72h", + "recentPostCacheTime": "5m", + "categoryCacheTime": "5m", + "contextPostCacheTime": "10h", + "recentCommentsCacheTime": "5m", + "digestCacheTime": "5m", + "postListCacheTime": "1h", + "searchPostCacheTime": "5m", + "monthPostCacheTime": "1h", + "postDataCacheTime": "1h", + "postCommentsCacheTime": "5m", + "crontabClearCacheTime": "5m", + "maxPostIdCacheTime": "1h", + "userInfoCacheTime": "24h", + "commentsCacheTime": "24h", + "sleepTime": [ + "1s", + "3s" + ] + }, + "digestWordCount": 300, + "digestTag": "

      • ", + "maxRequestSleepNum": 100, + "maxRequestNum": 500, + "singleIpSearchNum": 10, + "logOutput": "err.log", + "gzip": false, + "postCommentUrl": "http://127.0.0.1/wp-comments-post.php", + "trustIps": [], + "paginationStep": 1, + "showQuerySql": false, + "trustServerNames": [ + "xy.test", + "blog.xy.test" + ], + "theme": "twentyfifteen", + "postOrder": "desc", + "wpDir": "/var/www/wordpress", + "uploadDir": "", + "pprof": "/debug/pprof", + "plugins": [ + "enlightjs" + ], + "pluginPath": "./plugins", + "listPagePlugins": [ + "digest" + ], + "externScript": [ + "", + "" + ] +} \ No newline at end of file diff --git a/config.example.yaml b/config.example.yaml index 39c6bc3..2f59b31 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -19,9 +19,9 @@ Mail: user: xx@163.com alias: xx pass: - Host: smtp.163.com - Port: 465 - Ssl: true + host: smtp.163.com + port: 465 + insecureSkipVerify: false ssl: cert: ""