92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
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: getenv(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 {
|
|
logger.Log("aborted", err)
|
|
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)
|
|
errs <- http.ListenAndServe(p, api.MakeHandler(svc))
|
|
}()
|
|
|
|
go func() {
|
|
c := make(chan os.Signal)
|
|
signal.Notify(c, syscall.SIGINT)
|
|
errs <- fmt.Errorf("%s", <-c)
|
|
}()
|
|
|
|
logger.Log("terminated", <-errs)
|
|
}
|
|
|
|
func getenv(key, fallback string) string {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
|
|
return value
|
|
}
|