feat: auto-start embedded NATS server in New()

Pub/sub now works out of the box — New() starts a process-scoped
embedded NATS server with JetStream. The PubSub interface remains
for custom backends via Config(Options{PubSub: ...}).

- Move vianats functionality into nats.go (eliminates circular import)
- Add NATSConn(), JetStream(), EnsureStream(), ReplayHistory[T]() to via
- Delete vianats/ package
- Simplify nats-chatroom and pubsub-crud examples
- Rewrite pubsub tests to use real embedded NATS
This commit is contained in:
Ryan Hamamura
2026-02-12 08:54:44 -10:00
parent 10b4838f8d
commit 2310e45d35
9 changed files with 256 additions and 261 deletions

12
via.go
View File

@@ -49,6 +49,7 @@ type V struct {
devModePageInitFnMap map[string]func(*Context)
sessionManager *scs.SessionManager
pubsub PubSub
defaultNATS *defaultNATS
actionRateLimit RateLimitConfig
datastarPath string
datastarContent []byte
@@ -130,6 +131,7 @@ func (v *V) Config(cfg Options) {
v.datastarPath = cfg.DatastarPath
}
if cfg.PubSub != nil {
v.defaultNATS = nil
v.pubsub = cfg.PubSub
}
if cfg.ContextTTL != 0 {
@@ -379,6 +381,7 @@ func (v *V) Shutdown() {
v.logErr(nil, "pubsub close error: %v", err)
}
}
v.defaultNATS = nil
v.logInfo(nil, "shutdown complete")
}
@@ -725,6 +728,15 @@ func New() *V {
v.logDebug(c, "session close event triggered")
v.cleanupCtx(c)
})
dn, err := getSharedNATS()
if err != nil {
v.logWarn(nil, "embedded NATS unavailable: %v", err)
} else {
v.defaultNATS = dn
v.pubsub = &natsRef{dn: dn}
}
return v
}