package main import ( "fmt" "net/http" "os" "os/signal" "syscall" "github.com/go-kit/kit/log" kitprometheus "github.com/go-kit/kit/metrics/prometheus" adapter "github.com/mainflux/mainflux/http" "github.com/mainflux/mainflux/http/api" "github.com/mainflux/mainflux/http/nats" broker "github.com/nats-io/go-nats" stdprometheus "github.com/prometheus/client_golang/prometheus" ) const ( port int = 7070 defNatsURL string = broker.DefaultURL envNatsURL string = "HTTP_ADAPTER_NATS_URL" ) type config struct { Port int NatsURL string } func main() { cfg := config{ Port: port, NatsURL: env(envNatsURL, defNatsURL), } logger := log.NewJSONLogger(log.NewSyncWriter(os.Stdout)) logger = log.With(logger, "ts", log.DefaultTimestampUTC) nc, err := broker.Connect(cfg.NatsURL) if err != nil { status := fmt.Sprintf("Cannot connect to NATS due to %s", err.Error()) logger.Log("status", status) os.Exit(1) } defer nc.Close() repo := nats.NewMessageRepository(nc) svc := adapter.NewService(repo) svc = api.NewLoggingService(logger, svc) fields := []string{"method"} svc = api.NewMetricService( kitprometheus.NewCounterFrom(stdprometheus.CounterOpts{ Namespace: "http_adapter", Subsystem: "api", Name: "request_count", Help: "Number of requests received.", }, fields), kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{ Namespace: "http_adapter", Subsystem: "api", Name: "request_latency_microseconds", Help: "Total duration of requests in microseconds.", }, fields), svc, ) errs := make(chan error, 2) go func() { p := fmt.Sprintf(":%d", cfg.Port) logger.Log("status", "HTTP adapter started.") errs <- http.ListenAndServe(p, api.MakeHandler(svc)) }() go func() { c := make(chan os.Signal) signal.Notify(c, syscall.SIGINT) errs <- fmt.Errorf("%s", <-c) }() status := fmt.Sprintf("HTTP adapter stopped due to %s", <-errs) logger.Log("status", status) } func env(key, fallback string) string { value := os.Getenv(key) if value == "" { return fallback } return value }