natshttp

package module
v0.0.0-...-cfc5b22 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 13 Imported by: 0

README

nats-http-gateway

An open‑source alternative to the Synadia Cloud HTTP Gateway, exposing NATS functionality over a clean HTTP interface. The gateway currently supports two main API groups:

  • Core messaging API – publish, request/reply and server‑sent‑event subscription via the /nats/subjects/{subject} path.
  • Key‑Value store API – create, read, update and delete KV entries using the /kv/{bucket}/{key} path.
  • Object store API – create, read, update and delete ObjectStore entries using the /obj/{bucket}/{key} path.

Authentication is handled via command‑line flags when running the server binary:

nats-http-gateway \
    -s nats://demo.nats.io:4222          # NATS server URL (default: nats://127.0.0.1:4222)
    -creds path/to/user.creds            # optional user credentials file
    -nkey  path/to/seed.nk               # optional NKey seed file
    -token <jwt-token>                   # optional JWT token

The server reads the flags at startup and builds the appropriate NATS connection options.

Note: No built‑in role‑based access control is provided; use the NATS server's security features (credentials, NKey or token) to protect the gateway.

For full specification details, see the Synadia Cloud HTTP Gateway documentation.

Usage example (in Go)
import (
    "net/http"
    "github.com/gleez/nats-http-gateway"
    "github.com/nats-io/nats.go"
)

func main() {
    // Connect to NATS (flags can also be used when running the binary).
    nc, _ := nats.Connect(nats.DefaultURL)
    h := natshttp.New(nc)
    // Register the handler – the gateway routes internally based on the path.
    http.HandleFunc("/api/v1/", h.NatsHandler)
    http.ListenAndServe(":8080", nil)
}
Similar projects
  • hats – another NATS HTTP gateway implementation.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNatsMsg

func NewNatsMsg(subject, reply string, headers nats.Header, body []byte) *nats.Msg

NewNatsMsg creates a NATS message.

func WriteJSONError

func WriteJSONError(w http.ResponseWriter, statusCode int, message string)

WriteJSONError writes an error response as JSON.

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, r *http.Request, result interface{})

WriteJSONResponse writes a successful NATS response as JSON.

Types

type Error

type Error struct {
	Message string `json:"message"`
}

Error struct for JSON error responses.

type Handler

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

Handler provides HTTP to NATS bridging.

func New

func New(nc *nats.Conn) *Handler

func (*Handler) NatsHandler

func (h *Handler) NatsHandler(w http.ResponseWriter, r *http.Request)

NatsHandler routes HTTP requests to appropriate handlers based on path prefixes.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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