Add go http client

This commit is contained in:
若海
2023-12-17 13:16:15 +08:00
parent ccf742aef8
commit 4d9808b584
19 changed files with 2792 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
package args
import (
"embed"
)
// 调试模式
var Debug bool
// 嵌入目录
var Efs *embed.FS
// 日志参数
var Logger = struct {
Dir string
Level string
Target string
}{
Dir: "logs",
Level: "info",
Target: "stdout",
}
// Http 服务参数
var Httpd = struct {
Address string
Token string
}{
Address: "127.0.0.1:7600",
}
// Wcf 服务参数
var Wcf = struct {
Address string
SdkLibrary string
WeChatAuto bool
MsgPrint bool
}{
Address: "127.0.0.1:10080",
SdkLibrary: "sdk.dll",
}
+72
View File
@@ -0,0 +1,72 @@
package args
import (
"os"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/opentdp/go-helper/filer"
"github.com/opentdp/go-helper/logman"
)
// 配置信息操作类
type Config struct {
Koanf *koanf.Koanf
Parser *yaml.YAML
File string
}
func (c *Config) Init() *Config {
debug := os.Getenv("TDP_DEBUG")
Debug = debug == "1" || debug == "true"
c.Koanf = koanf.NewWithConf(koanf.Conf{
StrictMerge: true,
Delim: ".",
})
c.Parser = yaml.Parser()
c.File = "config.yml"
return c
}
func (c *Config) ReadYaml() {
// 配置不存在则忽略
_, err := os.Stat(c.File)
if os.IsNotExist(err) {
return
}
// 从配置文件读取参数
err = c.Koanf.Load(file.Provider(c.File), c.Parser)
if err != nil {
logman.Fatal("read config error", "error", err)
}
}
func (c *Config) WriteYaml() {
// 是否强制覆盖
if filer.Exists(c.File) {
return
}
// 序列化参数信息
buf, err := c.Koanf.Marshal(c.Parser)
if err != nil {
logman.Fatal("write config error", "error", err)
}
// 将参数写入配置文件
err = os.WriteFile(c.File, buf, 0644)
if err != nil {
logman.Fatal("write config error", "error", err)
}
}
+45
View File
@@ -0,0 +1,45 @@
package args
import (
"os"
"github.com/knadh/koanf/providers/confmap"
"github.com/opentdp/go-helper/logman"
)
func (c *Config) Unmarshal() {
// 读取默认配置
mp := map[string]any{
"logger": &Logger,
"httpd": &Httpd,
"wcf": &Wcf,
}
c.Koanf.Load(confmap.Provider(mp, "."), nil)
// 读取配置文件
c.ReadYaml()
for k, v := range mp {
c.Koanf.Unmarshal(k, v)
}
// 初始化日志
if Logger.Dir != "" && Logger.Dir != "." {
os.MkdirAll(Logger.Dir, 0755)
}
logman.SetDefault(&logman.Config{
Level: Logger.Level,
Target: Logger.Target,
Storage: Logger.Dir,
Filename: "rest",
})
// 写入配置文件
c.WriteYaml()
}