idecnode/pkg/api/echo.go

58 lines
1.3 KiB
Go
Raw Normal View History

2024-10-20 03:39:07 +03:00
package api
import (
"fmt"
"net/http"
"strings"
)
func (a *API) getEchoHandler(w http.ResponseWriter, r *http.Request) {
echoID := r.PathValue("id")
echos, err := a.idec.GetEchosByIDs([]string{echoID}, 0, 0)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(echos) == 0 {
return
}
fmt.Fprint(w, strings.Join(echos[echoID].Messages, "\n"))
}
func (a *API) getEchosHandler(w http.ResponseWriter, r *http.Request) {
ids := strings.Split(r.PathValue("ids"), "/")
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 {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, echoID := range ids {
e := echos[echoID]
fmt.Fprintln(w, e.Name)
if len(e.Messages) > 0 {
fmt.Fprintln(w, strings.Join(e.Messages, "\n"))
}
}
}
func (a *API) getEchosInfo(w http.ResponseWriter, r *http.Request) {
ids := strings.Split(r.PathValue("ids"), "/")
echos, err := a.idec.GetEchosByIDs(ids, 0, 0)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, e := range echos {
fmt.Fprintf(w, "%s:%d\n", e.Name, e.Count)
}
}