2022-03-21 22:27:35 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2022-03-22 22:59:09 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-03-21 22:27:35 +00:00
|
|
|
"github.com/unrolled/render"
|
2022-03-22 22:59:09 +00:00
|
|
|
|
|
|
|
"go.mills.io/saltyim"
|
2022-03-21 22:27:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// API ...
|
|
|
|
type API struct {
|
|
|
|
router *Router
|
|
|
|
config *Config
|
|
|
|
db Store
|
|
|
|
|
|
|
|
r *render.Render
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPI ...
|
|
|
|
func NewAPI(router *Router, config *Config, db Store) *API {
|
|
|
|
api := &API{router, config, db, render.New()}
|
|
|
|
|
|
|
|
api.initRoutes()
|
|
|
|
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) initRoutes() {
|
|
|
|
router := a.router.Group("/api/v1")
|
|
|
|
|
|
|
|
router.GET("/ping", a.PingEndpoint())
|
2022-04-02 02:59:39 +00:00
|
|
|
|
|
|
|
// Lookup and Send support for Web / PWA clients
|
2022-03-31 00:40:09 +00:00
|
|
|
router.GET("/lookup/:addr", a.LookupEndpoint())
|
2022-03-31 03:46:12 +00:00
|
|
|
router.POST("/send", a.SendEndpoint())
|
2022-04-02 02:59:39 +00:00
|
|
|
|
|
|
|
// Avatar Service
|
|
|
|
router.POST("/avatar", a.AvatarEndpoint())
|
2022-03-21 22:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PingEndpoint ...
|
|
|
|
func (a *API) PingEndpoint() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, _ = w.Write([]byte(`{}`))
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 22:59:09 +00:00
|
|
|
|
2022-03-31 03:46:12 +00:00
|
|
|
// LookupEndpoint ...
|
|
|
|
func (a *API) LookupEndpoint() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
addr, err := saltyim.LookupAddr(p.ByName("addr"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Lookup Error", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.r.JSON(w, http.StatusOK, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendEndpoint ...
|
|
|
|
func (a *API) SendEndpoint() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
req, signer, err := saltyim.NewSendRequest(r.Body)
|
2022-03-31 00:40:09 +00:00
|
|
|
if err != nil {
|
2022-03-31 03:46:12 +00:00
|
|
|
log.WithError(err).Error("error parsing send request")
|
|
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
2022-03-31 00:40:09 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-31 10:40:40 +00:00
|
|
|
// TODO: Do something with signer?
|
|
|
|
log.Debugf("request signed by %s", signer)
|
2022-03-31 03:46:12 +00:00
|
|
|
|
|
|
|
// TODO: Queue up an internal retry and return immediately on failure?
|
|
|
|
if err := saltyim.Send(req.Endpoint, req.Message, req.Capabilities); err != nil {
|
2022-03-31 21:38:34 +00:00
|
|
|
log.WithError(err).Errorf("error sending message to %s", req.Endpoint)
|
2022-03-31 10:40:40 +00:00
|
|
|
http.Error(w, "Send Error", http.StatusInternalServerError)
|
2022-03-31 03:46:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Error(w, "Message Accepted", http.StatusAccepted)
|
2022-03-31 00:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 02:59:39 +00:00
|
|
|
|
|
|
|
// AvatarEndpoint ...
|
|
|
|
func (a *API) AvatarEndpoint() httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
req, signer, err := saltyim.NewAvatarRequest(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("error parsing avatar request")
|
|
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: Do something with signer?
|
|
|
|
log.Debugf("request signed by %s", signer)
|
|
|
|
|
|
|
|
if err := CreateOrUpdateAvatar(a.config, req.Addr, req.Content); err != nil {
|
|
|
|
log.WithError(err).Errorf("error creating/updating avatar for %s", req.Addr)
|
|
|
|
http.Error(w, "Avatar Error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Error(w, "Avatar Created", http.StatusCreated)
|
|
|
|
}
|
|
|
|
}
|