58 lines
1.7 KiB
Go
58 lines
1.7 KiB
Go
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"
|
|
echopprof "github.com/labstack/echo-contrib/pprof"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
func Run(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))))
|
|
r.GET("/metrics", echo.WrapHandler(promhttp.Handler()))
|
|
|
|
log.Info("starting webserver", "host", fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port))
|
|
|
|
return r.Start((fmt.Sprintf("%s:%d", cfg.WebUi.IP, cfg.WebUi.Port)))
|
|
}
|
|
|
|
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)))
|
|
}
|
|
},
|
|
})
|
|
}
|