-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
134 lines (122 loc) · 3.45 KB
/
config.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
package transport
import (
"fmt"
"strings"
"github.com/luopengift/log"
"github.com/luopengift/types"
)
// Configer config interface
type Configer interface {
Parse(interface{}) error
}
// RuntimeConfig runtime config
type RuntimeConfig struct {
DEBUG bool `json:"DEBUG" yaml:"DEBUG"`
MAXPROCS int `json:"MAXPROCS" yaml:"MAXPROCS"`
BYTESIZE int `json:"BYTESIZE" yaml:"BYTESIZE"`
CHANSIZE int `json:"CHANSIZE" yaml:"CHANSIZE"`
VERSION string `json:"VERSION" yaml:"VERSION"`
HTTP string `json:"HTTP" yaml:"HTTP"`
}
// NewRuntimeConfig new runtime config
func NewRuntimeConfig() *RuntimeConfig {
return &RuntimeConfig{
DEBUG: true,
MAXPROCS: 1,
BYTESIZE: 1000,
CHANSIZE: 100,
HTTP: "0.0.0.0:12345",
VERSION: "",
}
}
// Config config
type Config struct {
Runtime *RuntimeConfig `json:"runtime"`
InputConfig map[string]types.HashMap `json:"inputs"`
HandleConfig map[string]types.HashMap `json:"handles"`
OutputConfig map[string]types.HashMap `json:"outputs"`
}
func (cfg *Config) String() string {
var Func = func(cfg map[string]types.HashMap) string {
str := ""
writeSpace := " "
for plugin, config := range cfg {
str += strings.Repeat(writeSpace, 2) + plugin + ":\n"
for key, value := range config {
valueString, _ := types.ToString(value)
str += strings.Repeat(writeSpace, 4) + key + ": " + valueString + "\n"
}
}
return str
}
str := "config info:\n"
str += "[Inputs]\n"
str += Func(cfg.InputConfig)
str += "[Adapts]\n"
str += Func(cfg.HandleConfig)
str += "[Outputs]\n"
str += Func(cfg.OutputConfig)
return str
}
// NewConfig new config
func NewConfig(path string) *Config {
cfg := new(Config)
err := cfg.Init(path)
if err != nil {
log.Error("config parse error!%v", err)
return nil
}
return cfg
}
// Init init
func (cfg *Config) Init(path string) error {
return types.ParseConfigFiles(cfg, path)
}
// InitInputs init input plugins
func (cfg *Config) InitInputs() ([]*Input, error) {
var inputs []*Input
for inputName, config := range cfg.InputConfig {
inputer, ok := Plugins.Inputers[inputName]
if !ok {
return nil, fmt.Errorf("[%s] input is not register in plugins", inputName)
}
input := NewInput(inputName, inputer)
if err := input.Inputer.Init(config); err != nil {
return nil, fmt.Errorf("[%s] input init error:%v", inputName, err)
}
inputs = append(inputs, input)
}
return inputs, nil
}
// InitOutputs init output plugins
func (cfg *Config) InitOutputs() ([]*Output, error) {
var outputs []*Output
for outputName, config := range cfg.OutputConfig {
outputer, ok := Plugins.Outputers[outputName]
if !ok {
return nil, fmt.Errorf("[%s] output is not register in plugins", outputName)
}
output := NewOutput(outputName, outputer)
if err := output.Outputer.Init(config); err != nil {
return nil, fmt.Errorf("[%s] output init error:%v", outputName, err)
}
outputs = append(outputs, output)
}
return outputs, nil
}
// InitAdapts init adapt plugins
func (cfg *Config) InitAdapts() ([]*Adapt, error) {
var adapts []*Adapt
for adaptName, config := range cfg.HandleConfig {
adapt, ok := Plugins.Adapters[adaptName]
if !ok {
return nil, fmt.Errorf("[%s] adapt is not register in plugins", adaptName)
}
handle := NewAdapt(adaptName, adapt, cfg.Runtime.CHANSIZE)
if err := handle.Adapter.Init(config); err != nil {
return nil, fmt.Errorf("[%s] adapt init error:%v", adaptName, err)
}
adapts = append(adapts, handle)
}
return adapts, nil
}