unified-push-xmpp/gateway/web_post.go

41 lines
947 B
Go
Raw Normal View History

2021-09-12 12:14:09 +02:00
package main
import (
"io/ioutil"
2021-09-14 01:32:37 +02:00
"net/http"
2021-09-12 12:14:09 +02:00
2021-09-14 21:55:21 +02:00
"github.com/gin-gonic/gin"
2021-09-12 12:14:09 +02:00
"dev.sum7.eu/genofire/golang-lib/web"
)
2021-09-14 01:32:37 +02:00
func Post(r *gin.Engine, ws *web.Service, xmpp *XMPPService, jwtsecret JWTSecret) {
2021-09-12 12:14:09 +02:00
r.POST("/UP", func(c *gin.Context) {
2021-09-14 21:55:21 +02:00
to, publicToken, err := jwtsecret.Read(c.Query("token"))
2021-09-14 01:32:37 +02:00
if err != nil {
c.JSON(http.StatusUnauthorized, web.HTTPError{
Message: "jwt token unauthoried - or not given",
Error: err.Error(),
})
return
}
2021-09-12 12:14:09 +02:00
b, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, web.HTTPError{
Message: web.ErrAPIInvalidRequestFormat.Error(),
Error: err.Error(),
})
return
}
content := string(b)
2021-09-14 21:55:21 +02:00
if err := xmpp.SendMessage(to, publicToken, content); err != nil {
2021-09-12 12:14:09 +02:00
c.JSON(http.StatusNotFound, web.HTTPError{
Message: "unable to forward to xmpp",
Error: err.Error(),
})
return
}
c.JSON(http.StatusAccepted, content)
})
}