2020-11-08 17:19:25 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-11-09 10:33:19 +00:00
|
|
|
"net/http"
|
2020-11-08 17:19:25 +00:00
|
|
|
|
|
|
|
"github.com/ajnavarro/distribyted/config"
|
|
|
|
"github.com/ajnavarro/distribyted/stats"
|
|
|
|
"github.com/anacrolix/missinggo/v2/filecache"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
var apiStatusHandler = func(fc *filecache.Cache, ss *stats.Torrent) gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
// TODO move to a struct
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
2020-11-08 17:19:25 +00:00
|
|
|
"cacheItems": fc.Info().NumItems,
|
|
|
|
"cacheFilled": fc.Info().Filled / 1024 / 1024,
|
|
|
|
"cacheCapacity": fc.Info().Capacity / 1024 / 1024,
|
|
|
|
"torrentStats": ss.GlobalStats(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiRoutesHandler = func(ss *stats.Torrent) gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
stats := ss.RoutesStats()
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.JSON(http.StatusOK, stats)
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiGetConfigFile = func(ch *config.Handler) gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
rc, err := ch.GetRaw()
|
|
|
|
if err != nil {
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.AbortWithError(http.StatusInternalServerError, err)
|
2020-11-08 17:19:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.Data(http.StatusOK, "text/x-yaml", rc)
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiSetConfigFile = func(ch *config.Handler) gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
if ctx.Request.Body == nil {
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.AbortWithError(http.StatusInternalServerError, errors.New("no config file sent"))
|
2020-11-08 17:19:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := ioutil.ReadAll(ctx.Request.Body)
|
|
|
|
if err != nil {
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.AbortWithError(http.StatusInternalServerError, err)
|
2020-11-08 17:19:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c) == 0 {
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.AbortWithError(http.StatusInternalServerError, errors.New("no config file sent"))
|
2020-11-08 17:19:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ch.Set(c); err != nil {
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.AbortWithError(http.StatusInternalServerError, err)
|
2020-11-08 17:19:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:33:19 +00:00
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
|
|
"message": "config file saved",
|
|
|
|
})
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:33:19 +00:00
|
|
|
var apiStreamEvents = func(events chan string) gin.HandlerFunc {
|
2020-11-08 17:19:25 +00:00
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
ctx.Stream(func(w io.Writer) bool {
|
2020-11-09 10:33:19 +00:00
|
|
|
if msg, ok := <-events; ok {
|
|
|
|
ctx.SSEvent("event", msg)
|
|
|
|
return true
|
2020-11-08 17:19:25 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 10:33:19 +00:00
|
|
|
|
|
|
|
var apiReloadServer = func(ch *config.Handler, events chan string) gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
|
|
|
events <- "starting reload configuration process..."
|
|
|
|
|
|
|
|
err := ch.Reload(
|
|
|
|
func(m string) {
|
|
|
|
events <- m
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
|
|
"message": "reload process finished with no errors",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|