9
9
"time"
10
10
11
11
"collectd.org/api"
12
+ "collectd.org/config"
12
13
"collectd.org/plugin"
13
14
"go.uber.org/multierr"
14
15
)
@@ -20,23 +21,10 @@ type restapi struct {
20
21
}
21
22
22
23
func init () {
23
- mux := http .NewServeMux ()
24
- mux .HandleFunc ("/valueList" , valueListHandler )
25
-
26
- api := restapi {
27
- srv : & http.Server {
28
- Addr : ":8080" ,
29
- Handler : mux ,
30
- },
31
- }
32
-
33
- go func () {
34
- if err := api .srv .ListenAndServe (); ! errors .Is (err , http .ErrServerClosed ) {
35
- plugin .Errorf ("%s plugin: ListenAndServe(): %v" , pluginName , err )
36
- }
37
- }()
24
+ ra := & restapi {}
38
25
39
- plugin .RegisterShutdown (pluginName , api )
26
+ plugin .RegisterConfig (pluginName , ra )
27
+ plugin .RegisterShutdown (pluginName , ra )
40
28
}
41
29
42
30
func valueListHandler (w http.ResponseWriter , req * http.Request ) {
@@ -66,11 +54,46 @@ func valueListHandler(w http.ResponseWriter, req *http.Request) {
66
54
}
67
55
}
68
56
69
- func (api restapi ) Shutdown (ctx context.Context ) error {
57
+ func (ra * restapi ) Configure (_ context.Context , rawConfig config.Block ) error {
58
+ fmt .Printf ("%s plugin: rawConfig = %v\n " , pluginName , rawConfig )
59
+
60
+ cfg := struct {
61
+ Args string // unused
62
+ Port string
63
+ }{
64
+ Port : "8080" ,
65
+ }
66
+
67
+ if err := rawConfig .Unmarshal (& cfg ); err != nil {
68
+ return err
69
+ }
70
+
71
+ mux := http .NewServeMux ()
72
+ mux .HandleFunc ("/valueList" , valueListHandler )
73
+
74
+ ra .srv = & http.Server {
75
+ Addr : ":" + cfg .Port ,
76
+ Handler : mux ,
77
+ }
78
+
79
+ go func () {
80
+ if err := ra .srv .ListenAndServe (); ! errors .Is (err , http .ErrServerClosed ) {
81
+ plugin .Errorf ("%s plugin: ListenAndServe(): %v" , pluginName , err )
82
+ }
83
+ }()
84
+
85
+ return nil
86
+ }
87
+
88
+ func (ra * restapi ) Shutdown (ctx context.Context ) error {
89
+ if ra == nil || ra .srv == nil {
90
+ return nil
91
+ }
92
+
70
93
ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
71
94
defer cancel ()
72
95
73
- return api .srv .Shutdown (ctx )
96
+ return ra .srv .Shutdown (ctx )
74
97
}
75
98
76
99
func main () {} // ignored
0 commit comments