Improve info when config is reloaded. ()

- Using toasts as message outputs (still a lot of room for improvement)

Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
This commit is contained in:
Antonio Navarro Perez 2020-11-09 11:33:19 +01:00 committed by GitHub
parent 0e2288565d
commit ab7d379408
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 59 deletions

View file

@ -4,6 +4,7 @@ import (
"errors"
"io"
"io/ioutil"
"net/http"
"github.com/ajnavarro/distribyted/config"
"github.com/ajnavarro/distribyted/stats"
@ -14,7 +15,7 @@ import (
var apiStatusHandler = func(fc *filecache.Cache, ss *stats.Torrent) gin.HandlerFunc {
return func(ctx *gin.Context) {
// TODO move to a struct
ctx.JSON(200, gin.H{
ctx.JSON(http.StatusOK, gin.H{
"cacheItems": fc.Info().NumItems,
"cacheFilled": fc.Info().Filled / 1024 / 1024,
"cacheCapacity": fc.Info().Capacity / 1024 / 1024,
@ -26,7 +27,7 @@ var apiStatusHandler = func(fc *filecache.Cache, ss *stats.Torrent) gin.HandlerF
var apiRoutesHandler = func(ss *stats.Torrent) gin.HandlerFunc {
return func(ctx *gin.Context) {
stats := ss.RoutesStats()
ctx.JSON(200, stats)
ctx.JSON(http.StatusOK, stats)
}
}
@ -34,54 +35,69 @@ var apiGetConfigFile = func(ch *config.Handler) gin.HandlerFunc {
return func(ctx *gin.Context) {
rc, err := ch.GetRaw()
if err != nil {
ctx.AbortWithError(500, err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.Data(200, "text/x-yaml", rc)
ctx.Data(http.StatusOK, "text/x-yaml", rc)
}
}
var apiSetConfigFile = func(ch *config.Handler) gin.HandlerFunc {
return func(ctx *gin.Context) {
if ctx.Request.Body == nil {
ctx.AbortWithError(500, errors.New("no config file sent"))
ctx.AbortWithError(http.StatusInternalServerError, errors.New("no config file sent"))
return
}
c, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.AbortWithError(500, err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
if len(c) == 0 {
ctx.AbortWithError(500, errors.New("no config file sent"))
ctx.AbortWithError(http.StatusInternalServerError, errors.New("no config file sent"))
return
}
if err := ch.Set(c); err != nil {
ctx.AbortWithError(500, err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
// TODO return something?
ctx.JSON(200, nil)
ctx.JSON(http.StatusOK, gin.H{
"message": "config file saved",
})
}
}
var apiReloadServer = func(ch *config.Handler) gin.HandlerFunc {
var apiStreamEvents = func(events chan string) gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Stream(func(w io.Writer) bool {
err := ch.Reload(
func(m string) {
ctx.SSEvent("reload", m)
})
if err != nil {
ctx.AbortWithError(500, err)
if msg, ok := <-events; ok {
ctx.SSEvent("event", msg)
return true
}
return false
})
}
}
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",
})
}
}

View file

@ -32,14 +32,16 @@ func New(fc *filecache.Cache, ss *stats.Torrent, ch *config.Handler, port int) e
r.GET("/routes", routesHandler(ss))
r.GET("/config", configHandler)
eventChan := make(chan string)
api := r.Group("/api")
{
api.GET("/status", apiStatusHandler(fc, ss))
api.GET("/routes", apiRoutesHandler(ss))
api.GET("/config", apiGetConfigFile(ch))
api.POST("/config", apiSetConfigFile(ch))
api.POST("/reload", apiReloadServer(ch))
api.POST("/reload", apiReloadServer(ch, eventChan))
api.GET("/events", apiStreamEvents(eventChan))
}
logrus.WithField("host", fmt.Sprintf("0.0.0.0:%d", port)).Info("starting webserver")