tstor/src/http/http.go

91 lines
2.2 KiB
Go
Raw Normal View History

package http
import (
"fmt"
2024-01-28 20:22:49 +00:00
"log/slog"
2021-11-21 17:31:47 +00:00
"net/http"
2023-10-08 16:46:03 +00:00
"git.kmsign.ru/royalcat/tstor"
"git.kmsign.ru/royalcat/tstor/src/config"
2024-01-28 20:22:49 +00:00
"git.kmsign.ru/royalcat/tstor/src/delivery"
2023-12-31 22:54:55 +00:00
"git.kmsign.ru/royalcat/tstor/src/host/service"
"github.com/anacrolix/missinggo/v2/filecache"
2023-12-21 23:15:39 +00:00
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/shurcooL/httpfs/html/vfstemplate"
)
2023-12-31 22:54:55 +00:00
func New(fc *filecache.Cache, ss *service.Stats, s *service.Service, logPath string, cfg *config.Config) error {
2024-01-28 20:22:49 +00:00
log := slog.With()
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery())
r.Use(gin.ErrorLogger())
2021-11-29 10:07:54 +00:00
r.Use(Logger())
2023-12-21 23:15:39 +00:00
pprof.Register(r)
2021-11-21 17:31:47 +00:00
r.GET("/assets/*filepath", func(c *gin.Context) {
2023-10-08 16:46:03 +00:00
c.FileFromFS(c.Request.URL.Path, http.FS(tstor.Assets))
2021-11-21 17:31:47 +00:00
})
2023-10-08 16:46:03 +00:00
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)
2023-10-16 09:18:40 +00:00
// r.GET("/routes", routesHandler(ss))
2021-11-20 19:57:25 +00:00
r.GET("/logs", logsHandler)
r.GET("/servers", serversFoldersHandler())
2024-01-28 20:22:49 +00:00
r.Any("/graphql", gin.WrapH(delivery.GraphQLHandler(s)))
api := r.Group("/api")
{
2021-11-20 19:57:25 +00:00
api.GET("/log", apiLogHandler(logPath))
api.GET("/status", apiStatusHandler(fc, ss))
2023-10-16 09:18:40 +00:00
// api.GET("/servers", apiServersHandler(tss))
2023-10-16 09:18:40 +00:00
// 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))
2023-10-08 16:46:03 +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)
}
return nil
}
2021-11-29 10:07:54 +00:00
func Logger() gin.HandlerFunc {
2024-01-28 20:22:49 +00:00
l := slog.With("component", "http")
2021-11-29 10:07:54 +00:00
return func(c *gin.Context) {
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
c.Next()
if raw != "" {
path = path + "?" + raw
}
msg := c.Errors.String()
if msg == "" {
msg = "Request"
}
s := c.Writer.Status()
switch {
case s >= 400 && s < 500:
2024-01-28 20:22:49 +00:00
l.Warn(msg, "path", path, "status", s)
2021-11-29 10:07:54 +00:00
case s >= 500:
2024-01-28 20:22:49 +00:00
l.Error(msg, "path", path, "status", s)
2021-11-29 10:07:54 +00:00
default:
2024-01-28 20:22:49 +00:00
l.Debug(msg, "path", path, "status", s)
2021-11-29 10:07:54 +00:00
}
}
}