Exemplo n.º 1
0
/*
	cliOpts    : extra options
	configFile : gnatsd config file
	routes     : cluster members
*/
func NewGnatsdServer(cliOpts server.Options, configFile string, routeHosts []string, log server.Logger) (s *server.Server, opts *server.Options, err error) {
	log.Noticef("gnatsd config file: %s\n", configFile)
	// Configure options based on config file
	if opts, err = configureServerOptions(&cliOpts, configFile, routeHosts); err != nil {
		return
	}
	log.Debugf("Adding routes: %v\n", opts.Routes)
	// Create the server with appropriate options.
	s = server.New(opts)

	// Configure the authentication mechanism
	configureAuth(s, opts)

	s.SetLogger(log, opts.Debug, opts.Trace)

	return
}
Exemplo n.º 2
0
func NewNatsClient(servers []string, log server.Logger) (nclient *NatsClient, err error) {
	opts := nats.DefaultOptions
	opts.Servers = servers

	nclient = &NatsClient{log: log}

	if nclient.conn, err = opts.Connect(); err != nil {
		return
	}

	log.Noticef("nats client connected to: %v!\n", nclient.conn.ConnectedUrl())

	nclient.conn.Opts.ReconnectedCB = func(nc *nats.Conn) {
		log.Noticef("nats client reconnected to: %v!\n", nc.ConnectedUrl())
	}

	nclient.conn.Opts.DisconnectedCB = func(_ *nats.Conn) {
		log.Noticef("nats client disconnected!\n")
	}

	nclient.encodedConn, err = nats.NewEncodedConn(nclient.conn, nats.JSON_ENCODER)

	return
}