github.com/kaydxh/golang@v0.0.131/pkg/binlog/config.go (about)

     1  /*
     2   *Copyright (c) 2023, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package binlog
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  
    28  	ds_ "github.com/kaydxh/golang/pkg/binlog/datastore"
    29  	dbstore_ "github.com/kaydxh/golang/pkg/binlog/datastore/dbstore"
    30  	filestore_ "github.com/kaydxh/golang/pkg/binlog/datastore/filestore"
    31  	mysql_ "github.com/kaydxh/golang/pkg/database/mysql"
    32  	rotate_ "github.com/kaydxh/golang/pkg/file-rotate"
    33  	mq_ "github.com/kaydxh/golang/pkg/mq"
    34  	taskq_ "github.com/kaydxh/golang/pkg/pool/taskqueue"
    35  	viper_ "github.com/kaydxh/golang/pkg/viper"
    36  	"github.com/sirupsen/logrus"
    37  	"github.com/spf13/viper"
    38  )
    39  
    40  type Config struct {
    41  	Proto Binlog
    42  	opts  struct {
    43  		// If set, overrides params below
    44  		viper *viper.Viper
    45  	}
    46  }
    47  
    48  type completedConfig struct {
    49  	*Config
    50  	completeError error
    51  }
    52  
    53  type CompletedConfig struct {
    54  	// Embed a private pointer that cannot be instantiated outside of this package.
    55  	*completedConfig
    56  }
    57  
    58  func (c *completedConfig) New(ctx context.Context, taskq *taskq_.Pool, consumers []mq_.Consumer, opts ...BinlogServiceOption) (*BinlogService, error) {
    59  
    60  	logrus.Infof("Installing BinlogService")
    61  
    62  	if c.completeError != nil {
    63  		return nil, c.completeError
    64  	}
    65  
    66  	if !c.Proto.GetEnabled() {
    67  		logrus.Warnf("BinlogService disenabled")
    68  		return nil, nil
    69  	}
    70  
    71  	sqlxDB, err := c.install(ctx, taskq, consumers, opts...)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	logrus.Infof("Installed BinlogService")
    76  
    77  	return sqlxDB, nil
    78  }
    79  
    80  func (c *completedConfig) install(ctx context.Context, taskq *taskq_.Pool, consumers []mq_.Consumer, opts ...BinlogServiceOption) (*BinlogService, error) {
    81  	config := &c.Proto
    82  	var (
    83  		dataStore ds_.DataStore
    84  		err       error
    85  	)
    86  	switch config.GetBinlogType() {
    87  	case BinlogType_BinlogType_DB:
    88  		db := mysql_.GetDB()
    89  		if db == nil {
    90  			return nil, fmt.Errorf("db is not installed")
    91  		}
    92  		dataStore, err = dbstore_.NewDBDataStore(db)
    93  		if err != nil {
    94  			return nil, fmt.Errorf("db is not installed")
    95  		}
    96  	case BinlogType_BinlogType_File:
    97  		filelogConfig := config.GetFileLog()
    98  
    99  		dataStore, err = filestore_.NewFileDataStore(
   100  			filelogConfig.GetFilepath(),
   101  			rotate_.WithRotateSize(filelogConfig.GetRotateSize()),
   102  			rotate_.WithRotateInterval(filelogConfig.GetRotateInterval().AsDuration()),
   103  			//rotate_.WithRotateCallback(bs.rotateCallback),
   104  		)
   105  		if err != nil {
   106  			return nil, err
   107  		}
   108  
   109  	default:
   110  		return nil, fmt.Errorf("binlog type %v is not support", config.GetBinlogType())
   111  	}
   112  	bs, err := NewBinlogService(dataStore, taskq, consumers, opts...)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  	err = bs.Run(ctx)
   117  	return bs, err
   118  }
   119  
   120  // Complete set default ServerRunOptions.
   121  func (c *Config) Complete() CompletedConfig {
   122  	err := c.loadViper()
   123  	if err != nil {
   124  		return CompletedConfig{&completedConfig{
   125  			Config:        c,
   126  			completeError: err,
   127  		}}
   128  	}
   129  
   130  	return CompletedConfig{&completedConfig{Config: c}}
   131  }
   132  
   133  func (c *Config) loadViper() error {
   134  	if c.opts.viper != nil {
   135  		return viper_.UnmarshalProtoMessageWithJsonPb(c.opts.viper, &c.Proto)
   136  	}
   137  
   138  	return nil
   139  }
   140  
   141  func NewConfig(options ...ConfigOption) *Config {
   142  	c := &Config{}
   143  	c.ApplyOptions(options...)
   144  
   145  	return c
   146  }