tstor/src/delivery/http.go

60 lines
1.6 KiB
Go
Raw Normal View History

2024-03-19 21:30:37 +00:00
package delivery
import (
"fmt"
2024-01-28 20:22:49 +00:00
"log/slog"
2021-11-21 17:31:47 +00:00
"net/http"
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"
"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"
)
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(),
Logger(),
)
2024-04-24 17:36:33 +00:00
echopprof.Register(r)
r.Any("/graphql", echo.WrapHandler((GraphQLHandler(s, vfs))))
2024-01-28 20:22:49 +00:00
log.Info("starting webserver", "host", fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port))
2024-04-24 17:36:33 +00:00
go r.Start((fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port)))
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
}