Beispiel #1
0
func main() {
	arguments, _ := docopt.Parse(Usage, nil, true, "HBserver 0.3", false)

	if arguments["--expiry"] != nil {
		withCache = true
	}

	if withCache {
		cache.Init(arguments["--expiry"].(string))
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, arguments["--redir-host"].(string), http.StatusFound)
	})

	// Required for some ELBs
	http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
	})

	http.HandleFunc("/bleed/", bleedHandler)
	http.HandleFunc("/bleed/query", bleedQueryHandler)

	log.Printf("Starting server on %s\n", arguments["--listen"].(string))
	err := http.ListenAndServe(arguments["--listen"].(string), nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
func main() {
	var err error
	arguments, err := docopt.Parse(Usage, nil, true, "HBserver 0.3", false)
	if err != nil {
		log.Printf("ERROR: %s", err.Error())
		return
	}

	var statsd_server, statsd_prefix string

	if arguments["--expiry"] != nil {
		withCache = true
	}

	if _, ok := arguments["--cache.name"]; !ok {
		arguments["--cache.name"] = ""
	}

	if withCache {
		if arguments["--cache.name"].(string) != "" {
			cache.CACHE_TAB = arguments["--cache.name"].(string)
		}
		cache.Init(arguments["--expiry"].(string))
	}

	if _, ok := arguments["--listen"]; !ok {
		arguments["--listen"] = ":8082"
	}

	// Metric handling block
	if arguments["--metric.server"] != nil {
		if prefix, ok := arguments["metric.name"]; !ok {
			statsd_prefix = "heartbleed"
		} else {
			statsd_prefix = prefix.(string)
		}

		metrics = mz_metrics.New(statsd_prefix, statsd_server)
		http.HandleFunc("/metrics", func(w http.ResponseWriter,
			r *http.Request) {
			w.Header().Set("Content-Type", "application/json")
			snapshot := metrics.Snapshot()
			if snapshot == nil {
				w.Write([]byte("{}"))
				return
			}
			reply, err := json.Marshal(snapshot)
			if err != nil {
				log.Printf("handler ERROR: Could not generate report: %s",
					err.Error())
				jer := make(map[string]bool)
				jer["error"] = true
				reply, err = json.Marshal(jer)
			}
			w.Write(reply)
			return
		})
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, arguments["--redir-host"].(string), http.StatusFound)
	})

	// Required for some ELBs
	http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("OK"))
	})

	http.HandleFunc("/bleed/", bleedHandler)
	http.HandleFunc("/bleed/query", bleedQueryHandler)

	log.Printf("Starting server on %s\n", arguments["--listen"].(string))
	err = http.ListenAndServe(arguments["--listen"].(string), nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}