1package stats
 2
 3import (
 4	"context"
 5	"net/http"
 6	"time"
 7
 8	"github.com/charmbracelet/soft-serve/server/config"
 9	"github.com/prometheus/client_golang/prometheus/promhttp"
10)
11
12// StatsServer is a server for collecting and reporting statistics.
13type StatsServer struct {
14	cfg    *config.Config
15	server *http.Server
16}
17
18// NewStatsServer returns a new StatsServer.
19func NewStatsServer(cfg *config.Config) (*StatsServer, error) {
20	mux := http.NewServeMux()
21	mux.Handle("/metrics", promhttp.Handler())
22	return &StatsServer{
23		cfg: cfg,
24		server: &http.Server{
25			Addr:              cfg.Stats.ListenAddr,
26			Handler:           mux,
27			ReadHeaderTimeout: time.Second * 10,
28			ReadTimeout:       time.Second * 10,
29			WriteTimeout:      time.Second * 10,
30			MaxHeaderBytes:    http.DefaultMaxHeaderBytes,
31		},
32	}, nil
33}
34
35// ListenAndServe starts the StatsServer.
36func (s *StatsServer) ListenAndServe() error {
37	return s.server.ListenAndServe()
38}
39
40// Shutdown gracefully shuts down the StatsServer.
41func (s *StatsServer) Shutdown(ctx context.Context) error {
42	return s.server.Shutdown(ctx)
43}
44
45// Close closes the StatsServer.
46func (s *StatsServer) Close() error {
47	return s.server.Close()
48}