]> git.uzoombox.com Git - uzsdk-v0.3.2.git/commitdiff
feat(uzsdk): 1. add function for check & default configuration. v0.3.0
authorbruce <bruce@nanzoom.com>
Mon, 4 Nov 2024 14:48:08 +0000 (14:48 +0000)
committerbruce <bruce@nanzoom.com>
Mon, 4 Nov 2024 14:48:08 +0000 (14:48 +0000)
models/variables.go [new file with mode: 0644]
uzsdk/configuration.go

diff --git a/models/variables.go b/models/variables.go
new file mode 100644 (file)
index 0000000..5fddfdf
--- /dev/null
@@ -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
+}
index e4609a83eccdefb5286f2c513832b15da5d97c2a..335f0e64ac5671b78c81fce143c6d798a00443b9 100644 (file)
@@ -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
+       }
+}