-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhandler.go
More file actions
36 lines (31 loc) · 822 Bytes
/
handler.go
File metadata and controls
36 lines (31 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"net/http"
)
// interface for parser functions
type parserFunc func(*http.Request) (string, error)
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
m, err := h.parserFunc(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
} else {
// send message to xmpp client
h.messages <- m
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
}
}
// returns new handler with a given parser function
func newMessageHandler(m chan<- string, f parserFunc) *messageHandler {
return &messageHandler{
messages: m,
parserFunc: f,
}
}