plugins/traffic-control: switch to http.ServeMux

It is nicer to instantiate a http.ServeMux, instead of modifying
the global http handler.
This commit is contained in:
Alessandro Puccetti
2016-09-03 14:36:22 +02:00
parent 3bd6e4c88d
commit 097225332a

View File

@@ -107,8 +107,22 @@ func main() {
if err != nil {
log.Fatalf("Failed to create a plugin: %v", err)
}
// Cache
trafficControlStatusCache = make(map[string]*TrafficControlStatus)
if err := plugin.Serve(listener); err != nil {
trafficControlServeMux := http.NewServeMux()
// Report request handler
reportHandler := http.HandlerFunc(plugin.report)
trafficControlServeMux.Handle("/report", reportHandler)
// Control request handler
controlHandler := http.HandlerFunc(plugin.control)
trafficControlServeMux.Handle("/control", controlHandler)
log.Println("Listening...")
if err = http.Serve(listener, trafficControlServeMux); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
@@ -157,13 +171,6 @@ func NewPlugin() (*Plugin, error) {
return plugin, nil
}
// Serve is a wrapper to http.ServeMux to serve the request supported by the plugin
func (p *Plugin) Serve(listener net.Listener) error {
http.HandleFunc("/report", p.report)
http.HandleFunc("/control", p.control)
return http.Serve(listener, nil)
}
func (p *Plugin) report(w http.ResponseWriter, r *http.Request) {
raw, err := p.reporter.RawReport()
if err != nil {