Skip to content

Commit

Permalink
perf: 优化配置文件读写
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 1, 2024
1 parent 6bf1724 commit 900b856
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 280 deletions.
2 changes: 1 addition & 1 deletion api/passport/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (*Controller) register(c *gin.Context) {

rq.Level = 0 //防止逃逸
rq.AppKey = strutil.Rand(32)
rq.StoreKey = args.Dataset.Secret
rq.StoreKey = args.Assets.Secret

if id, err := user.Create(rq); err == nil {
c.Set("Payload", gin.H{"Id": id})
Expand Down
2 changes: 1 addition & 1 deletion api/user/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (*Controller) create(c *gin.Context) {
}

rq.AppKey = strutil.Rand(32)
rq.StoreKey = args.Dataset.Secret
rq.StoreKey = args.Assets.Secret

if id, err := user.Create(rq); err == nil {
c.Set("Payload", gin.H{"Id": id})
Expand Down
10 changes: 5 additions & 5 deletions cmd/args/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ var Efs *embed.FS

// 数据存储

type IDataset struct {
type IAssets struct {
Dir string `yaml:"dir"`
Secret string `yaml:"secret"`
}

var Dataset = &IDataset{
var Assets = &IAssets{
Dir: "var",
}

// 日志参数
// 日志参数 - Logger

type ILogger struct {
Dir string `yaml:"dir"`
Expand All @@ -38,7 +38,7 @@ var Logger = &ILogger{

// 数据库参数 - Server

type IDatabase struct {
type IGormio struct {
Type string `yaml:"type"`
Host string `yaml:"host"`
User string `yaml:"user"`
Expand All @@ -47,7 +47,7 @@ type IDatabase struct {
Option string `yaml:"option"`
}

var Database = &IDatabase{
var Gormio = &IGormio{
Type: "sqlite",
Name: "server.db",
}
Expand Down
51 changes: 51 additions & 0 deletions cmd/args/setter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package args

import (
"os"
"path"
"path/filepath"

"github.com/opentdp/go-helper/logman"
"github.com/opentdp/go-helper/strutil"
)

// 初始化调试模式

func SetDebug() {

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

}

// 初始化存储目录

func SetAssets() {

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

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

}

// 初始化日志能力

func SetLogger() {

storage := Logger.Dir
if !filepath.IsAbs(storage) {
storage = path.Join(Assets.Dir, storage)
}

logman.SetDefault(&logman.Config{
Level: Logger.Level,
Target: Logger.Target,
Storage: storage,
Filename: "server",
})

}
66 changes: 66 additions & 0 deletions cmd/parse/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package parse

import (
"os"

"github.com/opentdp/go-helper/filer"
"gopkg.in/yaml.v3"
)

type Config struct {
File string
Data any
}

func (c *Config) Load() error {

if c.File == "" {
return nil
}

if !filer.Exists(c.File) {
return nil
}

bytes, err := os.ReadFile(c.File)
if err != nil {
return err
}

return yaml.Unmarshal(bytes, c.Data)

}

func (c *Config) Save() error {

if c.File == "" {
return nil
}

bytes, err := yaml.Marshal(c.Data)
if err != nil {
return err
}

return os.WriteFile(c.File, bytes, 0644)

}

func (c *Config) setYaml(n string) {

if f := "config.yml"; filer.Exists(f) {
c.File = f
return
}

if f := "/etc/tdp-" + n + ".yml"; filer.Exists(f) {
c.File = f
return
}

if f := "/etc/tdp-cloud/" + n + ".yml"; filer.Exists(f) {
c.File = f
return
}

}
84 changes: 0 additions & 84 deletions cmd/parse/koanf.go

This file was deleted.

80 changes: 27 additions & 53 deletions cmd/parse/server.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,52 @@
package parse

import (
"os"
"path"

"github.com/knadh/koanf/providers/confmap"
"github.com/opentdp/go-helper/logman"
"github.com/opentdp/go-helper/strutil"

"tdp-cloud/cmd/args"
)

func (c *Config) Server() {

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

// 读取默认配置

mp := map[string]any{
"dataset": &args.Dataset,
"database": &args.Database,
"logger": &args.Logger,
"server": &args.Server,
}
c.Koanf.Load(confmap.Provider(mp, "."), nil)

// 读取配置文件

if c.ReadYaml() == nil {
for k, v := range mp {
c.Koanf.Unmarshal(k, v)
}
}
type ServerData struct {
Assets *args.IAssets `yaml:"dataset"`
Gormio *args.IGormio `yaml:"database"`
Logger *args.ILogger `yaml:"logger"`
Server *args.IServer `yaml:"server"`
}

// 初始化存储目录
func ServerConfig(yaml string) *Config {

if args.Dataset.Secret == "" {
args.Dataset.Secret = strutil.Rand(32)
c.Override = true
}
args.SetDebug()

if args.Dataset.Dir != "" && args.Dataset.Dir != "." {
os.MkdirAll(args.Dataset.Dir, 0755)
config := &Config{
File: yaml,
Data: &ServerData{
Assets: args.Assets,
Gormio: args.Gormio,
Logger: args.Logger,
Server: args.Server,
},
}

// 修正数据库参数

if args.Database.Type == "sqlite" && !path.IsAbs(args.Database.Name) {
args.Database.Name = path.Join(args.Dataset.Dir, args.Database.Name)
if config.File == "" {
config.setYaml("server")
}

// 初始化日志能力

if !path.IsAbs(args.Logger.Dir) {
args.Logger.Dir = path.Join(args.Dataset.Dir, args.Logger.Dir)
if err := config.Load(); err != nil {
logman.Fatal("load config failed", "error", err)
}

if args.Logger.Dir != "" && args.Logger.Dir != "." {
os.MkdirAll(args.Logger.Dir, 0755)
if args.Gormio.Type == "sqlite" {
args.Gormio.Host = args.Assets.Dir
}

logman.SetDefault(&logman.Config{
Level: args.Logger.Level,
Target: args.Logger.Target,
Storage: args.Logger.Dir,
Filename: "server",
})

// 初始化 JwtKey

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

args.SetAssets()
args.SetLogger()

return config

}
Loading

0 comments on commit 900b856

Please sign in to comment.