github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/core/def.go (about)

     1  package core
     2  
     3  import (
     4  	"github.com/15mga/kiwi"
     5  	"github.com/15mga/kiwi/log"
     6  	"github.com/15mga/kiwi/util/mgo"
     7  	"github.com/15mga/kiwi/util/rds"
     8  	"github.com/15mga/kiwi/worker"
     9  	"github.com/gomodule/redigo/redis"
    10  	"go.mongodb.org/mongo-driver/mongo/options"
    11  	"time"
    12  )
    13  
    14  type option struct {
    15  	meta     *Meta
    16  	mongo    *Mongo
    17  	redis    *Redis
    18  	worker   Worker
    19  	node     kiwi.INode
    20  	services []kiwi.IService
    21  	gate     *Gate
    22  	loggers  []kiwi.ILogger
    23  }
    24  
    25  type Meta struct {
    26  	Id          int64
    27  	SvcToVer    map[string]string
    28  	SvcNameConv func(string) kiwi.TSvc
    29  }
    30  
    31  func SetMeta(meta *Meta) Option {
    32  	return func(o *option) {
    33  		o.meta = meta
    34  	}
    35  }
    36  
    37  func SetLoggers(loggers ...kiwi.ILogger) Option {
    38  	return func(o *option) {
    39  		o.loggers = loggers
    40  	}
    41  }
    42  
    43  func SetServices(services []kiwi.IService) Option {
    44  	return func(o *option) {
    45  		o.services = services
    46  	}
    47  }
    48  
    49  type Mongo struct {
    50  	uri     string
    51  	db      string
    52  	options *options.DatabaseOptions
    53  }
    54  
    55  func SetMongoDB(uri, db string, options *options.DatabaseOptions) Option {
    56  	return func(o *option) {
    57  		o.mongo = &Mongo{
    58  			uri:     uri,
    59  			db:      db,
    60  			options: options,
    61  		}
    62  	}
    63  }
    64  
    65  type Redis struct {
    66  	Addr     string
    67  	User     string
    68  	Password string
    69  	Db       int
    70  }
    71  
    72  func SetRedis(addr, user, pw string, db int) Option {
    73  	return func(o *option) {
    74  		o.redis = &Redis{
    75  			Addr:     addr,
    76  			User:     user,
    77  			Password: pw,
    78  			Db:       db,
    79  		}
    80  	}
    81  }
    82  
    83  type Worker struct {
    84  	active   bool
    85  	share    bool
    86  	parallel bool
    87  	global   bool
    88  }
    89  
    90  func SetWorker(active, share, parallel, global bool) Option {
    91  	return func(o *option) {
    92  		o.worker = Worker{
    93  			active:   active,
    94  			share:    share,
    95  			parallel: parallel,
    96  			global:   global,
    97  		}
    98  	}
    99  }
   100  
   101  type Gate struct {
   102  	receiver kiwi.FnAgentBytes
   103  	options  []GateOption
   104  }
   105  
   106  func SetGate(receiver kiwi.FnAgentBytes, options ...GateOption) Option {
   107  	return func(o *option) {
   108  		o.gate = &Gate{
   109  			receiver: receiver,
   110  			options:  options,
   111  		}
   112  	}
   113  }
   114  
   115  type Option func(*option)
   116  
   117  func StartDefault(opts ...Option) {
   118  	opt := option{
   119  		worker: Worker{
   120  			active:   true,
   121  			share:    true,
   122  			parallel: true,
   123  			global:   true,
   124  		},
   125  		node: NewNodeLocal(),
   126  		loggers: []kiwi.ILogger{
   127  			log.NewStd(),
   128  		},
   129  	}
   130  	for _, o := range opts {
   131  		o(&opt)
   132  	}
   133  
   134  	if len(opt.loggers) > 0 {
   135  		for _, logger := range opt.loggers {
   136  			kiwi.AddLogger(logger)
   137  		}
   138  	}
   139  
   140  	nodeMeta := kiwi.GetNodeMeta()
   141  	nodeMeta.Init(opt.meta.Id)
   142  	for svc, ver := range opt.meta.SvcToVer {
   143  		nodeMeta.AddService(opt.meta.SvcNameConv(svc), ver)
   144  	}
   145  
   146  	if opt.mongo != nil {
   147  		clientOpt := options.Client().ApplyURI(opt.mongo.uri)
   148  		err := mgo.Conn(opt.mongo.db, clientOpt, opt.mongo.options)
   149  		if err != nil {
   150  			panic(err)
   151  		}
   152  
   153  		err = mgo.CheckColl()
   154  		if err != nil {
   155  			kiwi.Fatal(err)
   156  		}
   157  	}
   158  
   159  	if opt.redis != nil {
   160  		rdsFac, rdsPool := getRedisFac(opt.redis)
   161  		rds.InitRedis(
   162  			rds.ConnFac(rdsFac),
   163  			rds.ConnPool(rdsPool),
   164  		)
   165  	}
   166  
   167  	if opt.worker.active {
   168  		worker.InitActive()
   169  	}
   170  	if opt.worker.share {
   171  		worker.InitShare()
   172  	}
   173  	if opt.worker.parallel {
   174  		worker.InitParallel()
   175  	}
   176  	if opt.worker.global {
   177  		worker.InitGlobal()
   178  	}
   179  	InitPacker()
   180  	InitCodec()
   181  	kiwi.SetNode(opt.node)
   182  	InitRouter()
   183  	RegisterSvc(opt.services...)
   184  
   185  	if opt.gate != nil {
   186  		InitGate(opt.gate.receiver, opt.gate.options...)
   187  	}
   188  	StartAllService()
   189  	kiwi.WaitExit()
   190  }
   191  
   192  func getRedisFac(conf *Redis) (rds.ToRedisConnError, *redis.Pool) {
   193  	redisFac := func() (redis.Conn, error) {
   194  		return redis.Dial("tcp", conf.Addr,
   195  			redis.DialUsername(conf.User),
   196  			redis.DialPassword(conf.Password),
   197  			redis.DialDatabase(conf.Db))
   198  	}
   199  	redisPool := &redis.Pool{
   200  		Dial:        redisFac,
   201  		IdleTimeout: 300 * time.Second,
   202  		MaxActive:   512,
   203  		MaxIdle:     512,
   204  	}
   205  	return redisFac, redisPool
   206  }