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 .idea
wp-go.iml /wp-go.iml
config.yaml /config.yaml
err.log err.log
/plugins/ /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.User,
c.Mail.Pass, c.Mail.Pass,
) )
if !c.Mail.Ssl { if c.Mail.InsecureSkipVerify {
d.TLSConfig = &tls.Config{InsecureSkipVerify: true} d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
} }
err := d.DialAndSend(m) err := d.DialAndSend(m)

View File

@ -2,7 +2,6 @@ package config
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -79,7 +78,7 @@ type Mail struct {
Pass string `yaml:"pass" json:"pass,omitempty"` Pass string `yaml:"pass" json:"pass,omitempty"`
Host string `yaml:"host" json:"host,omitempty"` Host string `yaml:"host" json:"host,omitempty"`
Port int `yaml:"port" json:"port,omitempty"` Port int `yaml:"port" json:"port,omitempty"`
Ssl bool `yaml:"ssl" json:"ssl,omitempty"` InsecureSkipVerify bool `yaml:"insecureSkipVerify" json:"insecureSkipVerify,omitempty"`
} }
type Mysql struct { type Mysql struct {
@ -110,7 +109,15 @@ func InitConfig(conf string) error {
case ".yaml": case ".yaml":
err = yaml.Unmarshal(file, &c) err = yaml.Unmarshal(file, &c)
case ".json": 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: default:
err = yaml.Unmarshal(file, &c) err = yaml.Unmarshal(file, &c)
if err == nil { if err == nil {
@ -120,7 +127,6 @@ func InitConfig(conf string) error {
if err == nil { if err == nil {
break break
} }
return errors.New("invalid suffix config file")
} }
if err != nil { if err != nil {
@ -132,7 +138,7 @@ func InitConfig(conf string) error {
type Dsn struct { type Dsn struct {
Host string `yaml:"host" json:"host,omitempty"` Host string `yaml:"host" json:"host,omitempty"`
Port string `yaml:"port" json:"port,omitempty"` Port json.Number `yaml:"port" json:"port,omitempty"`
Db string `yaml:"db" json:"db,omitempty"` Db string `yaml:"db" json:"db,omitempty"`
User string `yaml:"user" json:"user,omitempty"` User string `yaml:"user" json:"user,omitempty"`
Password string `yaml:"password" json:"password,omitempty"` Password string `yaml:"password" json:"password,omitempty"`

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 user: xx@163.com
alias: xx alias: xx
pass: pass:
Host: smtp.163.com host: smtp.163.com
Port: 465 port: 465
Ssl: true insecureSkipVerify: false
ssl: ssl:
cert: "" cert: ""