github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/config.go (about)

     1  package volts
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/volts-dev/volts/client"
     7  	"github.com/volts-dev/volts/registry"
     8  	"github.com/volts-dev/volts/server"
     9  	"github.com/volts-dev/volts/transport"
    10  )
    11  
    12  type (
    13  	Option func(*Config)
    14  
    15  	Config struct {
    16  		Client client.IClient
    17  		Server server.IServer
    18  
    19  		// Before and After funcs
    20  		BeforeStart []func() error // TODO 处理服务器启动后的事物
    21  		BeforeStop  []func() error
    22  		AfterStart  []func() error
    23  		AfterStop   []func() error
    24  		// Other options for implementations of the interface
    25  		// can be stored in a context
    26  		Context context.Context
    27  		Signal  bool
    28  		Debug   bool
    29  	}
    30  )
    31  
    32  func newConfig(opts ...Option) *Config {
    33  	cfg := &Config{}
    34  	cfg.Init(opts...)
    35  
    36  	if cfg.Context == nil {
    37  		cfg.Context = context.Background()
    38  	}
    39  
    40  	if cfg.Server == nil {
    41  		cfg.Server = server.New(
    42  			//server.Name(self.Name),
    43  			server.WithTransport(
    44  				transport.NewHTTPTransport(),
    45  			),
    46  		)
    47  	}
    48  
    49  	if cfg.Client == nil {
    50  		cfg.Client = client.NewRpcClient(
    51  			/*client.WithTransport(self.Transport)*/
    52  			client.WithConfigPrefixName(cfg.Server.Config().Name),
    53  		)
    54  	}
    55  	return cfg
    56  }
    57  
    58  func (self *Config) Init(opts ...Option) {
    59  	for _, opt := range opts {
    60  		opt(self)
    61  	}
    62  
    63  	if self.Debug {
    64  		//self.Transport.Init(transport.Debug())
    65  		if self.Client != nil {
    66  			self.Client.Init(client.Debug())
    67  		}
    68  		if self.Server != nil {
    69  			self.Server.Config().Init(server.Debug())
    70  		}
    71  	}
    72  }
    73  
    74  // Name of the service
    75  func Name(name string) Option {
    76  	return func(cfg *Config) {
    77  		if cfg.Server != nil {
    78  			cfg.Server.Config().Init(server.Name(name))
    79  		}
    80  	}
    81  }
    82  
    83  // Client to be used for service
    84  // under debug mode the port will keep at 35999
    85  func Debug() Option {
    86  	return func(cfg *Config) {
    87  		cfg.Debug = true
    88  	}
    89  }
    90  
    91  // Client to be used for service
    92  func Client(cli client.IClient) Option {
    93  	return func(cfg *Config) {
    94  		cfg.Client = cli
    95  	}
    96  }
    97  
    98  // Server to be used for service
    99  func Server(srv server.IServer) Option {
   100  	return func(cfg *Config) {
   101  		cfg.Server = srv
   102  	}
   103  }
   104  
   105  // Registry sets the registry for the Server
   106  // and the underlying components
   107  func Registry(r registry.IRegistry) Option {
   108  	return func(cfg *Config) {
   109  		// Update Client and Server
   110  		if cfg.Client != nil {
   111  			cfg.Client.Init(client.WithRegistry(r))
   112  		}
   113  
   114  		if cfg.Server != nil {
   115  			cfg.Server.Config().Init(server.WithRegistry(r))
   116  		}
   117  	}
   118  }
   119  
   120  // Transport sets the transport for the Server and client
   121  // and the underlying components
   122  func Transport(t transport.ITransport) Option {
   123  	return func(cfg *Config) {
   124  		// Update Client and Server
   125  		if cfg.Client != nil {
   126  			cfg.Client.Init(client.WithTransport(t))
   127  		}
   128  
   129  		if cfg.Server != nil {
   130  			cfg.Server.Config().Init(server.WithTransport(t))
   131  		}
   132  
   133  	}
   134  }