Initial QSfera import

This commit is contained in:
Курнат Андрей
2026-06-07 10:20:04 +03:00
commit 2315f25754
16485 changed files with 4826827 additions and 0 deletions
@@ -0,0 +1,50 @@
package debug
import (
"context"
"github.com/qsfera/server/pkg/log"
"github.com/qsfera/server/services/nats/pkg/config"
)
// Option defines a single option function.
type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Logger log.Logger
Context context.Context
Config *config.Config
}
// newOptions initializes the available default options.
func newOptions(opts ...Option) Options {
opt := Options{}
for _, o := range opts {
o(&opt)
}
return opt
}
// Logger provides a function to set the logger option.
func Logger(val log.Logger) Option {
return func(o *Options) {
o.Logger = val
}
}
// Context provides a function to set the context option.
func Context(val context.Context) Option {
return func(o *Options) {
o.Context = val
}
}
// Config provides a function to set the config option.
func Config(val *config.Config) Option {
return func(o *Options) {
o.Config = val
}
}
@@ -0,0 +1,36 @@
package debug
import (
"net/http"
"strconv"
"github.com/qsfera/server/pkg/checks"
"github.com/qsfera/server/pkg/handlers"
"github.com/qsfera/server/pkg/service/debug"
"github.com/qsfera/server/pkg/version"
)
// Server initializes the debug service and server.
func Server(opts ...Option) (*http.Server, error) {
options := newOptions(opts...)
// For nats readiness and liveness checks are identical
// the nats server will neither be healthy nor ready when it can not reach the nats server/cluster
checkHandler := handlers.NewCheckHandler(
handlers.NewCheckHandlerConfiguration().
WithLogger(options.Logger).
WithCheck("nats reachability", checks.NewNatsCheck(options.Config.Nats.Host+":"+strconv.Itoa(options.Config.Nats.Port))),
)
return debug.NewService(
debug.Logger(options.Logger),
debug.Name(options.Config.Service.Name),
debug.Version(version.GetString()),
debug.Address(options.Config.Debug.Addr),
debug.Token(options.Config.Debug.Token),
debug.Pprof(options.Config.Debug.Pprof),
debug.Zpages(options.Config.Debug.Zpages),
debug.Health(checkHandler),
debug.Ready(checkHandler),
), nil
}
@@ -0,0 +1,51 @@
package nats
import (
"time"
nserver "github.com/nats-io/nats-server/v2/server"
)
var NATSListenAndServeLoopTimer = 1 * time.Second
type NATSServer struct {
server *nserver.Server
}
// NatsOption configures the new NATSServer instance
func NewNATSServer(logger nserver.Logger, opts ...NatsOption) (*NATSServer, error) {
natsOpts := &nserver.Options{}
for _, o := range opts {
o(natsOpts)
}
// enable JetStream
natsOpts.JetStream = true
// The NATS server itself runs the signal handling. We set `natsOpts.NoSigs = true` because we want to handle signals ourselves
natsOpts.NoSigs = true
server, err := nserver.NewServer(natsOpts)
if err != nil {
return nil, err
}
server.SetLoggerV2(logger, true, true, false)
return &NATSServer{
server: server,
}, nil
}
// ListenAndServe runs the NATSServer in a blocking way until the server is shutdown or an error occurs
func (n *NATSServer) ListenAndServe() (err error) {
n.server.Start() // it won't block
n.server.WaitForShutdown() // block until the server is fully shutdown
return nil
}
// Shutdown stops the NATSServer gracefully
func (n *NATSServer) Shutdown() {
n.server.Shutdown()
n.server.WaitForShutdown()
}
@@ -0,0 +1,52 @@
package nats
import (
"crypto/tls"
nserver "github.com/nats-io/nats-server/v2/server"
)
// NatsOption configures the nats server
type NatsOption func(*nserver.Options)
// Host sets the host URL for the nats server
func Host(url string) NatsOption {
return func(o *nserver.Options) {
o.Host = url
}
}
// Port sets the host URL for the nats server
func Port(port int) NatsOption {
return func(o *nserver.Options) {
o.Port = port
}
}
// ClusterID sets the name for the nats cluster
func ClusterID(clusterID string) NatsOption {
return func(o *nserver.Options) {
o.Cluster.Name = clusterID
}
}
// StoreDir sets the folder for persistence
func StoreDir(StoreDir string) NatsOption {
return func(o *nserver.Options) {
o.StoreDir = StoreDir
}
}
// TLSConfig sets the tls config for the nats server
func TLSConfig(c *tls.Config) NatsOption {
return func(o *nserver.Options) {
o.TLSConfig = c
}
}
// AllowNonTLS sets the allow non tls options for the nats server
func AllowNonTLS(v bool) NatsOption {
return func(o *nserver.Options) {
o.AllowNonTLS = v
}
}