设置配置文件参数

This commit is contained in:
xing 2022-11-05 15:07:08 +08:00
parent fc5e71d79d
commit 8b5424bdd2
5 changed files with 20 additions and 7 deletions

View File

@ -69,3 +69,5 @@ postCommentUrl: http://wp.test/wp-comments-post.php
trustIps: []
# trust servername 信任的域名
trustServerNames: ["xy.test","blog.xy.test"]
# port
port: 8082

View File

@ -3,7 +3,7 @@ package config
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"time"
)
@ -36,6 +36,7 @@ type Config struct {
PostCommentUrl string `yaml:"postCommentUrl"`
TrustIps []string `yaml:"trustIps"`
TrustServerNames []string `yaml:"trustServerNames"`
Port string `yaml:"port"`
}
type Mail struct {
@ -52,8 +53,11 @@ type Mysql struct {
Pool Pool `yaml:"pool"`
}
func InitConfig() error {
file, err := ioutil.ReadFile("config.yaml")
func InitConfig(conf string) error {
if conf == "" {
conf = "config.yaml"
}
file, err := os.ReadFile(conf)
if err != nil {
return err
}

View File

@ -6,7 +6,7 @@ import (
)
func TestSendMail(t *testing.T) {
config.InitConfig()
config.InitConfig("config.yaml")
type args struct {
mailTo []string
subject string

11
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"flag"
"github/fthvgb1/wp-go/actions"
"github/fthvgb1/wp-go/actions/common"
"github/fthvgb1/wp-go/config"
@ -13,8 +14,11 @@ import (
)
func init() {
var c string
flag.StringVar(&c, "c", "config.yaml", "config file")
flag.Parse()
rand.Seed(time.Now().UnixNano())
err := config.InitConfig()
err := config.InitConfig(c)
if err != nil {
panic(err)
}
@ -52,7 +56,10 @@ func cronClearCache() {
}
func main() {
err := route.SetupRouter().Run(":8082")
if config.Conf.Port == "" {
config.Conf.Port = "80"
}
err := route.SetupRouter().Run(config.Conf.Port)
if err != nil {
panic(err)
}

View File

@ -13,7 +13,7 @@ import (
"unicode/utf8"
)
// PasswordHash 目前还不完善,只有encode64 getRandomBytes CryptPrivate 方法能用
// PasswordHash 目前还不完善,只有 Encode64 getRandomBytes CryptPrivate 方法能用
type PasswordHash struct {
itoa64 string
iterationCountLog2 int