154 lines
3.6 KiB
Go
154 lines
3.6 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strconv"
|
|
"syscall"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
|
|
"github.com/mainflux/mainflux"
|
|
adapter "github.com/mainflux/mainflux/http"
|
|
"github.com/mainflux/mainflux/http/api"
|
|
"github.com/mainflux/mainflux/http/nats"
|
|
"github.com/mainflux/mainflux/logger"
|
|
thingsapi "github.com/mainflux/mainflux/things/api/grpc"
|
|
broker "github.com/nats-io/go-nats"
|
|
stdprometheus "github.com/prometheus/client_golang/prometheus"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
const (
|
|
defClientTLS = "false"
|
|
defCACerts = ""
|
|
defPort = "8180"
|
|
defLogLevel = "error"
|
|
defNatsURL = broker.DefaultURL
|
|
defThingsURL = "localhost:8181"
|
|
envClientTLS = "MF_HTTP_ADAPTER_CLIENT_TLS"
|
|
envCACerts = "MF_HTTP_ADAPTER_CA_CERTS"
|
|
envPort = "MF_HTTP_ADAPTER_PORT"
|
|
envLogLevel = "MF_HTTP_ADAPTER_LOG_LEVEL"
|
|
envNatsURL = "MF_NATS_URL"
|
|
envThingsURL = "MF_THINGS_URL"
|
|
)
|
|
|
|
type config struct {
|
|
thingsURL string
|
|
natsURL string
|
|
logLevel string
|
|
port string
|
|
clientTLS bool
|
|
caCerts string
|
|
}
|
|
|
|
func main() {
|
|
|
|
cfg := loadConfig()
|
|
|
|
logger, err := logger.New(os.Stdout, cfg.logLevel)
|
|
if err != nil {
|
|
log.Fatalf(err.Error())
|
|
}
|
|
|
|
nc, err := broker.Connect(cfg.natsURL)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to connect to NATS: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
defer nc.Close()
|
|
|
|
conn := connectToThings(cfg, logger)
|
|
defer conn.Close()
|
|
|
|
cc := thingsapi.NewClient(conn)
|
|
pub := nats.NewMessagePublisher(nc)
|
|
|
|
svc := adapter.New(pub)
|
|
svc = api.LoggingMiddleware(svc, logger)
|
|
svc = api.MetricsMiddleware(
|
|
svc,
|
|
kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{
|
|
Namespace: "http_adapter",
|
|
Subsystem: "api",
|
|
Name: "request_count",
|
|
Help: "Number of requests received.",
|
|
}, []string{"method"}),
|
|
kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
|
|
Namespace: "http_adapter",
|
|
Subsystem: "api",
|
|
Name: "request_latency_microseconds",
|
|
Help: "Total duration of requests in microseconds.",
|
|
}, []string{"method"}),
|
|
)
|
|
|
|
errs := make(chan error, 2)
|
|
|
|
go func() {
|
|
p := fmt.Sprintf(":%s", cfg.port)
|
|
logger.Info(fmt.Sprintf("HTTP adapter service started on port %s", cfg.port))
|
|
errs <- http.ListenAndServe(p, api.MakeHandler(svc, cc))
|
|
}()
|
|
|
|
go func() {
|
|
c := make(chan os.Signal)
|
|
signal.Notify(c, syscall.SIGINT)
|
|
errs <- fmt.Errorf("%s", <-c)
|
|
}()
|
|
|
|
err = <-errs
|
|
logger.Error(fmt.Sprintf("HTTP adapter terminated: %s", err))
|
|
}
|
|
|
|
func loadConfig() config {
|
|
tls, err := strconv.ParseBool(mainflux.Env(envClientTLS, defClientTLS))
|
|
if err != nil {
|
|
log.Fatalf("Invalid value passed for %s\n", envClientTLS)
|
|
}
|
|
|
|
return config{
|
|
thingsURL: mainflux.Env(envThingsURL, defThingsURL),
|
|
natsURL: mainflux.Env(envNatsURL, defNatsURL),
|
|
logLevel: mainflux.Env(envLogLevel, defLogLevel),
|
|
port: mainflux.Env(envPort, defPort),
|
|
clientTLS: tls,
|
|
caCerts: mainflux.Env(envCACerts, defCACerts),
|
|
}
|
|
}
|
|
|
|
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())
|
|
}
|
|
|
|
conn, err := grpc.Dial(cfg.thingsURL, opts...)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Failed to connect to things service: %s", err))
|
|
os.Exit(1)
|
|
}
|
|
return conn
|
|
}
|