-
Notifications
You must be signed in to change notification settings - Fork 3
/
input_http.go
160 lines (138 loc) · 4.14 KB
/
input_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package steps
import (
"fmt"
"net/http"
"sync"
"github.com/antongulenko/golib"
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
type RestEndpointFactory struct {
endpoints map[string]*RestEndpoint
}
func (s *RestEndpointFactory) GetEndpoint(endpointString string) *RestEndpoint {
if s.endpoints == nil {
s.endpoints = make(map[string]*RestEndpoint)
}
endpoint, ok := s.endpoints[endpointString]
if !ok {
endpoint = &RestEndpoint{
engine: golib.NewGinEngine(),
endpoint: endpointString,
}
s.endpoints[endpointString] = endpoint
}
return endpoint
}
type RestEndpoint struct {
engine *gin.Engine
paths []string
startOnce sync.Once
endpoint string
stoppers []golib.StopChan
}
func (endpoint *RestEndpoint) start() {
endpoint.startOnce.Do(func() {
go func() {
// This routine cannot be interrupted
log.Println("Listening for REST input on", endpoint.endpoint)
for _, path := range endpoint.paths {
log.Println(" -", path)
}
if err := endpoint.engine.Run(endpoint.endpoint); err != nil {
for _, stopper := range endpoint.stoppers {
stopper.StopErr(err)
}
}
}()
})
}
func (endpoint *RestEndpoint) serve(verb string, path string, logFile string, serve func(*gin.Context)) (err error) {
pathStr := fmt.Sprintf("[%s] %s", verb, path)
// TODO check if pathStr is already present in paths, raise error if so
handlers := gin.HandlersChain{serve}
if logFile != "" {
handlers = append(handlers, golib.LogGinRequests(logFile, true, true))
}
defer func() {
if p := recover(); p != nil {
// Recovered from panic in Handle()
err = fmt.Errorf("Failed to register REST path %v: %v", path, p)
}
}()
endpoint.engine.Handle(verb, path, handlers...)
endpoint.paths = append(endpoint.paths, pathStr)
return
}
func (endpoint *RestEndpoint) NewDataSource(outgoingSampleBuffer int) *RestDataSource {
stopper := golib.NewStopChan()
endpoint.stoppers = append(endpoint.stoppers, stopper)
return &RestDataSource{
outgoing: make(chan bitflow.SampleAndHeader, outgoingSampleBuffer),
endpoint: endpoint,
stop: stopper,
}
}
type RestDataSource struct {
bitflow.AbstractSampleSource
outgoing chan bitflow.SampleAndHeader
stop golib.StopChan
closeOnce sync.Once
endpoint *RestEndpoint
wg *sync.WaitGroup
}
func (source *RestDataSource) Endpoint() string {
return source.endpoint.endpoint
}
func (source *RestDataSource) Serve(verb string, path string, httpLogFile string, serve func(*gin.Context)) error {
return source.endpoint.serve(verb, path, httpLogFile, serve)
}
func (source *RestDataSource) EmitSample(sample *bitflow.Sample, header *bitflow.Header) {
source.outgoing <- bitflow.SampleAndHeader{sample, header}
}
func (source *RestDataSource) Start(wg *sync.WaitGroup) golib.StopChan {
source.wg = wg
wg.Add(1)
go source.sinkSamples(wg)
source.endpoint.start()
return source.stop
}
func (source *RestDataSource) Close() {
if source.wg == nil {
return
}
source.closeOnce.Do(func() {
source.stop.Stop()
close(source.outgoing)
source.AbstractSampleSource.CloseSinkParallel(source.wg)
})
}
func (source *RestDataSource) sinkSamples(wg *sync.WaitGroup) {
defer wg.Done()
for sample := range source.outgoing {
if err := source.GetSink().Sample(sample.Sample, sample.Header); err != nil {
log.Errorln("Error sinking sample:", err)
}
}
}
type RestReplyHelpers struct {
}
func (h RestReplyHelpers) ReplySuccess(context *gin.Context, message string) {
context.Writer.Header().Set("Content-Type", "text/plain")
_, err := context.Writer.WriteString(message + "\n")
if err != nil {
log.Errorln("REST: Error sending success-reply to client:", err)
}
}
func (h RestReplyHelpers) ReplyGenericError(context *gin.Context, errorMessage string) {
log.Warnln("REST:", errorMessage)
_, err := context.Writer.WriteString(errorMessage + "\n")
context.Writer.WriteHeader(http.StatusBadRequest)
if err != nil {
log.Errorln("REST: Error sending error-reply to client:", err)
}
}
func (h RestReplyHelpers) ReplyError(context *gin.Context, errorMessage string) {
h.ReplyGenericError(context, "Wrong HTTP request: "+errorMessage)
}