2024-03-19 21:30:37 +00:00
|
|
|
package delivery
|
2020-11-08 17:19:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-28 20:22:49 +00:00
|
|
|
"log/slog"
|
2021-11-21 17:31:47 +00:00
|
|
|
"net/http"
|
2020-11-08 17:19:25 +00:00
|
|
|
|
2024-04-24 17:36:33 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/pkg/rlog"
|
2023-10-08 16:46:03 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/config"
|
2024-06-02 19:53:33 +00:00
|
|
|
"git.kmsign.ru/royalcat/tstor/src/sources/torrent"
|
|
|
|
"git.kmsign.ru/royalcat/tstor/src/vfs"
|
2020-11-08 17:19:25 +00:00
|
|
|
"github.com/anacrolix/missinggo/v2/filecache"
|
2024-04-24 17:36:33 +00:00
|
|
|
echopprof "github.com/labstack/echo-contrib/pprof"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2020-11-08 17:19:25 +00:00
|
|
|
)
|
|
|
|
|
2024-06-14 22:14:44 +00:00
|
|
|
func New(fc *filecache.Cache, s *torrent.Daemon, vfs vfs.Filesystem, logPath string, cfg *config.Settings) error {
|
2024-01-28 20:22:49 +00:00
|
|
|
log := slog.With()
|
|
|
|
|
2024-04-24 17:36:33 +00:00
|
|
|
r := echo.New()
|
|
|
|
r.Use(
|
2024-04-27 11:00:34 +00:00
|
|
|
// middleware.Recover(),
|
2024-04-24 17:36:33 +00:00
|
|
|
middleware.Gzip(),
|
|
|
|
middleware.Decompress(),
|
2024-07-10 09:26:17 +00:00
|
|
|
// Logger(),
|
2024-04-24 17:36:33 +00:00
|
|
|
)
|
2020-11-08 17:19:25 +00:00
|
|
|
|
2024-04-24 17:36:33 +00:00
|
|
|
echopprof.Register(r)
|
|
|
|
|
|
|
|
r.Any("/graphql", echo.WrapHandler((GraphQLHandler(s, vfs))))
|
2020-11-09 10:33:19 +00:00
|
|
|
|
2024-01-28 20:22:49 +00:00
|
|
|
log.Info("starting webserver", "host", fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port))
|
2020-11-08 17:19:25 +00:00
|
|
|
|
2024-04-24 17:36:33 +00:00
|
|
|
go r.Start((fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port)))
|
2020-11-08 17:19:25 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-29 10:07:54 +00:00
|
|
|
|
2024-04-24 17:36:33 +00:00
|
|
|
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)))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2021-11-29 10:07:54 +00:00
|
|
|
}
|