Exemple #1
0
// GetFeedConfig from current configuration settings.
func (p *Projector) GetFeedConfig() c.Config {
	p.rw.Lock()
	defer p.rw.Unlock()

	config, _ := c.NewConfig(map[string]interface{}{})
	config["clusterAddr"] = p.config["clusterAddr"] // copy by value.
	pconfig := p.config.SectionConfig("projector.", true /*trim*/)
	for _, key := range FeedConfigParams() {
		config.Set(key, pconfig[key])
	}
	return config
}
Exemple #2
0
// handle settings
func (p *Projector) handleSettings(w http.ResponseWriter, r *http.Request) {
	logging.Infof("%s Request %q %q\n", p.logPrefix, r.Method, r.URL.Path)
	switch r.Method {
	case "GET":
		header := w.Header()
		header["Content-Type"] = []string{"application/json"}
		fmt.Fprintf(w, "%s", string(p.GetConfig().Json()))

	case "POST":
		dataIn := make([]byte, r.ContentLength)
		// read settings
		if err := requestRead(r.Body, dataIn); err != nil {
			logging.Errorf("%v handleSettings() POST: %v\n", p.logPrefix, err)
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		// parse settings
		newConfig := make(map[string]interface{})
		if err := json.Unmarshal(dataIn, &newConfig); err != nil {
			fmsg := "%v handleSettings() json decoding: %v\n"
			logging.Errorf(fmsg, p.logPrefix, err)
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		// update projector settings
		logging.Infof("%v updating projector config ...\n", p.logPrefix)
		config, _ := c.NewConfig(newConfig)
		config.LogConfig(p.logPrefix)
		p.ResetConfig(config)
		// update feed settings
		feedConfig := config.SectionConfig("projector.", true /*trim*/)
		for _, feed := range p.GetFeeds() {
			if err := feed.ResetConfig(feedConfig); err != nil {
				fmsg := "%v feed(`%v`).ResetConfig: %v"
				logging.Errorf(fmsg, p.logPrefix, feed.topic, err)
			}
		}

	default:
		http.Error(w, "only GET POST supported", http.StatusMethodNotAllowed)
	}
}