github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/cmd/natsql/config.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/angenalZZZ/gofunc/configfile"
     9  	"github.com/angenalZZZ/gofunc/data"
    10  	"github.com/angenalZZZ/gofunc/data/cache/store"
    11  	"github.com/angenalZZZ/gofunc/f"
    12  	"github.com/angenalZZZ/gofunc/log"
    13  	"github.com/go-redis/redis/v7"
    14  )
    15  
    16  var (
    17  	// 全局订阅前缀:subject
    18  	subject string
    19  	// 发布订阅功能处理集
    20  	handlers   []*handler
    21  	scriptFile string
    22  	configInfo *Config
    23  	configFile = "natsql.yaml"
    24  	// 当前目录下:全局订阅前缀:目录
    25  	cacheDir  string
    26  	configMod time.Time
    27  	scriptMod time.Time
    28  )
    29  
    30  // Config The Config Info For natsql.yaml
    31  type Config struct {
    32  	// 功能配置根目录cache+js目录
    33  	Dir string
    34  	// 功能配置js文件
    35  	Js string
    36  	// 数据库client
    37  	Db struct {
    38  		// 支持mssql,mysql
    39  		Type string
    40  		// 连接字符串
    41  		Conn string
    42  	}
    43  	// 消息中间件/发布订阅处理
    44  	Nats struct {
    45  		// 消息中间件client
    46  		// nat.Connection
    47  		Addr  string
    48  		Token string
    49  		Cred  string
    50  		Cert  string
    51  		Key   string
    52  		// 全局订阅前缀=功能配置根目录cache+js目录 function func(records)
    53  		Subscribe string
    54  		// 批量获取记录数限制
    55  		Amount int
    56  		// 批量插入记录数<=2000
    57  		Bulk int
    58  		// 间隔?毫秒,批量处理一次
    59  		Interval int
    60  		// 配置订阅任务"subscribe"
    61  		Script string
    62  	}
    63  	Redis *redis.Options
    64  	Log   *log.Config
    65  }
    66  
    67  func initConfig() error {
    68  	isConfig := configInfo != nil
    69  	if !isConfig {
    70  		configInfo = new(Config)
    71  	}
    72  
    73  	if !isConfig && isConfigMod() == false {
    74  		return os.ErrNotExist
    75  	}
    76  
    77  	if err := configfile.YamlTo(configFile, configInfo); err != nil {
    78  		return err
    79  	}
    80  
    81  	if filename := configInfo.Nats.Script; strings.HasSuffix(filename, ".js") {
    82  		scriptFile = filename
    83  
    84  		if !isConfig && isScriptMod() == false {
    85  			return os.ErrNotExist
    86  		}
    87  
    88  		if err := doScriptMod(); err != nil {
    89  			return err
    90  		}
    91  	} else {
    92  		configInfo.Nats.Script = strings.TrimSpace(filename)
    93  	}
    94  
    95  	data.DbType, data.DbConn = configInfo.Db.Type, configInfo.Db.Conn
    96  
    97  	if store.RedisClient == nil && configInfo.Redis != nil && configInfo.Redis.Addr != "" {
    98  		store.RedisClient = redis.NewClient(configInfo.Redis)
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  func isConfigMod() bool {
   105  	if configFile == "" {
   106  		return false
   107  	}
   108  	info, err := os.Stat(configFile)
   109  	if os.IsNotExist(err) {
   110  		return false
   111  	}
   112  	if t := info.ModTime(); t.Unix() != configMod.Unix() {
   113  		configMod = t
   114  		return true
   115  	}
   116  	return false
   117  }
   118  
   119  func isScriptMod() bool {
   120  	if scriptFile == "" {
   121  		return false
   122  	}
   123  	info, err := os.Stat(scriptFile)
   124  	if os.IsNotExist(err) {
   125  		return false
   126  	}
   127  	if t := info.ModTime(); t.Unix() != scriptMod.Unix() {
   128  		scriptMod = t
   129  		return true
   130  	}
   131  	return false
   132  }
   133  
   134  func doScriptMod() error {
   135  	if scriptFile == "" {
   136  		return nil
   137  	}
   138  	script, err := f.ReadFile(scriptFile)
   139  	if err != nil {
   140  		return err
   141  	}
   142  
   143  	configInfo.Nats.Script = strings.TrimSpace(string(script))
   144  	return nil
   145  }