Skip to content

server

import "github.com/danmestas/dagnats/server"

Embedded NATS server with the full DagNats stack: engine, API, bridge, and trigger runtime. Provides programmatic startup for single-binary deployment.

Key Types

TypeDescription
ConfigServer configuration: data directory, ports, leaf node remotes, store limits
ServerThe assembled server: embedded NATS, engine, HTTP API, bridge

Key Functions

FunctionDescription
New(cfg)Creates a new server from configuration. It does not start anything.
DefaultConfig()Built-in defaults
ConfigFromEnv()Loads configuration from dagnats.yaml and environment variables
EmbeddedWorker(srv)Returns a *WorkerShim to register handlers on before Run()
(*Server).Run()Starts NATS, the engine, API, bridge and triggers, and blocks until shutdown
(*Server).HTTPAddr()The bound HTTP address once Run has finished starting

Configuration Sources

Configuration is resolved in order (later sources override):

  1. Built-in defaults
  2. dagnats.yaml in the current directory
  3. Environment variables
Environment VariableConfig FieldDefault
DAGNATS_DATA_DIRDataDir~/Library/Application Support/dagnats on macOS, $XDG_DATA_HOME/dagnats or ~/.local/share/dagnats on Linux
DAGNATS_HTTP_ADDRHTTPAddr127.0.0.1:8080
DAGNATS_NATS_PORTNATSPort4222
DAGNATS_LEAF_REMOTESLeafRemotes(none)
DAGNATS_LEAF_CREDENTIALSLeafCredentials(none)
DAGNATS_MAX_STORE_BYTESMaxStoreBytesderived from free disk space
DAGNATS_MAX_MEMORY_BYTESMaxMemoryBytes1 GiB
DAGNATS_MONITOR_PORTMonitorPort(disabled)

The server has more settings (cluster, WebSocket, limits). See the configuration reference for the full list.

Server Lifecycle

  1. New(cfg) builds the server object
  2. Optionally register embedded workers with EmbeddedWorker(srv)
  3. Run() starts embedded NATS, the engine, API, bridge, triggers and your workers, then blocks until SIGINT/SIGTERM
  4. Graceful shutdown stops all components

Usage

cfg := server.ConfigFromEnv()
srv := server.New(cfg)

// Optional: register embedded workers
w := server.EmbeddedWorker(srv)
w.Handle("process", myHandler)

// Blocks until signal
if err := srv.Run(); err != nil {
    log.Fatal(err)
}