func DebugPrintHttpRequest(r *http.Request) { // If debug logger is enabled, // print out all the details of the supplied HTTP request. if log.GetLevel() == log.DebugLevel { dump, err := httputil.DumpRequest(r, true) if err == nil { log.Debug("Request received: \n" + string(dump)) } else { log.Debug("Error reading request: " + err.Error()) } } }
func InitLogLevel() { // Check if --debug argument was supplied on the command line. // Check if LIVEPLANTDEBUG environment variable is present. // (Environment variable takes precedence over command line flag) // Enable or disable debug logger accordingly. // Declare and parse command line flag boolPtr := flag.Bool("debug", false, "Whether or not to enable debug logger.") flag.Parse() var debugLoggerEnabled bool = *boolPtr if len(os.Getenv("LIVEPLANTDEBUG")) > 0 { // Environment variable is present, so // debug logger should be enabled. // (overrides command line flag) debugLoggerEnabled = true } if debugLoggerEnabled { // Log everything log.SetLevel(log.DebugLevel) log.Debug("Debug logging enabled") } else { // Only log fatal or panic events // (events where the application is terminated) log.SetLevel(log.FatalLevel) } }
func PostVotes(w http.ResponseWriter, r *http.Request) { log.Debug("PostVotes called") DebugPrintHttpRequest(r) decoder := json.NewDecoder(r.Body) var vote Vote var message string err := decoder.Decode(&vote) if err == nil { if vote.Action == ActionWater { VoteCountWater++ message = fmt.Sprintf("Voted for action \"%s\" total count is %d", ActionWater, VoteCountWater) } else if vote.Action == ActionNothing { VoteCountNothing++ message = fmt.Sprintf("Voted for action \"%s\" Total Count: %d", ActionNothing, VoteCountNothing) } else { message = fmt.Sprintf("Encountered unhandled action \"%s\"", vote.Action) // TODO: return a standard error object w.WriteHeader(http.StatusBadRequest) } } else { message = "Error parsing vote body: " + err.Error() w.WriteHeader(http.StatusBadRequest) } log.Debug(message) json.NewEncoder(w).Encode(&VoteReceipt{ Message: message, Vote: vote, }) }