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

     1  /*
     2   *Copyright (c) 2022, 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 mysql
    23  
    24  import (
    25  	"context"
    26  
    27  	"github.com/jmoiron/sqlx"
    28  	viper_ "github.com/kaydxh/golang/pkg/viper"
    29  	"github.com/sirupsen/logrus"
    30  	"github.com/spf13/viper"
    31  )
    32  
    33  type Config struct {
    34  	Proto Mysql
    35  	opts  struct {
    36  		// If set, overrides params below
    37  		viper *viper.Viper
    38  	}
    39  }
    40  
    41  type completedConfig struct {
    42  	*Config
    43  	completeError error
    44  }
    45  
    46  type CompletedConfig struct {
    47  	// Embed a private pointer that cannot be instantiated outside of this package.
    48  	*completedConfig
    49  }
    50  
    51  func (c *completedConfig) New(ctx context.Context) (*sqlx.DB, error) {
    52  
    53  	logrus.Infof("Installing Mysql")
    54  
    55  	if c.completeError != nil {
    56  		return nil, c.completeError
    57  	}
    58  
    59  	if !c.Proto.GetEnabled() {
    60  		logrus.Warnf("Mysql disenabled")
    61  		return nil, nil
    62  	}
    63  
    64  	sqlxDB, err := c.install(ctx)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	logrus.Infof("Installed Mysql")
    69  
    70  	return sqlxDB, nil
    71  }
    72  
    73  func (c *completedConfig) install(ctx context.Context) (*sqlx.DB, error) {
    74  	db := NewDB(
    75  		DBConfig{
    76  			Address:  c.Proto.GetAddress(),
    77  			DataName: c.Proto.GetDbName(),
    78  			UserName: c.Proto.GetUsername(),
    79  			Password: c.Proto.GetPassword(),
    80  		},
    81  		WithMaxConnections(int(c.Proto.GetMaxConnections())),
    82  		WithMaxIdleConnections(int(c.Proto.GetMaxIdleConnections())),
    83  		WithDialTimeout(c.Proto.GetDialTimeout().AsDuration()),
    84  		WithReadTimeout(c.Proto.GetReadTimeout().AsDuration()),
    85  		WithWriteTimeout(c.Proto.GetWriteTimeout().AsDuration()),
    86  		WithConnMaxLifetime(c.Proto.GetMaxLifeTime().AsDuration()),
    87  		WithInterpolateParams(c.Proto.GetInterpolateParams()),
    88  	)
    89  
    90  	return db.GetDatabaseUntil(
    91  		ctx,
    92  		c.Proto.GetMaxWaitDuration().AsDuration(),
    93  		c.Proto.GetFailAfterDuration().AsDuration(),
    94  	)
    95  }
    96  
    97  // Complete set default ServerRunOptions.
    98  func (c *Config) Complete() CompletedConfig {
    99  	err := c.loadViper()
   100  	if err != nil {
   101  		return CompletedConfig{&completedConfig{
   102  			Config:        c,
   103  			completeError: err,
   104  		}}
   105  	}
   106  
   107  	return CompletedConfig{&completedConfig{Config: c}}
   108  }
   109  
   110  func (c *Config) loadViper() error {
   111  	if c.opts.viper != nil {
   112  		return viper_.UnmarshalProtoMessageWithJsonPb(c.opts.viper, &c.Proto)
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  func NewConfig(options ...ConfigOption) *Config {
   119  	c := &Config{}
   120  	c.ApplyOptions(options...)
   121  
   122  	return c
   123  }