prometheus metrics server
All checks were successful
/ Make image (push) Successful in 49s

This commit is contained in:
nquidox 2026-02-17 12:19:47 +03:00
parent 1a90f1e605
commit 4665c90c2c
4 changed files with 97 additions and 0 deletions

61
pkg/router/handler.go Normal file
View file

@ -0,0 +1,61 @@
package router
import (
"context"
"errors"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
ginprometheus "github.com/zsais/go-gin-prometheus"
"net/http"
)
type Handler struct {
srv *http.Server
}
type Deps struct {
Addr string
GinMode string
}
const pkgLogHeader string = "Router |"
func NewHandler(deps Deps) *Handler {
engine := gin.Default()
if deps.GinMode == "release" {
gin.SetMode(gin.ReleaseMode)
err := engine.SetTrustedProxies([]string{"172.20.0.0/16"})
if err != nil {
log.WithError(err).Errorf("%v Set proxies failed", pkgLogHeader)
return nil
}
}
engine.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{"msg": "v2"}) })
p := ginprometheus.NewPrometheus("gin")
p.Use(engine)
srv := http.Server{
Addr: deps.Addr,
Handler: engine,
}
return &Handler{
srv: &srv,
}
}
func (h *Handler) Run() error {
log.Infof("Starting server on %s", h.srv.Addr)
if err := h.srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.WithError(err).Errorf("%v ListenAndServe failed", pkgLogHeader)
return err
}
return nil
}
func (h *Handler) Shutdown(ctx context.Context) error {
return h.srv.Shutdown(ctx)
}