config json转yaml

This commit is contained in:
xing 2023-06-01 15:06:57 +08:00
parent 23c9cc1596
commit 7431823ac7
5 changed files with 110 additions and 21 deletions

5
.gitignore vendored
View File

@ -1,5 +1,6 @@
.idea
wp-go.iml
config.yaml
/wp-go.iml
/config.yaml
err.log
/plugins/
/config.json

View File

@ -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)

View File

@ -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 {

82
config.example.json Normal file
View File

@ -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": "<a><b><blockquote><br><cite><code><dd><del><div><dl><dt><em><h1><h2><h3><h4><h5><h6><i><img><li><ol><p><pre><span><strong><ul>",
"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": [
"",
""
]
}

View File

@ -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: ""