natsx

package
v0.0.0-...-a807e99 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

README

Natswrap

natsx is a simple wrapper for nats.io client.

Usage

To start use the natsx package add import:

...
import (
  "git.ooo.ua/pub/armory/natsx"
)
...
  • Fill config structure:
Field Type Required
Host string +
Port int +
User string
Password string
  • Set config:
natsx.SetCfg(cfg)

Connect will be initialized at first try to push or subscribe a message

err := PublishMessage(topic, obj)
Example
package main

import (
    "fmt"
    "git.ooo.ua/pub/armory/natsx"
)

var config = natsx.Config{
    Host: "127.0.0.1",
    Port: 4222,
    User: "user",
    Password: "password",  
}

err := config.Validate()
if err != nil {
    fmt.Println('invalid nats configuration')
}

natsx.SetConfig(&config)

testMsg := []string {"1", "2"}
err := natsx.PublishMessage("Topic", testMsg)
if err != nil {
    log.Get().Error(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConn

func GetConn() (*nats.Conn, error)

GetConn returns nats.Conn singleton.

func PublishMessage

func PublishMessage(topic string, msg interface{}) error

PublishMessage serializes the message in JSON and sends to the topic.

func SetConfig

func SetConfig(config *Config)

SetConfig sets NATS configuration singleton.

func Subscribe

func Subscribe(topic string, msgs chan *nats.Msg) (*nats.Subscription, error)

Subscribe initiates chan subscription for topic.

Types

type Config

type Config struct {
	Host     string `yaml:"host" mapstructure:"nats.host"`
	Port     int    `yaml:"port" mapstructure:"nats.port"`
	User     string `yaml:"user" mapstructure:"nats.user"`
	Password string `yaml:"password" mapstructure:"nats.password"`
}

Config is configuration for the interaction with the NATS server.

func (Config) ToURL

func (config Config) ToURL() string

ToURL formats config into NATS connection string.

func (Config) Validate

func (config Config) Validate() error

Validate is an implementation of Validatable interface from ozzo-validation.

type Message

type Message struct {
	EventID string      `json:"eventId"`
	Result  string      `json:"result"`
	Msg     string      `json:"msg"`
	Details interface{} `json:"details"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL