go-micro.dev/v5@v5.12.0/profile/profile.go (about)

     1  // Package profileconfig provides grouped plugin profiles for go-micro
     2  package profile
     3  
     4  import (
     5  	"os"
     6  	"strings"
     7  
     8  	natslib "github.com/nats-io/nats.go"
     9  	"go-micro.dev/v5/broker"
    10  	"go-micro.dev/v5/broker/nats"
    11  	"go-micro.dev/v5/events"
    12  	nevents "go-micro.dev/v5/events/natsjs"
    13  	"go-micro.dev/v5/registry"
    14  	nreg "go-micro.dev/v5/registry/nats"
    15  	"go-micro.dev/v5/store"
    16  	nstore "go-micro.dev/v5/store/nats-js-kv"
    17  
    18  	"go-micro.dev/v5/transport"
    19  	ntx "go-micro.dev/v5/transport/nats"
    20  )
    21  
    22  type Profile struct {
    23  	Registry  registry.Registry
    24  	Broker    broker.Broker
    25  	Store     store.Store
    26  	Transport transport.Transport
    27  	Stream    events.Stream
    28  }
    29  
    30  // LocalProfile returns a profile with local mDNS as the registry, HTTP as the broker, file as the store, and HTTP as the transport
    31  // It is used for local development and testing
    32  func LocalProfile() (Profile, error) {
    33  	stream, err := events.NewStream()
    34  	return Profile{
    35  		Registry:  registry.NewMDNSRegistry(),
    36  		Broker:    broker.NewHttpBroker(),
    37  		Store:     store.NewFileStore(),
    38  		Transport: transport.NewHTTPTransport(),
    39  		Stream:    stream,
    40  	}, err
    41  }
    42  
    43  // NatsProfile returns a profile with NATS as the registry, broker, store, and transport
    44  // It uses the environment variable MICR_NATS_ADDRESS to set the NATS server address
    45  // If the variable is not set, it defaults to nats://0.0.0.0:4222 which will connect to a local NATS server
    46  func NatsProfile() (Profile, error) {
    47  	addr := os.Getenv("MICRO_NATS_ADDRESS")
    48  	if addr == "" {
    49  		addr = "nats://0.0.0.0:4222"
    50  	}
    51  	// Split the address by comma, trim whitespace, and convert to a slice of strings
    52  	addrs := splitNatsAdressList(addr)
    53  
    54  	reg := nreg.NewNatsRegistry(registry.Addrs(addrs...))
    55  
    56  	nopts := natslib.GetDefaultOptions()
    57  	nopts.Servers = addrs
    58  	brok := nats.NewNatsBroker(broker.Addrs(addrs...), nats.Options(nopts))
    59  
    60  	st := nstore.NewStore(nstore.NatsOptions(natslib.Options{Servers: addrs}))
    61  	tx := ntx.NewTransport(ntx.Options(natslib.Options{Servers: addrs}))
    62  
    63  	stream, err := nevents.NewStream(
    64  		nevents.Address(addr),
    65  	)
    66  
    67  	registry.DefaultRegistry = reg
    68  	broker.DefaultBroker = brok
    69  	store.DefaultStore = st
    70  	transport.DefaultTransport = tx
    71  	return Profile{
    72  		Registry:  reg,
    73  		Broker:    brok,
    74  		Store:     st,
    75  		Transport: tx,
    76  		Stream:    stream,
    77  	}, err
    78  }
    79  
    80  func splitNatsAdressList(addr string) []string {
    81  	// Split the address by comma
    82  	addrs := strings.Split(addr, ",")
    83  	// Trim any whitespace from each address
    84  	for i, a := range addrs {
    85  		addrs[i] = strings.TrimSpace(a)
    86  	}
    87  	return addrs
    88  }
    89  
    90  // Add more profiles as needed, e.g. grpc