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.Service, 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.GET("/assets/*filepath", func(c *echo.Context) { // c.FileFromFS(c.Request.URL.Path, http.FS(tstor.Assets)) // }) // t, err := vfstemplate.ParseGlob(http.FS(tstor.Templates), nil, "/templates/*") // if err != nil { // return fmt.Errorf("error parsing html: %w", err) // } // r.SetHTMLTemplate(t) // r.GET("/", indexHandler) // // r.GET("/routes", routesHandler(ss)) // r.GET("/logs", logsHandler) // r.GET("/servers", serversFoldersHandler()) r.Any("/graphql", echo.WrapHandler((GraphQLHandler(s, vfs)))) // api := r.Group("/api") // { // api.GET("/log", apiLogHandler(logPath)) // api.GET("/status", apiStatusHandler(fc, ss)) // // api.GET("/servers", apiServersHandler(tss)) // // api.GET("/routes", apiRoutesHandler(ss)) // // api.POST("/routes/:route/torrent", apiAddTorrentHandler(s)) // // api.DELETE("/routes/:route/torrent/:torrent_hash", apiDelTorrentHandler(s)) // } 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) // } 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))) } }, }) }