2017-09-26 12:39:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2020-01-06 17:18:24 +00:00
|
|
|
// interface for parser functions
|
|
|
|
type parserFunc func(*http.Request) (string, error)
|
2017-09-26 12:39:19 +00:00
|
|
|
|
|
|
|
type messageHandler struct {
|
|
|
|
messages chan<- string // chan to xmpp client
|
|
|
|
parserFunc parserFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// http request handler
|
|
|
|
func (h *messageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// parse/generate message from http request
|
2020-01-06 17:18:24 +00:00
|
|
|
m, err := h.parserFunc(r)
|
2017-09-26 12:39:19 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2020-01-06 17:18:24 +00:00
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
} else {
|
|
|
|
// send message to xmpp client
|
|
|
|
h.messages <- m
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write([]byte("ok"))
|
2017-09-26 12:39:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns new handler with a given parser function
|
|
|
|
func newMessageHandler(m chan<- string, f parserFunc) *messageHandler {
|
|
|
|
return &messageHandler{
|
|
|
|
messages: m,
|
|
|
|
parserFunc: f,
|
|
|
|
}
|
|
|
|
}
|