caddynarun

package
v0.0.0-...-5d06168 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 19 Imported by: 0

README

Build Caddy with the Plugin

xcaddy build --with github.com/akhenakh/narun/caddynarun@latest

# local
xcaddy build  --with github.com/caddy-dns/cloudflare --with github.com/akhenakh/narun/caddynarun --with  github.com/protomaps/go-pmtiles/caddy --replace github.com/akhenakh/narun=.

This will create a custom caddy binary in your current directory.

Example Caddyfile

Create a Caddyfile like this:

{
	# Optional: Global NATS settings if needed elsewhere, but narun config is self-contained
	# admin off # Disable admin API if not needed
	# log {
	#  	level DEBUG # For development
	# }
}

http://localhost:2080 {
	# Optional: Enable debug logging for this site
	log {
	 	output stderr
	 	level DEBUG
	}

	# Route specific paths through narun
	route /hello/* {
		narun {
			nats_url localhost:4222 # Or nats://localhost:4222
			request_timeout 15s
			nats_stream TASK      # Matches the stream name from narun config/consumer

			# Define routes for this narun instance
			# route <path> <app> [METHOD...]
			route /hello/ hello POST PUT # Handle POST/PUT for /hello/ -> app 'hello'
			# Add more routes here if this instance handles multiple apps/paths
		}
	}

	# Another route example
	route /goodbye {
		narun {
			nats_url localhost:4222
			request_timeout 10s
			nats_stream TASK # Could potentially be a different stream if needed

			route /goodbye goodbye GET # Handle GET for /goodbye -> app 'goodbye'
		}
	}

	# Fallback or other handlers
	route /* {
		respond "Not handled by narun" 404
	}
}

Run

  1. Ensure your NATS server
  2. Run your narun consumer(s):
    # Assuming narun/consumers/cmd/hello is built
    ./narun/consumers/cmd/hello/hello -nats-url "nats://localhost:4222" -stream "TASK" -app "hello"
    # Add other consumers (e.g., for 'goodbye' if you implement one)
    
  3. Run the custom Caddy build with your Caddyfile:
    ./caddy run
    
  4. Send requests:
    # Should hit the 'hello' consumer via narun handler
    curl -v -X POST -H "Content-Type: application/json" -d '{"name":"Caddy User"}' http://localhost:8080/hello/
    
    # Should hit the 'goodbye' consumer via narun handler (if you run one)
    curl -v http://localhost:8080/goodbye
    
    # Should hit the fallback handler
    curl -v http://localhost:8080/other/path
    

You should see logs in Caddy, the NATS server, and your consumer indicating the request flow. The response from the consumer should be returned to the curl client.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	// Configuration
	NatsURL        string         `json:"nats_url,omitempty"`
	NKeySeedFile   string         `json:"nkey_seed_file,omitempty"` // Optional NKey seed
	RequestTimeout caddy.Duration `json:"request_timeout,omitempty"`
	Service        string         `json:"service,omitempty"` // Target NATS service name
	// contains filtered or unexported fields
}

Handler implements the Caddy HTTP handler for narun integration using NATS Micro.

func (*Handler) CaddyModule

func (*Handler) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*Handler) Cleanup

func (h *Handler) Cleanup() error

Cleanup closes the NATS connection.

func (*Handler) Provision

func (h *Handler) Provision(ctx caddy.Context) error

Provision sets up the handler instance. Connects to NATS, validates config.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

ServeHTTP handles the incoming HTTP request, routing it via NATS Micro.

func (*Handler) UnmarshalCaddyfile

func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile sets up the handler from Caddyfile tokens.

func (*Handler) Validate

func (h *Handler) Validate() error

Validate ensures the handler is correctly configured.

type SlogWrapper

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

func NewSlogWrapper

func NewSlogWrapper(zapLogger *zap.Logger) *SlogWrapper

NewSlogWrapper creates a slog-compatible wrapper around a zap logger.

func (*SlogWrapper) Debug

func (s *SlogWrapper) Debug(msg string, args ...any)

Debug implements the slog interface.

func (*SlogWrapper) Error

func (s *SlogWrapper) Error(msg string, args ...any)

Error implements the slog interface.

func (*SlogWrapper) Info

func (s *SlogWrapper) Info(msg string, args ...any)

Info implements the slog interface.

func (*SlogWrapper) Warn

func (s *SlogWrapper) Warn(msg string, args ...any)

Warn implements the slog interface.

type ZapSlogHandler

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

ZapSlogHandler implements slog.Handler by writing records to a zap.Logger.

func NewZapSlogHandler

func NewZapSlogHandler(logger *zap.Logger, level slog.Leveler) *ZapSlogHandler

NewZapSlogHandler creates a handler that writes slog records using zap. It skips 2 caller frames by default (New + Handle) to show the correct source location.

func (*ZapSlogHandler) Enabled

func (h *ZapSlogHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled implements slog.Handler.

func (*ZapSlogHandler) Handle

func (h *ZapSlogHandler) Handle(_ context.Context, record slog.Record) error

Handle implements slog.Handler.

func (*ZapSlogHandler) WithAttrs

func (h *ZapSlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler.

func (*ZapSlogHandler) WithGroup

func (h *ZapSlogHandler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.

Jump to

Keyboard shortcuts

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