idecnode/pkg/apiv1/message.go

64 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-10-29 01:21:05 +03:00
package apiv1
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
func (a *API) getBundleHandler(c echo.Context) error {
ids := strings.Split(c.Param("*"), "/")
for _, messageID := range ids {
msg, err := a.idec.GetMessage(messageID)
if err != nil {
log.Println("cant read file for message", messageID, err)
continue
}
b64msg := base64.StdEncoding.EncodeToString([]byte(msg.Bundle()))
fmt.Fprintf(c.Response(), "%s:%s\n", messageID, b64msg)
}
return nil
}
func (a *API) getMessageHandler(c echo.Context) error {
msgID := c.Param("msgID")
msg, err := a.idec.GetMessage(msgID)
if err != nil {
return echo.ErrBadRequest
}
return c.String(http.StatusOK, msg.Bundle())
}
func (a *API) postPointHandler(c echo.Context) error {
form := new(postForm)
if err := c.Bind(form); err != nil {
return err
}
point, err := a.idec.GetPointByAuth(form.PAuth)
if err != nil {
return c.String(http.StatusForbidden, "error: no auth - wrong authstring")
}
if err := a.idec.SavePointMessage(point.Username, form.TMsg); err != nil {
return err
}
return c.String(http.StatusOK, "msg ok")
}
type postForm struct {
TMsg string `form:"tmsg"`
PAuth string `form:"pauth"`
}