zhangzengfei
2023-08-14 8f750b461a4f442825e516016bf78d05ed66afcb
config/config.go
@@ -9,24 +9,53 @@
)
type Config struct {
   WebPort     string `json:"web_port"`
   SqlAddr     string `json:"sql_addr"`
   SqlDBName   string `json:"sql_db_name"`
   SqlUsername string `json:"sql_username"`
   SqlPassword string `json:"sql_password"`
   WebPort        string `json:"web_port"`        // 本地web服务绑定接口
   SqlAddr        string `json:"sql_addr"`        // 数据库ip地址, 不加端口号, 默认使用1433端口
   SqlDBName      string `json:"sql_db_name"`     // 数据库名称
   SqlUsername    string `json:"sql_username"`    // 数据库用户
   SqlPassword    string `json:"sql_password"`    // 数据库密码
   NsqServer      string `json:"nsq_server"`      // nsq TCP服务端地址
   NsqWebApi      string `json:"nsq_webapi"`      // nsq HTTP接口地址
   OrderTopic     string `json:"order_topic"`     // 订单上报的topic
   BomTopic       string `json:"bom_topic"`       // bom上报的topic
   BomQueryTopic  string `json:"bom_query_topic"` // bom上报的topic
   BomChildTopic  string `json:"bom_child_topic"` // bom子项上报的topic
   InventoryTopic string `json:"inventory_topic"` // 库存上报的topic
   SqlQueryTopic  string `json:"query_topic"`     // 金蝶查询接口的topic
   SqlReplyTopic  string `json:"reply_topic"`     // 金蝶响应查询接口的topic
   CSTWebApi      string `json:"cst_webapi"`      // 生产任务单提交地址
   CSTQueryTopic  string `json:"cst_query_topic"` // 提交生产任务单主题
   CSTReplyTopic  string `json:"cst_reply_topic"` // 响应生产任务单主题
   SyncInterval   int    `json:"interval"`        // 同步的时间间隔, 单位/秒
}
const configPath = "config.json"
var Options Config
// Init is an exported method that takes the environment starts the viper
// (external lib) and returns the configuration struct.
func init() {
func DefaultConfig() {
   Options.WebPort = "10210"
   Options.SqlAddr = "127.0.0.1"
   Options.SqlDBName = "dbname"
   Options.SqlUsername = "sa"
   Options.SqlPassword = "123456"
   Options.NsqServer = "fai365.com:4150"
   Options.NsqWebApi = "http://121.31.232.83:9080/api/nsq/pub?topic=your_topic"
   Options.OrderTopic = "aps.factory.erp.seorder"
   Options.BomTopic = "aps.factory.erp.icBom"
   Options.BomChildTopic = "aps.factory.erp.icBomChild"
   Options.BomQueryTopic = "aps.factory.erp.icBomQuery"
   Options.InventoryTopic = "aps.factory.erp.inventory"
   Options.SqlQueryTopic = "aps.factory.erp.k3resource"
   Options.SqlReplyTopic = "aps.factory.erp.k3reply"
   Options.CSTQueryTopic = "aps.factory.erp.cstApply"
   Options.CSTReplyTopic = "aps.factory.erp.cstReply"
   Options.SyncInterval = 60
}
func Load() {
   if !pathExists(configPath) {
      DefaultConfig()
      SaveConfig()
   } else {
      file, _ := os.Open(configPath)
@@ -35,8 +64,6 @@
      fd := json.NewDecoder(file)
      fd.Decode(&Options)
      fmt.Printf("%v\n", Options)
   }
}
@@ -44,8 +71,11 @@
   b, err := json.Marshal(Options)
   if err == nil {
      var out bytes.Buffer
      err = json.Indent(&out, b, "", "    ")
      ioutil.WriteFile(configPath, out.Bytes(), 0644)
      if err == nil {
         err = ioutil.WriteFile(configPath, out.Bytes(), 0644)
      }
   }
   return err