postnat

package module
v0.0.0-...-6fbf13c Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 9 Imported by: 0

README

postnat

Publish messages to NATS via Postgres' LISTEN/NOTIFY feature.

Why would I use this?

Let's assume you already have a postgresql backed application and you want to introduce a Messaging layer but don't have the resources to modify the system to introduce more dependencies or you just want to put it off for a bit more.

postnat helps you use the facilities of Postgres to publish messages to a NATS server with minimal changes to your code.

NOTIFY 'time_us_east', '<binary-or-json-message-here>';

What postnat does is basically listen to all registered patterns and publishes the payloads to the configured NATS server.

Usage

You can run it like so:

$ postnat --config "postnat.toml" run
CLI
Usage: postnat <command>

Publish messages to NATS from PostgreSQL LISTEN/NOTIFY messages

Flags:
  -h, --help                     Show context-sensitive help.
      --config="postnat.toml"    Location of configuration file
      --debug                    Enable debug mode
      --version                  Show version and quit

Commands:
  run    Start the postnat daemon

Run "postnat <command> --help" for more information on a command.
Configuration
[postgres]
host = "localhost"
port = 5432
database = "database"
username = "username"
password = "password"
sslmode = "disable"

[nats]
url = "nats://username:password@localhost:4222"
max_reconnects = 10

[topics]
listen_for = ["users", "users_id"]
# optional topic prefix
prefix = "app."
# This replaces the underscore when publishing to nats, e.g. emails_signup -> emails.signup
replace_underscore_with_dot = true

Building

First clone this repo:

$ git clone https://github.com/zikani03/postnat
$ cd postnat
Building from source
$ go build ./cmd/postnat.go
Docker, with docker compose

Create a configuration file named development.toml and update as appropriate, then run:

$ docker compose up

MIT LICENSE © Zikani Nyirenda Mwase

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

func New

func New(config *Config) (*App, error)

func (*App) Run

func (a *App) Run() chan bool

func (*App) Shutdown

func (a *App) Shutdown()

type Config

type Config struct {
	DB     PostgresConfig `toml:"postgres"`
	Nats   NatsConfig     `toml:"nats"`
	Topics TopicsConfig   `toml:"topics"`
}

func ParseConfig

func ParseConfig(path string) (*Config, error)

type NatsConfig

type NatsConfig struct {
	URL           string `toml:"url"`
	MaxReconnects int    `toml:"max_reconnects"`
}

type PostgresConfig

type PostgresConfig struct {
	Username string `toml:"username"`
	Password string `toml:"password"`
	Database string `toml:"database"`
	Host     string `toml:"host"`
	Port     int    `toml:"port"`
	SSLMode  string `toml:"sslmode"`
}

type TopicsConfig

type TopicsConfig struct {
	Prefix            string   `toml:"prefix"`
	ListenFor         []string `toml:"listen_for"`
	ReplaceUnderscore bool     `toml:"replace_underscore_with_dot"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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