-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
232 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
Oops, something went wrong.