This commit is contained in:
royalcat 2024-01-28 23:22:49 +03:00
parent 2cefb9db98
commit b97dcc8d8f
52 changed files with 7570 additions and 555 deletions
src/http

View file

@ -2,19 +2,22 @@ package http
import (
"fmt"
"log/slog"
"net/http"
"git.kmsign.ru/royalcat/tstor"
"git.kmsign.ru/royalcat/tstor/src/config"
"git.kmsign.ru/royalcat/tstor/src/delivery"
"git.kmsign.ru/royalcat/tstor/src/host/service"
"github.com/anacrolix/missinggo/v2/filecache"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"github.com/shurcooL/httpfs/html/vfstemplate"
)
func New(fc *filecache.Cache, ss *service.Stats, s *service.Service, logPath string, cfg *config.Config) error {
log := slog.With()
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery())
@ -37,6 +40,7 @@ func New(fc *filecache.Cache, ss *service.Stats, s *service.Service, logPath str
// r.GET("/routes", routesHandler(ss))
r.GET("/logs", logsHandler)
r.GET("/servers", serversFoldersHandler())
r.Any("/graphql", gin.WrapH(delivery.GraphQLHandler(s)))
api := r.Group("/api")
{
@ -50,7 +54,7 @@ func New(fc *filecache.Cache, ss *service.Stats, s *service.Service, logPath str
}
log.Info().Str("host", fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port)).Msg("starting webserver")
log.Info("starting webserver", "host", fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port))
if err := r.Run(fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port)); err != nil {
return fmt.Errorf("error initializing server: %w", err)
@ -60,7 +64,7 @@ func New(fc *filecache.Cache, ss *service.Stats, s *service.Service, logPath str
}
func Logger() gin.HandlerFunc {
l := log.Logger.With().Str("component", "http").Logger()
l := slog.With("component", "http")
return func(c *gin.Context) {
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
@ -76,11 +80,11 @@ func Logger() gin.HandlerFunc {
s := c.Writer.Status()
switch {
case s >= 400 && s < 500:
l.Warn().Str("path", path).Int("status", s).Msg(msg)
l.Warn(msg, "path", path, "status", s)
case s >= 500:
l.Error().Str("path", path).Int("status", s).Msg(msg)
l.Error(msg, "path", path, "status", s)
default:
l.Debug().Str("path", path).Int("status", s).Msg(msg)
l.Debug(msg, "path", path, "status", s)
}
}
}