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
ConfigFromEnv()Loads configuration from dagnats.yaml and environment variables
EmbeddedWorker(srv)Returns a *worker.Worker connected to the embedded server’s NATS

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/tmp/dagnats
DAGNATS_HTTP_ADDRHTTPAddr:8080
DAGNATS_NATS_PORTNATSPort4222
DAGNATS_LEAF_REMOTESLeafRemotes(none)
DAGNATS_LEAF_CREDENTIALSLeafCredentials(none)
DAGNATS_MAX_STORE_BYTESMaxStoreBytes1073741824 (1 GiB)
DAGNATS_MONITOR_PORTMonitorPort(disabled)

Server Lifecycle

  1. New(cfg) creates the server and starts the embedded NATS instance
  2. Optionally attach embedded workers with EmbeddedWorker(srv)
  3. Run() starts the engine, API, bridge, and blocks until SIGINT/SIGTERM
  4. Graceful shutdown stops all components in reverse order

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)
}