mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-26 13:48:53 +08:00

* Replace Nats with Nats Jestream For PubSub Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add Stream Description Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Subscribe using wildcard Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add consumers description Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Remove unused queue variable Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Add extra configs to stream Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * Use inline error handling Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * Fix connection leak in NATS publisher The publisher struct in pkg/messaging/nats/publisher.go was modified to include a new `conn` field of type `*broker.Conn`. This change was made to fix a connection leak issue in the NATS publisher. The `NewPublisher` function was updated to assign the `conn` parameter to the new `conn` field in the publisher struct. Additionally, the `Close` method in the publisher struct was modified to close the `conn` connection. This commit fixes the connection leak issue in the NATS publisher and ensures that connections are properly closed. Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> * feat(messaging): Add support for durable consumers This commit adds support for durable consumers to NATS JS in the messaging package. To support this functionality, the `strings` package has been imported in the `pubsub.go` file to check the topic. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat : remove internal logic to keep subscribers Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(messaging): add function to format consumer name This commit adds a new function to the `pubsub` package in the `messaging` module. The function is called `formatConsumerName` and is used to generate a consumer name for NATS messaging. The function takes two parameters, `topic` and `id`, and returns a formatted consumer name. The consumer name is generated by concatenating the `topic` and `id` parameters, with some restrictions on the characters that can be used. This function will be useful for creating durable subscriptions in NATS messaging. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: rodneyosodo <blackd0t@protonmail.com> Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
151 lines
4.4 KiB
Go
151 lines
4.4 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package main contains coap-adapter main function to start the coap-adapter service.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
chclient "github.com/mainflux/callhome/pkg/client"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/coap"
|
|
"github.com/mainflux/mainflux/coap/api"
|
|
"github.com/mainflux/mainflux/coap/tracing"
|
|
"github.com/mainflux/mainflux/internal"
|
|
authapi "github.com/mainflux/mainflux/internal/clients/grpc/auth"
|
|
jaegerclient "github.com/mainflux/mainflux/internal/clients/jaeger"
|
|
"github.com/mainflux/mainflux/internal/env"
|
|
"github.com/mainflux/mainflux/internal/server"
|
|
coapserver "github.com/mainflux/mainflux/internal/server/coap"
|
|
httpserver "github.com/mainflux/mainflux/internal/server/http"
|
|
mflog "github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/pkg/messaging/brokers"
|
|
brokerstracing "github.com/mainflux/mainflux/pkg/messaging/brokers/tracing"
|
|
"github.com/mainflux/mainflux/pkg/uuid"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
const (
|
|
svcName = "coap_adapter"
|
|
envPrefix = "MF_COAP_ADAPTER_"
|
|
envPrefixHTTP = "MF_COAP_ADAPTER_HTTP_"
|
|
defSvcHTTPPort = "5683"
|
|
defSvcCoAPPort = "5683"
|
|
)
|
|
|
|
type config struct {
|
|
LogLevel string `env:"MF_COAP_ADAPTER_LOG_LEVEL" envDefault:"info"`
|
|
BrokerURL string `env:"MF_BROKER_URL" envDefault:"nats://localhost:4222"`
|
|
JaegerURL string `env:"MF_JAEGER_URL" envDefault:"http://jaeger:14268/api/traces"`
|
|
SendTelemetry bool `env:"MF_SEND_TELEMETRY" envDefault:"true"`
|
|
InstanceID string `env:"MF_COAP_ADAPTER_INSTANCE_ID" envDefault:""`
|
|
}
|
|
|
|
func main() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
|
cfg := config{}
|
|
if err := env.Parse(&cfg); err != nil {
|
|
log.Fatalf("failed to load %s configuration : %s", svcName, err)
|
|
}
|
|
|
|
logger, err := mflog.New(os.Stdout, cfg.LogLevel)
|
|
if err != nil {
|
|
log.Fatalf("failed to init logger: %s", err)
|
|
}
|
|
|
|
var exitCode int
|
|
defer mflog.ExitWithError(&exitCode)
|
|
|
|
if cfg.InstanceID == "" {
|
|
if cfg.InstanceID, err = uuid.New().ID(); err != nil {
|
|
logger.Error(fmt.Sprintf("failed to generate instanceID: %s", err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
}
|
|
|
|
httpServerConfig := server.Config{Port: defSvcHTTPPort}
|
|
if err := env.Parse(&httpServerConfig, env.Options{Prefix: envPrefixHTTP}); err != nil {
|
|
logger.Error(fmt.Sprintf("failed to load %s HTTP server configuration : %s", svcName, err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
coapServerConfig := server.Config{Port: defSvcCoAPPort}
|
|
if err := env.Parse(&coapServerConfig, env.Options{Prefix: envPrefix}); err != nil {
|
|
logger.Error(fmt.Sprintf("failed to load %s CoAP server configuration : %s", svcName, err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
|
|
auth, aHandler, err := authapi.SetupAuthz(svcName)
|
|
if err != nil {
|
|
logger.Error(err.Error())
|
|
exitCode = 1
|
|
return
|
|
}
|
|
defer aHandler.Close()
|
|
|
|
logger.Info("Successfully connected to things grpc server " + aHandler.Secure())
|
|
|
|
tp, err := jaegerclient.NewProvider(svcName, cfg.JaegerURL, cfg.InstanceID)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to init Jaeger: %s", err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
defer func() {
|
|
if err := tp.Shutdown(ctx); err != nil {
|
|
logger.Error(fmt.Sprintf("Error shutting down tracer provider: %v", err))
|
|
}
|
|
}()
|
|
tracer := tp.Tracer(svcName)
|
|
|
|
nps, err := brokers.NewPubSub(ctx, cfg.BrokerURL, logger)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("failed to connect to message broker: %s", err))
|
|
exitCode = 1
|
|
return
|
|
}
|
|
defer nps.Close()
|
|
nps = brokerstracing.NewPubSub(coapServerConfig, tracer, nps)
|
|
|
|
svc := coap.New(auth, nps)
|
|
|
|
svc = tracing.New(tracer, svc)
|
|
|
|
svc = api.LoggingMiddleware(svc, logger)
|
|
|
|
counter, latency := internal.MakeMetrics(svcName, "api")
|
|
svc = api.MetricsMiddleware(svc, counter, latency)
|
|
|
|
hs := httpserver.New(ctx, cancel, svcName, httpServerConfig, api.MakeHandler(cfg.InstanceID), logger)
|
|
|
|
cs := coapserver.New(ctx, cancel, svcName, coapServerConfig, api.MakeCoAPHandler(svc, logger), logger)
|
|
|
|
if cfg.SendTelemetry {
|
|
chc := chclient.New(svcName, mainflux.Version, logger, cancel)
|
|
go chc.CallHome(ctx)
|
|
}
|
|
|
|
g.Go(func() error {
|
|
return hs.Start()
|
|
})
|
|
g.Go(func() error {
|
|
return cs.Start()
|
|
})
|
|
g.Go(func() error {
|
|
return server.StopSignalHandler(ctx, cancel, logger, svcName, hs, cs)
|
|
})
|
|
|
|
if err := g.Wait(); err != nil {
|
|
logger.Error(fmt.Sprintf("CoAP adapter service terminated: %s", err))
|
|
}
|
|
}
|