summaryrefslogtreecommitdiff
path: root/pkg/apiv1/echo.go
blob: 68e7f48f2dd9820d8af86f6d1f52214e67a363b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
}