设置配置文件参数

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: [] trustIps: []
# trust servername 信任的域名 # trust servername 信任的域名
trustServerNames: ["xy.test","blog.xy.test"] trustServerNames: ["xy.test","blog.xy.test"]
# port
port: 8082

View File

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

View File

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

11
main.go
View File

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

View File

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