package delivery import ( "fmt" "log/slog" "net/http" "git.kmsign.ru/royalcat/tstor/pkg/rlog" "git.kmsign.ru/royalcat/tstor/src/config" "git.kmsign.ru/royalcat/tstor/src/sources/torrent" "git.kmsign.ru/royalcat/tstor/src/vfs" "github.com/anacrolix/missinggo/v2/filecache" echopprof "github.com/labstack/echo-contrib/pprof" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) func New(fc *filecache.Cache, s *torrent.Daemon, vfs vfs.Filesystem, logPath string, cfg *config.Settings) error { log := slog.With() r := echo.New() r.Use( // middleware.Recover(), middleware.Gzip(), middleware.Decompress(), // Logger(), ) echopprof.Register(r) r.Any("/graphql", echo.WrapHandler((GraphQLHandler(s, vfs)))) log.Info("starting webserver", "host", fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port)) go r.Start((fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port))) return nil } func Logger() echo.MiddlewareFunc { l := rlog.Component("http") return middleware.BodyDumpWithConfig(middleware.BodyDumpConfig{ Skipper: func(c echo.Context) bool { return c.Request().Method == http.MethodGet }, Handler: func(c echo.Context, reqBody, resBody []byte) { log := l.With( slog.String("method", c.Request().Method), slog.String("uri", c.Request().RequestURI), ) if c.Request().Header.Get("Content-Type") == "application/json" { log.Info(c.Request().Context(), "Request body", slog.String("body", string(reqBody))) } if c.Response().Header().Get("Content-Type") == "application/json" { log.Info(c.Request().Context(), "Response body", slog.String("body", string(resBody))) } }, }) }