unified-push-xmpp/gateway/web_post.go

40 lines
934 B
Go
Raw Normal View History

2021-09-12 12:14:09 +02:00
package main
import (
"github.com/gin-gonic/gin"
"io/ioutil"
2021-09-14 01:32:37 +02:00
"net/http"
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 01:32:37 +02:00
to, token, err := jwtsecret.Read(c.Query("token"))
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)
if err := xmpp.SendMessage(to, token, content); err != nil {
c.JSON(http.StatusNotFound, web.HTTPError{
Message: "unable to forward to xmpp",
Error: err.Error(),
})
return
}
c.JSON(http.StatusAccepted, content)
})
}