matrix-handler/pkg/generic/generic.go

37 lines
891 B
Go
Raw Permalink Normal View History

2021-02-04 07:25:16 +00:00
package generic
import (
"io/ioutil"
"net/http"
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/config"
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/matrix"
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/router"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)
// Handle is the incoming handler for Generic-type requests.
func Handle(cfg config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
// Get Matrix Token for User/Pass in path
token := matrix.GetToken(cfg, vars)
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error().Err(err).Msg("An error has occurred")
}
2021-02-04 23:50:52 +00:00
// Attempt to join room before sending a message.
matrix.JoinRoom(cfg, vars, token)
// Publish to Matrix
resp := matrix.PublishText(cfg, vars, reqBody, token)
2021-02-04 07:25:16 +00:00
router.Respond(w, 200, resp)
2021-02-04 07:25:16 +00:00
}
}