diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-29 01:21:05 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-29 01:21:05 +0300 |
commit | fd4e0c3112d69061d495dfcf79f6ef62e3c6d5e6 (patch) | |
tree | fa55d6e879e3c6d85601d5c2486837f323ffd81d /pkg/apiv1 | |
parent | b26bd10926447ed59cbf263aef087bb7c04f35eb (diff) |
Diffstat (limited to 'pkg/apiv1')
-rw-r--r-- | pkg/apiv1/api.go | 44 | ||||
-rw-r--r-- | pkg/apiv1/echo.go | 61 | ||||
-rw-r--r-- | pkg/apiv1/file.go | 51 | ||||
-rw-r--r-- | pkg/apiv1/list.go | 32 | ||||
-rw-r--r-- | pkg/apiv1/message.go | 63 | ||||
-rw-r--r-- | pkg/apiv1/misc.go | 13 |
6 files changed, 264 insertions, 0 deletions
diff --git a/pkg/apiv1/api.go b/pkg/apiv1/api.go new file mode 100644 index 0000000..70e9cc1 --- /dev/null +++ b/pkg/apiv1/api.go @@ -0,0 +1,44 @@ +package apiv1 + +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) { + e.GET(`/list.txt`, a.getListHandler) + e.GET(`/blacklist.txt`, a.getBlacklistHandler) + + func(g *echo.Group) { + g.GET(`/e/*`, a.getEchosHandler) + g.GET(`/m/*`, a.getBundleHandler) + g.GET(`/point/:pauth/:tmsg`, a.postPointHandler) + g.POST(`/point`, a.postPointHandler) + }(e.Group("/u")) + + e.GET(`/e/:id`, a.getEchoHandler) + e.GET(`/m/:msgID`, a.getMessageHandler) + + func(g *echo.Group) { + e.GET(`/features`, a.getFeaturesHandler) + e.GET(`/c/*`, a.getEchosInfo) + // e.POST(`/filelist`, a.getFilelistHandler) + // e.GET(`/filelist/:pauth`, a.getFilelistHandler) + // e.POST(`/file`, a.getFileHandler) + // e.GET(`/file/:filename`, a.getFileHandler) + }(e.Group("/x")) +} diff --git a/pkg/apiv1/echo.go b/pkg/apiv1/echo.go new file mode 100644 index 0000000..68e7f48 --- /dev/null +++ b/pkg/apiv1/echo.go @@ -0,0 +1,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 +} diff --git a/pkg/apiv1/file.go b/pkg/apiv1/file.go new file mode 100644 index 0000000..eb1a8d8 --- /dev/null +++ b/pkg/apiv1/file.go @@ -0,0 +1,51 @@ +package apiv1 + +import ( + "fmt" + "net/http" +) + +func (a *API) getFilelistHandler(w http.ResponseWriter, r *http.Request) { + pauth := r.PathValue("pauth") + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + + return + } + form := r.PostForm + if form.Has("pauth") { + pauth = form.Get("pauth") + } + + files, err := a.idec.FilesList(pauth) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + + return + } + + for _, file := range files { + fmt.Fprintf(w, "%s:%d:%s\n", file.FullName, file.Size, file.Name) + } +} + +func (a *API) getFileHandler(w http.ResponseWriter, r *http.Request) { + filename := r.PathValue("filename") + pauth := "" + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + + return + } + form := r.PostForm + if form.Has("pauth") { + pauth = form.Get("pauth") + } + if form.Has("filename") { + filename = form.Get("filename") + } + + if err := a.idec.GetFile(pauth, filename, w); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } +} diff --git a/pkg/apiv1/list.go b/pkg/apiv1/list.go new file mode 100644 index 0000000..1e5dafc --- /dev/null +++ b/pkg/apiv1/list.go @@ -0,0 +1,32 @@ +package apiv1 + +import ( + "fmt" + "strings" + + "github.com/labstack/echo/v4" +) + +func (a *API) getListHandler(c echo.Context) error { + echos, err := a.idec.GetEchos() + if err != nil { + return echo.ErrInternalServerError + } + + for _, e := range echos { + fmt.Fprintf(c.Response(), "%s:%d:%s\n", e.Name, e.Count, e.Description) + } + + return nil +} + +func (a *API) getBlacklistHandler(c echo.Context) error { + list, err := a.idec.GetBlacklist() + if err != nil { + return echo.ErrInternalServerError + } + + fmt.Fprint(c.Response(), strings.Join(list, "\n")) + + return nil +} diff --git a/pkg/apiv1/message.go b/pkg/apiv1/message.go new file mode 100644 index 0000000..301b5cb --- /dev/null +++ b/pkg/apiv1/message.go @@ -0,0 +1,63 @@ +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"` +} diff --git a/pkg/apiv1/misc.go b/pkg/apiv1/misc.go new file mode 100644 index 0000000..fa3ca21 --- /dev/null +++ b/pkg/apiv1/misc.go @@ -0,0 +1,13 @@ +package apiv1 + +import ( + "net/http" + "strings" + + "github.com/labstack/echo/v4" + "gitrepo.ru/neonxp/idecnode/pkg/idec" +) + +func (a *API) getFeaturesHandler(c echo.Context) error { + return c.String(http.StatusOK, strings.Join(idec.Features, "\n")) +} |