Introduce the Go backend (netwatch-server) with an HTTP API that accepts telemetry reports and persists them as zstd-compressed JSONL files. Reports are buffered in memory and flushed to disk when the buffer reaches 10 MiB or every 60 seconds.
75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
// Package handlers implements HTTP request handlers for the
|
|
// netwatch-server API.
|
|
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"sneak.berlin/go/netwatch/internal/globals"
|
|
"sneak.berlin/go/netwatch/internal/healthcheck"
|
|
"sneak.berlin/go/netwatch/internal/logger"
|
|
"sneak.berlin/go/netwatch/internal/reportbuf"
|
|
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
const jsonContentType = "application/json; charset=utf-8"
|
|
|
|
// Params defines the dependencies for Handlers.
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Buffer *reportbuf.Buffer
|
|
Globals *globals.Globals
|
|
Healthcheck *healthcheck.Healthcheck
|
|
Logger *logger.Logger
|
|
}
|
|
|
|
// Handlers provides HTTP handler factories for all endpoints.
|
|
type Handlers struct {
|
|
buf *reportbuf.Buffer
|
|
hc *healthcheck.Healthcheck
|
|
log *slog.Logger
|
|
params *Params
|
|
}
|
|
|
|
// New creates a Handlers instance.
|
|
func New(
|
|
lc fx.Lifecycle,
|
|
params Params,
|
|
) (*Handlers, error) {
|
|
s := new(Handlers)
|
|
s.buf = params.Buffer
|
|
s.params = ¶ms
|
|
s.log = params.Logger.Get()
|
|
s.hc = params.Healthcheck
|
|
|
|
lc.Append(fx.Hook{
|
|
OnStart: func(_ context.Context) error {
|
|
return nil
|
|
},
|
|
})
|
|
|
|
return s, nil
|
|
}
|
|
|
|
func (s *Handlers) respondJSON(
|
|
w http.ResponseWriter,
|
|
_ *http.Request,
|
|
data any,
|
|
status int,
|
|
) {
|
|
w.Header().Set("Content-Type", jsonContentType)
|
|
w.WriteHeader(status)
|
|
|
|
if data != nil {
|
|
err := json.NewEncoder(w).Encode(data)
|
|
if err != nil {
|
|
s.log.Error("json encode error", "error", err)
|
|
}
|
|
}
|
|
}
|