esbot/internal/http/http.go

52 lines
1.2 KiB
Go
Raw Normal View History

2021-02-22 19:39:22 +00:00
package http
import (
"crypto/tls"
"encoding/json"
"net/http"
"git.netflux.io/rob/esbot/internal/matrix"
"github.com/rs/zerolog"
)
type handler struct {
logger zerolog.Logger
ep matrix.EventProcessor
}
func (h *handler) handleTransactions(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
var payload EventsRequest
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if err := h.ep.ProcessEvents(payload.RawEvents); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}
2021-02-25 15:35:34 +00:00
func ListenAndServe(listenAddr, baseURL, accessToken string, insecureTLS bool, logger zerolog.Logger) error {
2021-02-22 19:39:22 +00:00
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecureTLS}
h := handler{
logger: logger,
2021-02-24 17:20:32 +00:00
ep: matrix.NewProcessor(baseURL, accessToken, logger),
2021-02-22 19:39:22 +00:00
}
mux := http.NewServeMux()
mux.HandleFunc("/transactions/", h.handleTransactions)
2021-02-25 15:35:34 +00:00
logger.Info().Msgf("Listening on %s...", listenAddr)
2021-02-24 17:20:32 +00:00
return http.ListenAndServe(listenAddr, loggerMiddleware(logger, mux))
2021-02-22 19:39:22 +00:00
}