tstor/src/delivery/http.go

89 lines
2.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-05-19 21:24:09 +00:00
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
2024-03-19 21:30:37 +00:00
"git.kmsign.ru/royalcat/tstor/src/host/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-05-19 21:24:09 +00:00
func New(fc *filecache.Cache, s *torrent.Service, 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.GET("/assets/*filepath", func(c *echo.Context) {
// c.FileFromFS(c.Request.URL.Path, http.FS(tstor.Assets))
// })
2021-11-21 17:31:47 +00:00
2024-04-24 17:36:33 +00:00
// t, err := vfstemplate.ParseGlob(http.FS(tstor.Templates), nil, "/templates/*")
// if err != nil {
// return fmt.Errorf("error parsing html: %w", err)
// }
2024-04-24 17:36:33 +00:00
// r.SetHTMLTemplate(t)
2024-04-24 17:36:33 +00:00
// r.GET("/", indexHandler)
// // r.GET("/routes", routesHandler(ss))
// r.GET("/logs", logsHandler)
// r.GET("/servers", serversFoldersHandler())
r.Any("/graphql", echo.WrapHandler((GraphQLHandler(s, vfs))))
2024-04-24 17:36:33 +00:00
// 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))
// }
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
// 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
}
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
}