-
Notifications
You must be signed in to change notification settings - Fork 194
/
http.go
88 lines (77 loc) · 1.73 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
package httpoutput
import (
"encoding/json"
http_transport "github.com/eolinker/apinto/output/http-transport"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/formatter"
)
type Handler struct {
formatter eosc.IFormatter
transport formatter.ITransport
}
func NewHandler(config *Config) (*Handler, error) {
transport, fm, err := create(config)
if err != nil {
return nil, err
}
h := &Handler{
formatter: fm,
transport: transport,
}
return h, nil
}
func (h *Handler) Close() error {
h.transport.Close()
h.transport = nil
h.formatter = nil
return nil
}
func (h *Handler) Output(entry eosc.IEntry) error {
if h.formatter == nil && h.transport == nil {
return nil
}
data := h.formatter.Format(entry)
if len(data) == 0 {
return nil
}
return h.transport.Write(data)
}
func (h *Handler) reset(config *Config) error {
o := h.transport
transport, fm, err := create(config)
if err != nil {
return err
}
h.transport = transport
h.formatter = fm
if o != nil {
o.Close()
}
return nil
}
func create(config *Config) (formatter.ITransport, eosc.IFormatter, error) {
cfg := &http_transport.Config{
Method: config.Method,
Url: config.Url,
Headers: toHeader(config.Headers),
HandlerCount: 5, // 默认值, 以后可能会改成配置
}
transport, err := http_transport.CreateTransporter(cfg)
if err != nil {
return nil, nil, err
}
//创建formatter
factory, has := formatter.GetFormatterFactory(config.Type)
if !has {
return nil, nil, errFormatterType
}
var extendCfg []byte
if config.Type == "json" {
extendCfg, _ = json.Marshal(config.ContentResize)
}
fm, err := factory.Create(config.Formatter, extendCfg)
if err != nil {
return nil, nil, err
}
return transport, fm, nil
}