forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
118 lines (97 loc) · 2.34 KB
/
http.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package http
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper/server"
"github.com/elastic/beats/metricbeat/mb"
)
type HttpServer struct {
server *http.Server
ctx context.Context
stop context.CancelFunc
done chan struct{}
eventQueue chan server.Event
}
type HttpEvent struct {
event common.MapStr
meta server.Meta
}
func (h *HttpEvent) GetEvent() common.MapStr {
return h.event
}
func (h *HttpEvent) GetMeta() server.Meta {
return h.meta
}
func NewHttpServer(mb mb.BaseMetricSet) (server.Server, error) {
config := defaultHttpConfig()
err := mb.Module().UnpackConfig(&config)
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
h := &HttpServer{
done: make(chan struct{}),
eventQueue: make(chan server.Event),
ctx: ctx,
stop: cancel,
}
httpServer := &http.Server{
Addr: fmt.Sprintf("%s:%d", config.Host, config.Port),
Handler: http.HandlerFunc(h.handleFunc),
}
h.server = httpServer
return h, nil
}
func (h *HttpServer) Start() error {
go func() {
logp.Info("Starting http server on %s", h.server.Addr)
err := h.server.ListenAndServe()
if err != nil {
logp.Critical("Unable to start HTTP server due to error: %v", err)
}
}()
return nil
}
func (h *HttpServer) Stop() {
close(h.done)
h.stop()
h.server.Shutdown(h.ctx)
close(h.eventQueue)
}
func (h *HttpServer) GetEvents() chan server.Event {
return h.eventQueue
}
func (h *HttpServer) handleFunc(writer http.ResponseWriter, req *http.Request) {
switch req.Method {
case "POST":
meta := server.Meta{
"path": req.URL.String(),
}
contentType := req.Header.Get("Content-Type")
if contentType != "" {
meta["Content-Type"] = contentType
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
logp.Err("Error reading body: %v", err)
http.Error(writer, "Unexpected error reading request payload", http.StatusBadRequest)
return
}
payload := common.MapStr{
server.EventDataKey: body,
}
event := &HttpEvent{
event: payload,
meta: meta,
}
h.eventQueue <- event
writer.WriteHeader(http.StatusAccepted)
case "GET":
writer.WriteHeader(http.StatusOK)
writer.Write([]byte("HTTP Server accepts data via POST"))
}
}