修正logsize的解析问题

This commit is contained in:
lwch
2021-08-13 10:20:45 +08:00
parent 947963798d
commit 0bf8d57e06
7 changed files with 38 additions and 10 deletions
+5 -4
View File
@@ -2,6 +2,7 @@ package global
import (
"crypto/md5"
"natpass/code/utils"
"os"
"github.com/lwch/runtime"
@@ -23,7 +24,7 @@ type Configure struct {
Server string
Enc [md5.Size]byte
LogDir string
LogSize int
LogSize utils.Bytes
LogRotate int
Tunnels []Tunnel
}
@@ -34,9 +35,9 @@ func LoadConf(dir string) *Configure {
Server string `yaml:"server"`
Secret string `yaml:"secret"`
Log struct {
Dir string `yaml:"dir"`
Size int `yaml:"size"`
Rotate int `yaml:"rotate"`
Dir string `yaml:"dir"`
Size utils.Bytes `yaml:"size"`
Rotate int `yaml:"rotate"`
} `yaml:"log"`
Tunnel []Tunnel `yaml:"tunnel"`
}
+1 -1
View File
@@ -33,7 +33,7 @@ func main() {
cfg := global.LoadConf(*conf)
logging.SetSizeRotate(cfg.LogDir, "np-cli", cfg.LogSize, cfg.LogRotate, true)
logging.SetSizeRotate(cfg.LogDir, "np-cli", int(cfg.LogSize.Bytes()), cfg.LogRotate, true)
conn, err := tls.Dial("tcp", cfg.Server, nil)
runtime.Assert(err)
+5 -4
View File
@@ -2,6 +2,7 @@ package global
import (
"crypto/md5"
"natpass/code/utils"
"os"
"github.com/lwch/runtime"
@@ -14,7 +15,7 @@ type Configure struct {
TLSKey string
TLSCrt string
LogDir string
LogSize int
LogSize utils.Bytes
LogRotate int
}
@@ -23,9 +24,9 @@ func LoadConf(dir string) *Configure {
Listen uint16 `yaml:"listen"`
Secret string `yaml:"secret"`
Log struct {
Dir string `yaml:"dir"`
Size int `yaml:"size"`
Rotate int `yaml:"rotate"`
Dir string `yaml:"dir"`
Size utils.Bytes `yaml:"size"`
Rotate int `yaml:"rotate"`
} `yaml:"log"`
TLS struct {
Key string `yaml:"key"`
+1 -1
View File
@@ -31,7 +31,7 @@ func main() {
cfg := global.LoadConf(*conf)
logging.SetSizeRotate(cfg.LogDir, "np-svr", cfg.LogSize, cfg.LogRotate, true)
logging.SetSizeRotate(cfg.LogDir, "np-svr", int(cfg.LogSize.Bytes()), cfg.LogRotate, true)
cert, err := tls.LoadX509KeyPair(cfg.TLSCrt, cfg.TLSKey)
runtime.Assert(err)
+23
View File
@@ -0,0 +1,23 @@
package utils
import "github.com/dustin/go-humanize"
type Bytes uint64
func (bt *Bytes) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
err := unmarshal(&str)
if err != nil {
return err
}
n, err := humanize.ParseBytes(str)
if err != nil {
return err
}
*bt = Bytes(n)
return nil
}
func (bt *Bytes) Bytes() uint64 {
return uint64(*bt)
}
+1
View File
@@ -3,6 +3,7 @@ module natpass
go 1.16
require (
github.com/dustin/go-humanize v1.0.0
github.com/lwch/daemon v0.0.0-20210628111519-5911af6cec06
github.com/lwch/logging v0.0.0-20210528090125-a154917d90c6
github.com/lwch/runtime v0.0.0-20190520054850-8c97e19e0c6d
+2
View File
@@ -1,3 +1,5 @@
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=