2020-11-08 17:19:25 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-11-21 17:31:47 +00:00
|
|
|
"net/http"
|
2020-11-08 17:19:25 +00:00
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo/v2/filecache"
|
2021-03-01 16:43:28 +00:00
|
|
|
"github.com/distribyted/distribyted"
|
|
|
|
"github.com/distribyted/distribyted/config"
|
2021-11-16 12:13:58 +00:00
|
|
|
"github.com/distribyted/distribyted/torrent"
|
2020-11-08 17:19:25 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-03-10 09:54:56 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-11-08 17:19:25 +00:00
|
|
|
"github.com/shurcooL/httpfs/html/vfstemplate"
|
|
|
|
)
|
|
|
|
|
2021-11-23 12:05:49 +00:00
|
|
|
func New(fc *filecache.Cache, ss *torrent.Stats, s *torrent.Service, ch *config.Handler, tss []*torrent.Server, fs http.FileSystem, logPath string, cfg *config.HTTPGlobal) error {
|
2020-11-08 17:19:25 +00:00
|
|
|
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())
|
2020-11-08 17:19:25 +00:00
|
|
|
|
2021-11-21 17:31:47 +00:00
|
|
|
r.GET("/assets/*filepath", func(c *gin.Context) {
|
|
|
|
c.FileFromFS(c.Request.URL.Path, http.FS(distribyted.Assets))
|
|
|
|
})
|
|
|
|
|
2021-11-23 12:05:49 +00:00
|
|
|
if cfg.HTTPFS {
|
|
|
|
log.Info().Str("host", fmt.Sprintf("%s:%d/fs", cfg.IP, cfg.Port)).Msg("starting HTTPFS")
|
|
|
|
r.GET("/fs/*filepath", func(c *gin.Context) {
|
|
|
|
path := c.Param("filepath")
|
|
|
|
c.FileFromFS(path, fs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-21 17:31:47 +00:00
|
|
|
t, err := vfstemplate.ParseGlob(http.FS(distribyted.Templates), nil, "/templates/*")
|
2020-11-08 17:19:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing html: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.SetHTMLTemplate(t)
|
|
|
|
|
|
|
|
r.GET("/", indexHandler)
|
|
|
|
r.GET("/routes", routesHandler(ss))
|
2021-11-20 19:57:25 +00:00
|
|
|
r.GET("/logs", logsHandler)
|
2021-11-21 13:03:18 +00:00
|
|
|
r.GET("/servers", serversFoldersHandler())
|
2020-11-09 10:33:19 +00:00
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
api := r.Group("/api")
|
|
|
|
{
|
2021-11-20 19:57:25 +00:00
|
|
|
api.GET("/log", apiLogHandler(logPath))
|
2020-11-08 17:19:25 +00:00
|
|
|
api.GET("/status", apiStatusHandler(fc, ss))
|
2021-11-21 13:03:18 +00:00
|
|
|
api.GET("/servers", apiServersHandler(tss))
|
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
api.GET("/routes", apiRoutesHandler(ss))
|
2021-11-16 12:13:58 +00:00
|
|
|
api.POST("/routes/:route/torrent", apiAddTorrentHandler(s))
|
|
|
|
api.DELETE("/routes/:route/torrent/:torrent_hash", apiDelTorrentHandler(s))
|
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 12:05:49 +00:00
|
|
|
log.Info().Str("host", fmt.Sprintf("%s:%d", cfg.IP, cfg.Port)).Msg("starting webserver")
|
2020-11-08 17:19:25 +00:00
|
|
|
|
2021-11-23 12:05:49 +00:00
|
|
|
if err := r.Run(fmt.Sprintf("%s:%d", cfg.IP, cfg.Port)); err != nil {
|
2020-11-08 17:19:25 +00:00
|
|
|
return fmt.Errorf("error initializing server: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-29 10:07:54 +00:00
|
|
|
|
|
|
|
func Logger() gin.HandlerFunc {
|
|
|
|
l := log.Logger.With().Str("component", "http").Logger()
|
|
|
|
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:
|
|
|
|
l.Warn().Str("path", path).Int("status", s).Msg(msg)
|
|
|
|
case s >= 500:
|
|
|
|
l.Error().Str("path", path).Int("status", s).Msg(msg)
|
|
|
|
default:
|
|
|
|
l.Debug().Str("path", path).Int("status", s).Msg(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|