idecnode/pkg/apiv1/echo.go

62 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-10-29 01:21:05 +03:00
package apiv1
import (
"fmt"
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
func (a *API) getEchoHandler(c echo.Context) error {
echoID := c.Param("id")
echos, err := a.idec.GetEchosByIDs([]string{echoID}, 0, 0)
if err != nil {
return echo.ErrBadGateway
}
if len(echos) == 0 {
return nil
}
return c.String(http.StatusOK, strings.Join(echos[echoID].Messages, "\n"))
}
func (a *API) getEchosHandler(c echo.Context) error {
ids := strings.Split(c.Param("*"), "/")
last := ids[len(ids)-1]
offset, limit := 0, 0
if _, err := fmt.Sscanf(last, "%d:%d", &offset, &limit); err == nil {
ids = ids[:len(ids)-1]
}
echos, err := a.idec.GetEchosByIDs(ids, offset, limit)
if err != nil {
return echo.ErrBadRequest
}
for _, echoID := range ids {
e := echos[echoID]
fmt.Fprintln(c.Response(), e.Name)
if len(e.Messages) > 0 {
fmt.Fprintln(c.Response(), strings.Join(e.Messages, "\n"))
}
}
return nil
}
func (a *API) getEchosInfo(c echo.Context) error {
ids := strings.Split(c.Param("*"), "/")
echos, err := a.idec.GetEchosByIDs(ids, 0, 0)
if err != nil {
return echo.ErrBadRequest
}
for _, e := range echos {
fmt.Fprintf(c.Response(), "%s:%d\n", e.Name, e.Count)
}
return nil
}