Browse Source

Add /statsd_feedback to make graphing better after fetching/deleting feedbacks

master
Teran McKinney 2 years ago
parent
commit
1ea155727c
  1. 1
      demo.html
  2. 20
      rantbin.go
  3. 12
      test.sh

1
demo.html

@ -14,6 +14,7 @@
</form>
<h2>Demo with page</h2>
<form action="http://localhost:2222/feedback" method="post" autocomplete="off" target="_blank">
<!-- page is not required but helpful on sites with more than one page with this. -->
<input type="hidden" name="page" value="demo.html">
<input type="text" name="data" placeholder="Your rant here...">
<br/>

20
rantbin.go

@ -77,16 +77,14 @@ func countFeedbacks() (int, error) {
return len(feedbacks), err
}
func tooManyFeedbacks(numberOfFeedbacks int) error {
func tooManyFeedbacks(numberOfFeedbacks int) (err error) {
// Make sure we don't fill up disk with too many feedbacks.
// Return error string if too many feedbacks, nil if not.
var err error
if numberOfFeedbacks >= MAX_FILES {
log.Print("EXCEPTION")
log.Printf("%d feedbacks detected, max is %d", numberOfFeedbacks, MAX_FILES)
log.Printf("EXCEPTION: %d feedbacks detected, max is %d", numberOfFeedbacks, MAX_FILES)
err = errors.New("Too many feedbacks. Please contact the administrator.")
}
return err
return
}
func main() {
@ -102,6 +100,18 @@ func main() {
}
defer s.Close()
http.HandleFunc("/statsd_feedback", func(w http.ResponseWriter, r *http.Request) {
s.Increment("statsd_feedback.hit")
defer s.NewTiming().Send("statsd_feedback.time")
numberOfFeedbacks, err := countFeedbacks()
if err != nil {
http.Error(w, err.Error(), 500)
}
log.Printf("%d feedbacks.", numberOfFeedbacks)
s.Gauge("feedback_count", numberOfFeedbacks)
return
})
http.HandleFunc("/feedback", func(w http.ResponseWriter, r *http.Request) {
s.Increment("feedback.hit")
defer s.NewTiming().Send("feedback.time")

12
test.sh

@ -50,13 +50,13 @@ echo test | curl -s --show-error --fail --data-urlencode data@- "http://localhos
echo -n | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback && fail "Should error on empty input."
# This is.. approximate because it's URL encoded.
yes | head -c 4096 | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback || fail "Should allow a 4096 byte post."
yes 2> /dev/null | head -c 4096 | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback || fail "Should allow a 4096 byte post."
yes | head -c 4097 | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback && fail "Should fail on anything greater than 4096 bytes."
yes 2> /dev/null | head -c 4097 | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback && fail "Should fail on anything greater than 4096 bytes."
[ "$(wc -l test_dir/309a1668b23adc98b0ec1b67d55bdca1e89e9d81c0930d5baf9b85df85d76ee0)" != "4096" ] || fail "4096 byte file wrong length or checksum???"
yes | head -c 4096 | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback && fail "Shouldn't be able to write the same message twice."
yes 2> /dev/null | head -c 4096 | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback && fail "Shouldn't be able to write the same message twice."
ls -1 test_dir
@ -64,7 +64,7 @@ ls -1 test_dir
rm test_dir/*
for i in $(seq 1 128); do
yes | head -c "$i" | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback || fail "Should be able to add file number $i"
yes 2> /dev/null | head -c "$i" | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback || fail "Should be able to add file number $i"
done
echo test | curl -s --show-error --fail --data-urlencode data@- "http://localhost:2222"/feedback && fail "Should not be able to make more than 128 feedbacks."
@ -78,11 +78,11 @@ echo test | curl -s --show-error --fail -d page=/index.html --data-urlencode dat
[ -f "test_dir/35ebe6e8dab05c744864f4fcc3a21f1d16c01a79be0257169d82894ae879970b" ] || fail "Output file with page not created."
yes | tr -d '\n' | head -c 256 | curl -s --show-error --fail --data-urlencode page@- -d data=test "http://localhost:2222"/feedback || fail "Should be able to write with 256 byte page"
yes 2> /dev/null | tr -d '\n' | head -c 256 | curl -s --show-error --fail --data-urlencode page@- -d data=test "http://localhost:2222"/feedback || fail "Should be able to write with 256 byte page"
[ -f "test_dir/b287a5c7db0e96917aca6c9751a3f5e890d6d92ab093f826b6ed5d5c8948df41" ] || fail "Output file with longest page not created."
yes | tr -d '\n' | head -c 257 | curl -s --show-error --fail --data-urlencode page@- -d data=test "http://localhost:2222"/feedback && fail "Should NOT be able to write with 257 byte page"
yes 2> /dev/null | tr -d '\n' | head -c 257 | curl -s --show-error --fail --data-urlencode page@- -d data=test "http://localhost:2222"/feedback && fail "Should NOT be able to write with 257 byte page"
cleanup

Loading…
Cancel
Save