2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 19:15:48 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-08 19:38:34 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-07-18 21:01:09 +08:00
|
|
|
"io"
|
2019-07-23 20:31:02 +08:00
|
|
|
"io/ioutil"
|
2018-09-27 00:58:51 +08:00
|
|
|
"log"
|
2018-08-08 19:38:34 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-11-20 19:52:28 +08:00
|
|
|
"strconv"
|
2018-08-08 19:38:34 +08:00
|
|
|
"syscall"
|
2019-07-18 21:01:09 +08:00
|
|
|
"time"
|
2018-08-08 19:38:34 +08:00
|
|
|
|
|
|
|
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
|
|
|
|
"github.com/mainflux/mainflux"
|
2018-09-27 00:58:51 +08:00
|
|
|
"github.com/mainflux/mainflux/logger"
|
2018-08-08 19:38:34 +08:00
|
|
|
"github.com/mainflux/mainflux/readers"
|
|
|
|
"github.com/mainflux/mainflux/readers/api"
|
|
|
|
"github.com/mainflux/mainflux/readers/mongodb"
|
2019-07-04 23:06:56 +08:00
|
|
|
thingsapi "github.com/mainflux/mainflux/things/api/auth/grpc"
|
2019-07-18 21:01:09 +08:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2018-08-08 19:38:34 +08:00
|
|
|
stdprometheus "github.com/prometheus/client_golang/prometheus"
|
2019-07-18 21:01:09 +08:00
|
|
|
jconfig "github.com/uber/jaeger-client-go/config"
|
2019-04-16 20:58:56 +08:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2018-08-08 19:38:34 +08:00
|
|
|
"google.golang.org/grpc"
|
2018-11-20 19:52:28 +08:00
|
|
|
"google.golang.org/grpc/credentials"
|
2018-08-08 19:38:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-04-07 18:02:17 +08:00
|
|
|
defLogLevel = "error"
|
|
|
|
defPort = "8180"
|
2020-06-04 02:33:52 +08:00
|
|
|
defDB = "mainflux"
|
2020-04-07 18:02:17 +08:00
|
|
|
defDBHost = "localhost"
|
|
|
|
defDBPort = "27017"
|
|
|
|
defClientTLS = "false"
|
|
|
|
defCACerts = ""
|
|
|
|
defServerCert = ""
|
|
|
|
defServerKey = ""
|
|
|
|
defJaegerURL = ""
|
|
|
|
defThingsAuthURL = "localhost:8181"
|
2020-06-09 00:17:16 +08:00
|
|
|
defThingsAuthTimeout = "1s"
|
2020-04-07 18:02:17 +08:00
|
|
|
|
|
|
|
envLogLevel = "MF_MONGO_READER_LOG_LEVEL"
|
|
|
|
envPort = "MF_MONGO_READER_PORT"
|
|
|
|
envDB = "MF_MONGO_READER_DB"
|
|
|
|
envDBHost = "MF_MONGO_READER_DB_HOST"
|
|
|
|
envDBPort = "MF_MONGO_READER_DB_PORT"
|
|
|
|
envClientTLS = "MF_MONGO_READER_CLIENT_TLS"
|
|
|
|
envCACerts = "MF_MONGO_READER_CA_CERTS"
|
|
|
|
envServerCert = "MF_MONGO_READER_SERVER_CERT"
|
|
|
|
envServerKey = "MF_MONGO_READER_SERVER_KEY"
|
|
|
|
envJaegerURL = "MF_JAEGER_URL"
|
2020-06-05 23:39:22 +08:00
|
|
|
envThingsAuthURL = "MF_THINGS_AUTH_GRPC_URL"
|
2020-04-07 18:02:17 +08:00
|
|
|
envThingsAuthTimeout = "MF_THINGS_AUTH_GRPC_TIMEOUT"
|
2018-08-08 19:38:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2020-04-07 18:02:17 +08:00
|
|
|
logLevel string
|
|
|
|
port string
|
|
|
|
dbName string
|
|
|
|
dbHost string
|
|
|
|
dbPort string
|
|
|
|
clientTLS bool
|
|
|
|
caCerts string
|
|
|
|
serverCert string
|
|
|
|
serverKey string
|
|
|
|
jaegerURL string
|
|
|
|
thingsAuthURL string
|
|
|
|
thingsAuthTimeout time.Duration
|
2018-08-08 19:38:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cfg := loadConfigs()
|
2018-09-27 00:58:51 +08:00
|
|
|
logger, err := logger.New(os.Stdout, cfg.logLevel)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2018-11-20 19:52:28 +08:00
|
|
|
|
|
|
|
conn := connectToThings(cfg, logger)
|
2018-08-08 19:38:34 +08:00
|
|
|
defer conn.Close()
|
|
|
|
|
2019-07-18 21:01:09 +08:00
|
|
|
thingsTracer, thingsCloser := initJaeger("things", cfg.jaegerURL, logger)
|
|
|
|
defer thingsCloser.Close()
|
|
|
|
|
2020-04-07 18:02:17 +08:00
|
|
|
tc := thingsapi.NewClient(conn, thingsTracer, cfg.thingsAuthTimeout)
|
2018-08-08 19:38:34 +08:00
|
|
|
|
|
|
|
db := connectToMongoDB(cfg.dbHost, cfg.dbPort, cfg.dbName, logger)
|
|
|
|
|
|
|
|
repo := newService(db, logger)
|
|
|
|
|
|
|
|
errs := make(chan error, 2)
|
|
|
|
go func() {
|
|
|
|
c := make(chan os.Signal)
|
|
|
|
signal.Notify(c, syscall.SIGINT)
|
|
|
|
errs <- fmt.Errorf("%s", <-c)
|
|
|
|
}()
|
|
|
|
|
2020-02-06 03:24:36 +08:00
|
|
|
go startHTTPServer(repo, tc, cfg, logger, errs)
|
2018-08-08 19:38:34 +08:00
|
|
|
|
2018-09-27 00:58:51 +08:00
|
|
|
err = <-errs
|
2018-08-08 19:38:34 +08:00
|
|
|
logger.Error(fmt.Sprintf("MongoDB reader service terminated: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfigs() config {
|
2018-11-20 19:52:28 +08:00
|
|
|
tls, err := strconv.ParseBool(mainflux.Env(envClientTLS, defClientTLS))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Invalid value passed for %s\n", envClientTLS)
|
|
|
|
}
|
|
|
|
|
2020-06-09 00:17:16 +08:00
|
|
|
authTimeout, err := time.ParseDuration(mainflux.Env(envThingsAuthTimeout, defThingsAuthTimeout))
|
2019-07-18 21:01:09 +08:00
|
|
|
if err != nil {
|
2020-04-07 18:02:17 +08:00
|
|
|
log.Fatalf("Invalid %s value: %s", envThingsAuthTimeout, err.Error())
|
2019-07-18 21:01:09 +08:00
|
|
|
}
|
|
|
|
|
2018-08-08 19:38:34 +08:00
|
|
|
return config{
|
2020-04-07 18:02:17 +08:00
|
|
|
logLevel: mainflux.Env(envLogLevel, defLogLevel),
|
|
|
|
port: mainflux.Env(envPort, defPort),
|
|
|
|
dbName: mainflux.Env(envDB, defDB),
|
|
|
|
dbHost: mainflux.Env(envDBHost, defDBHost),
|
|
|
|
dbPort: mainflux.Env(envDBPort, defDBPort),
|
|
|
|
clientTLS: tls,
|
|
|
|
caCerts: mainflux.Env(envCACerts, defCACerts),
|
|
|
|
serverCert: mainflux.Env(envServerCert, defServerCert),
|
|
|
|
serverKey: mainflux.Env(envServerKey, defServerKey),
|
|
|
|
jaegerURL: mainflux.Env(envJaegerURL, defJaegerURL),
|
|
|
|
thingsAuthURL: mainflux.Env(envThingsAuthURL, defThingsAuthURL),
|
2020-06-09 00:17:16 +08:00
|
|
|
thingsAuthTimeout: authTimeout,
|
2018-08-08 19:38:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:58:51 +08:00
|
|
|
func connectToMongoDB(host, port, name string, logger logger.Logger) *mongo.Database {
|
2019-04-16 20:58:56 +08:00
|
|
|
addr := fmt.Sprintf("mongodb://%s:%s", host, port)
|
|
|
|
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(addr))
|
2018-08-08 19:38:34 +08:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(fmt.Sprintf("Failed to connect to database: %s", err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Database(name)
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:01:09 +08:00
|
|
|
func initJaeger(svcName, url string, logger logger.Logger) (opentracing.Tracer, io.Closer) {
|
2019-07-23 20:31:02 +08:00
|
|
|
if url == "" {
|
|
|
|
return opentracing.NoopTracer{}, ioutil.NopCloser(nil)
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:01:09 +08:00
|
|
|
tracer, closer, err := jconfig.Configuration{
|
|
|
|
ServiceName: svcName,
|
|
|
|
Sampler: &jconfig.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
Reporter: &jconfig.ReporterConfig{
|
|
|
|
LocalAgentHostPort: url,
|
|
|
|
LogSpans: true,
|
|
|
|
},
|
|
|
|
}.NewTracer()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(fmt.Sprintf("Failed to init Jaeger client: %s", err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracer, closer
|
|
|
|
}
|
|
|
|
|
2018-11-20 19:52:28 +08:00
|
|
|
func connectToThings(cfg config, logger logger.Logger) *grpc.ClientConn {
|
|
|
|
var opts []grpc.DialOption
|
|
|
|
if cfg.clientTLS {
|
|
|
|
if cfg.caCerts != "" {
|
|
|
|
tpc, err := credentials.NewClientTLSFromFile(cfg.caCerts, "")
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(fmt.Sprintf("Failed to load certs: %s", err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
opts = append(opts, grpc.WithTransportCredentials(tpc))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Info("gRPC communication is not encrypted")
|
|
|
|
opts = append(opts, grpc.WithInsecure())
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:02:17 +08:00
|
|
|
conn, err := grpc.Dial(cfg.thingsAuthURL, opts...)
|
2018-08-08 19:38:34 +08:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(fmt.Sprintf("Failed to connect to things service: %s", err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2018-09-27 00:58:51 +08:00
|
|
|
func newService(db *mongo.Database, logger logger.Logger) readers.MessageRepository {
|
2018-08-08 19:38:34 +08:00
|
|
|
repo := mongodb.New(db)
|
|
|
|
repo = api.LoggingMiddleware(repo, logger)
|
|
|
|
repo = api.MetricsMiddleware(
|
|
|
|
repo,
|
|
|
|
kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
|
|
|
|
Namespace: "mongodb",
|
|
|
|
Subsystem: "message_reader",
|
|
|
|
Name: "request_count",
|
|
|
|
Help: "Number of requests received.",
|
|
|
|
}, []string{"method"}),
|
|
|
|
kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
|
|
|
|
Namespace: "mongodb",
|
|
|
|
Subsystem: "message_reader",
|
|
|
|
Name: "request_latency_microseconds",
|
|
|
|
Help: "Total duration of requests in microseconds.",
|
|
|
|
}, []string{"method"}),
|
|
|
|
)
|
|
|
|
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:24:36 +08:00
|
|
|
func startHTTPServer(repo readers.MessageRepository, tc mainflux.ThingsServiceClient, cfg config, logger logger.Logger, errs chan error) {
|
|
|
|
p := fmt.Sprintf(":%s", cfg.port)
|
|
|
|
if cfg.serverCert != "" || cfg.serverKey != "" {
|
|
|
|
logger.Info(fmt.Sprintf("Mongo reader service started using https on port %s with cert %s key %s",
|
|
|
|
cfg.port, cfg.serverCert, cfg.serverKey))
|
|
|
|
errs <- http.ListenAndServeTLS(p, cfg.serverCert, cfg.serverKey, api.MakeHandler(repo, tc, "mongodb-reader"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Info(fmt.Sprintf("Mongo reader service started, exposed port %s", cfg.port))
|
2019-02-12 18:36:58 +08:00
|
|
|
errs <- http.ListenAndServe(p, api.MakeHandler(repo, tc, "mongodb-reader"))
|
2018-08-08 19:38:34 +08:00
|
|
|
}
|