diff options
Diffstat (limited to 'pkg/apiv2')
-rw-r--r-- | pkg/apiv2/api.go | 27 | ||||
-rw-r--r-- | pkg/apiv2/echo.go | 36 | ||||
-rw-r--r-- | pkg/apiv2/message.go | 28 |
3 files changed, 91 insertions, 0 deletions
diff --git a/pkg/apiv2/api.go b/pkg/apiv2/api.go new file mode 100644 index 0000000..8fbd759 --- /dev/null +++ b/pkg/apiv2/api.go @@ -0,0 +1,27 @@ +package apiv2 + +import ( + "github.com/labstack/echo/v4" + "gitrepo.ru/neonxp/idecnode/pkg/config" + "gitrepo.ru/neonxp/idecnode/pkg/idec" +) + +type API struct { + config *config.Config + idec *idec.IDEC +} + +func New(i *idec.IDEC, cfg *config.Config) *API { + return &API{ + config: cfg, + idec: i, + } +} + +func (a *API) Register(e *echo.Echo) { + func(g *echo.Group) { + g.GET("/list", a.getListHandler) + g.GET("/e", a.getEchoHandler) + g.GET("/m", a.getMessagesHandler) + }(e.Group("/api")) +} diff --git a/pkg/apiv2/echo.go b/pkg/apiv2/echo.go new file mode 100644 index 0000000..daa68a8 --- /dev/null +++ b/pkg/apiv2/echo.go @@ -0,0 +1,36 @@ +package apiv2 + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +func (a *API) getListHandler(c echo.Context) error { + echos, err := a.idec.GetEchos() + if err != nil { + return err + } + + return c.JSON(http.StatusOK, echos) +} + +func (a *API) getEchoHandler(c echo.Context) error { + q := new(getEchosRequest) + if err := c.Bind(q); err != nil { + return err + } + + echos, err := a.idec.GetEchosByIDs(q.EchoIDs, q.Offset, q.Limit) + if err != nil { + return err + } + + return c.JSON(http.StatusOK, echos) +} + +type getEchosRequest struct { + EchoIDs []string `query:"e"` + Offset int `query:"offset"` + Limit int `query:"limit"` +} diff --git a/pkg/apiv2/message.go b/pkg/apiv2/message.go new file mode 100644 index 0000000..7b06fde --- /dev/null +++ b/pkg/apiv2/message.go @@ -0,0 +1,28 @@ +package apiv2 + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +func (a *API) getMessagesHandler(c echo.Context) error { + q := new(getMessagesRequest) + if err := c.Bind(q); err != nil { + return err + } + + msgs, err := a.idec.GetMessagesByEcho(q.Echo, q.Message, q.Offset, q.Limit) + if err != nil { + return err + } + + return c.JSON(http.StatusOK, msgs) +} + +type getMessagesRequest struct { + Echo string `query:"e"` + Message string `query:"m"` + Offset int `query:"offset"` + Limit int `query:"limit"` +} |