Skip to content

Commit

Permalink
perf: 优化参数注册
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Dec 13, 2023
1 parent d22c90c commit f92ac4f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
37 changes: 13 additions & 24 deletions cmd/args/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,49 @@ var Efs *embed.FS

// 数据存储

type IDataset struct {
var Dataset = struct {
Dir string
Secret string
}

var Dataset = IDataset{
}{
Dir: "var",
}

// 日志参数

type ILogger struct {
var Logger = struct {
Dir string
Level string
Target string
}

var Logger = ILogger{
Dir: ".",
}{
Level: "info",
Target: "stdout",
}

// 数据库参数
// 数据库参数 - Server

type IDatabase struct {
var Database = struct {
Type string
Host string
User string
Passwd string
Name string
Option string
}

var Database = IDatabase{
}{
Type: "sqlite",
Name: "server.db",
}

// 主节点参数
// 主节点参数 - Server

type IServer struct {
var Server = struct {
Listen string
JwtKey string
}

var Server = IServer{
}{
Listen: ":7800",
}

// 子节点参数
// 子节点参数 - Worker

type IWorker struct {
var Worker = struct {
Remote string
}

var Worker = IWorker{}
}{}
20 changes: 13 additions & 7 deletions cmd/parse/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/knadh/koanf/v2"
"github.com/opentdp/go-helper/filer"
"github.com/opentdp/go-helper/logman"
"github.com/opentdp/go-helper/strutil"

"tdp-cloud/cmd/args"
)
Expand All @@ -26,6 +27,8 @@ type Config struct {

func NewConfig() *Config {

preConfig()

var p = yaml.Parser()
var k = koanf.NewWithConf(koanf.Conf{
StrictMerge: true,
Expand All @@ -41,8 +44,8 @@ func (c *Config) Server() {
// 读取默认配置
mp := map[string]any{
"dataset": args.Dataset,
"logger": args.Logger,
"database": args.Database,
"logger": args.Logger,
"server": args.Server,
}
c.Koanf.Load(confmap.Provider(mp, "."), nil)
Expand All @@ -52,12 +55,15 @@ func (c *Config) Server() {
c.ReadYaml()
// 使用配置文件中的参数覆盖默认值
c.Koanf.Unmarshal("dataset", &args.Dataset)
c.Koanf.Unmarshal("logger", &args.Logger)
c.Koanf.Unmarshal("database", &args.Database)
c.Koanf.Unmarshal("logger", &args.Logger)
c.Koanf.Unmarshal("server", &args.Server)
}

RuntimeFix()
// 初始化 JwtKey
if args.Server.JwtKey == "" {
args.Server.JwtKey = strutil.Rand(32)
}

}

Expand All @@ -80,8 +86,6 @@ func (c *Config) Worker() {
c.Koanf.Unmarshal("worker", &args.Worker)
}

RuntimeFix()

}

func (c *Config) ReadYaml() {
Expand All @@ -102,19 +106,21 @@ func (c *Config) ReadYaml() {

func (c *Config) WriteYaml(force bool) {

postConfig()

// 是否强制覆盖
if !force && filer.Exists(YamlFile) {
return
}

// 序列化参数信息
b, err := c.Koanf.Marshal(c.Parser)
buf, err := c.Koanf.Marshal(c.Parser)
if err != nil {
logman.Fatal("write config error", "error", err)
}

// 将参数写入配置文件
err = os.WriteFile(YamlFile, b, 0644)
err = os.WriteFile(YamlFile, buf, 0644)
if err != nil {
logman.Fatal("write config error", "error", err)
}
Expand Down
24 changes: 10 additions & 14 deletions cmd/parse/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ import (
"tdp-cloud/cmd/args"
)

func RuntimeFix() {

// debug mode
func preConfig() {

debug := os.Getenv("TDP_DEBUG")
args.Debug = debug == "1" || debug == "true"

// init dataset
}

if args.Dataset.Dir != "" && args.Dataset.Dir != "." {
os.MkdirAll(args.Dataset.Dir, 0755)
}
func postConfig() {

// init dataset

if args.Dataset.Secret == "" {
args.Dataset.Secret = strutil.Rand(32)
}

if args.Dataset.Dir != "" && args.Dataset.Dir != "." {
os.MkdirAll(args.Dataset.Dir, 0755)
}

// init logger

if !path.IsAbs(args.Logger.Dir) {
Expand All @@ -41,13 +43,7 @@ func RuntimeFix() {
Level: args.Logger.Level,
Target: args.Logger.Target,
Storage: args.Logger.Dir,
Filename: "global",
Filename: "default",
})

// init jwtkey

if args.Server.JwtKey == "" {
args.Server.JwtKey = strutil.Rand(32)
}

}

0 comments on commit f92ac4f

Please sign in to comment.