fix
zhangqian
2023-12-01 8324f872ef3a4d0c978a9b1d062800c6a1701c12
conf/config.go
@@ -3,11 +3,12 @@
import (
   "apsClient/pkg/convertx"
   "apsClient/pkg/logx"
   "apsClient/pkg/mysqlx"
   "apsClient/pkg/redisx"
   "flag"
   "apsClient/pkg/sqlitex"
   "apsClient/pkg/timex"
   "apsClient/utils"
   "github.com/spf13/viper"
   "log"
   "time"
)
var (
@@ -23,31 +24,13 @@
)
type (
   Captcha struct {
      KeyLong            int // 验证码长度
      ImgWidth           int // 验证码宽度
      ImgHeight          int // 验证码高度
      OpenCaptcha        int // 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码次数,如3代表错误三次后出现验证码
      OpenCaptchaTimeOut int // 防爆破验证码超时时间,单位:s(秒)
   }
   JWT struct {
      SigningKey  string // jwt签名
      ExpiresTime string // 过期时间
      BufferTime  string // 缓冲时间
      Issuer      string // 签发者
   }
   System struct {
      Env           string // 环境值 develop test public
      Port          int    // 端口
      DbType        string // 数据库类型
      UseMultipoint bool   // 多点登录拦截
      UseRedis      bool   // 使用redis
      LimitCountIP  int
      LimitTimeIP   int
      RouterPrefix  string // 路由前缀
      SudoPassword  string // sudo密码
      Env              string // 环境值 develop test public
      Port             int    // 端口
      DeviceId         string //设备id
      NetSetShellPath  string //网络设置脚本路径
      NetUpShellName   string //网络停用脚本
      NetDownShellName string //网络开启脚本
   }
   Etcd struct {
@@ -78,6 +61,27 @@
   Services struct {
      ApsServer string
      Serial    string
   }
   nsqConf struct {
      NodeId         string
      NsqdAddr       string
      NsqlookupdAddr string
   }
   plc struct {
      FinishNumberTimeInterval int
      TotalNumberTimeInterval  int
      ModbusIntType            string
      SlaveId                  int
      Package                  string
      StandbyTime              int64
   }
   Prompt struct {
      SafeProduce     string `json:"safeProduce,omitempty"`
      PlcNotConnected string `json:"plcNotConnected,omitempty"`
   }
   config struct {
@@ -88,28 +92,26 @@
      Log logx.Conf
      // mysql配置
      Mysql mysqlx.Conf
      // redis配置
      Redis redisx.Conf
      // 验证码
      Captcha Captcha
      // JWT配置
      JWT JWT
      // etcd配置
      Etcd Etcd
      // rancher配置
      Rancher Rancher
      // k8s配置
      K8s K8s
      Sqlite sqlitex.Conf
      //Services Address
      Services Services
      //NsqConf
      NsqConf nsqConf
      //PLC
      PLC plc
      Prompt Prompt
      CurrentDeviceID string //设置当前面板控制的设备
      SerfClusterStatus string //集群状态
      ClusterNodeQuantity int //集群节点数量
      SystemDeviceRunSince int64 //系统开始运行时间戳
   }
)
@@ -119,14 +121,6 @@
)
func init() {
   var configFile string
   flag.StringVar(&configFile, "config", "", "config file in json")
   flag.Parse()
   if len(configFile) > 0 {
      configName = configFile
   }
   Viper = viper.New()
   Viper.SetConfigName(configName)
   for _, path := range configPaths {
@@ -140,6 +134,22 @@
   if err := Viper.Unmarshal(&Conf); err != nil {
      log.Fatalf("Unmarshal err:%v", err)
   }
   if Conf.PLC.StandbyTime == 0 {
      Conf.PLC.StandbyTime = 300 //5分钟
   }
   uptimeStr, err := utils.Exec("uptime -s")
   if err == nil {
      t, err := timex.StringToTime(uptimeStr)
      if err == nil {
         Conf.SystemDeviceRunSince = t.Unix()
      }
   } else {
      Conf.SystemDeviceRunSince = time.Now().Unix()
   }
   SetUpTime()
   ShowConfig()
}
@@ -156,11 +166,19 @@
   log.Println("......................................................")
   log.Printf("   System:                %+v", Conf.System)
   log.Printf("   Log:                   %+v", Conf.Log)
   log.Printf("   Mysql:                 %+v", Conf.Mysql)
   log.Printf("   Captcha:               %+v", Conf.Captcha)
   log.Printf("   JWT:                   %+v", Conf.JWT)
   log.Printf("   etcd:                  %+v", Conf.Etcd)
   log.Printf("   rancher:               %+v", Conf.Rancher)
   log.Printf("   k8s:                   %+v", Conf.K8s)
   log.Printf("   plc :                  %+v", Conf.PLC)
   log.Printf("   services :                  %+v", Conf.Services)
   log.Println("......................................................")
}
func SetUpTime() {
   uptimeStr, err := utils.Exec("uptime -s")
   if err == nil {
      t, err := timex.StringToTime(uptimeStr)
      if err == nil {
         Conf.SystemDeviceRunSince = t.Unix()
      }
   } else {
      Conf.SystemDeviceRunSince = time.Now().Unix()
   }
}