From 0e05fdb069706bfd3cdade412ef4d9a6562d3f2c Mon Sep 17 00:00:00 2001 From: bruce Date: Mon, 4 Nov 2024 14:48:08 +0000 Subject: [PATCH] feat(uzsdk): 1. add function for check & default configuration. --- models/variables.go | 23 +++++++++++ uzsdk/configuration.go | 92 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 models/variables.go diff --git a/models/variables.go b/models/variables.go new file mode 100644 index 0000000..5fddfdf --- /dev/null +++ b/models/variables.go @@ -0,0 +1,23 @@ +package models + +import ( + "log" + "os" + + "git.uzoombox.com/git/uzsdk/utils/osutil" +) + +var ( + AppDir string // application directory + CurDir string // current working directory +) + +func init() { + AppDir = osutil.GetAppDirectory() + + dir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + CurDir = dir +} diff --git a/uzsdk/configuration.go b/uzsdk/configuration.go index e4609a8..335f0e6 100644 --- a/uzsdk/configuration.go +++ b/uzsdk/configuration.go @@ -2,6 +2,14 @@ package uzsdk +import ( + "compress/gzip" + "fmt" + "path/filepath" + + "git.uzoombox.com/git/uzsdk/models" +) + // Configuration defines application's configuration. type Configuration struct { Service struct { @@ -42,3 +50,87 @@ type Configuration struct { } } } + +func (setting *Configuration) Check() { + // Configuration.Service + if setting.Service.IP == "" { + setting.Service.IP = "0.0.0.0" + } + if setting.Service.Port <= 0 { + setting.Service.Port = 9000 + } + if setting.Service.ServerName == "" { + setting.Service.ServerName = fmt.Sprintf("%v://%v:%v", "http", setting.Service.IP, setting.Service.Port) + } + if setting.Service.SystemName == "" { + setting.Service.SystemName = fmt.Sprintf("%v://%v", "http", setting.Service.IP) + } + if setting.Service.PublicName == "" { + setting.Service.PublicName = setting.Service.ServerName + } + if setting.Service.ServicePath == "" { + setting.Service.ServicePath = "/wservice" + } + if setting.Service.DataDirectory == "" { + setting.Service.DataDirectory = "/data/uzoombox/cloud" + } + if filepath.Dir(setting.Service.DataDirectory) == "." { + setting.Service.DataDirectory = filepath.Join(models.AppDir, setting.Service.DataDirectory) + } + if len(setting.Service.TrustedProxies) == 0 { + setting.Service.TrustedProxies = append(setting.Service.TrustedProxies, "127.0.0.1") + } + + // // Configuration.System + if setting.System.DBDriver == "" { + setting.System.DBDriver = "postgres" + } + if setting.System.DBHost == "" { + setting.System.DBHost = "127.0.0.1" + } + if setting.System.DBPort <= 0 { + setting.System.DBPort = 5432 + } + if setting.System.DBUser == "" { + setting.System.DBUser = "postgres" + } + if setting.System.DBPassword == "" { + setting.System.DBPassword = "postgres" + } + if setting.System.DBName == "" { + setting.System.DBName = "smartdb" + } + if setting.System.Heartbeat <= 0 { + setting.System.Heartbeat = 604800 // 7 days + } + setting.System.IdleTime = (setting.System.Heartbeat << 2) + if setting.System.IdleTime <= 0 { + setting.System.IdleTime = (3600 << 2) + } + setting.System.CheckTime = (setting.System.Heartbeat >> 1) + if setting.System.CheckTime <= 0 { + setting.System.CheckTime = (3600 >> 1) + } + + // Configuration.GZip + if setting.GZip.Enable { + setting.GZip.Enable = true + } + if setting.GZip.Level == 0 { + setting.GZip.Level = gzip.DefaultCompression + } + + // Configuration.Logging + if setting.Logging.Level == "" { + setting.Logging.Level = "info" + } + if setting.Logging.Prefix == "" { + setting.Logging.Prefix = "" + } + if setting.Logging.File.Name == "" { + setting.Logging.File.Name = "server.log" + } + if !setting.Logging.Syslog.Enable { + setting.Logging.Syslog.Enable = false + } +} -- 2.43.5