From 8ae92884dd52a8f9bd888f0a1dcf88a20103d706 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 23:21:18 -0700 Subject: [PATCH] implemented problem route --- handlers.go | 22 +++++++++++++++++++--- main.go | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/handlers.go b/handlers.go index 71a51de..b53db79 100644 --- a/handlers.go +++ b/handlers.go @@ -8,21 +8,37 @@ import ( "net/http" ) +type prob struct { + Operation string + First int + Last int +} + func attempt(w http.ResponseWriter, req *http.Request) { operation := "+" if r := rand.Intn(2); r == 0 { operation = "-" } - r := map[string]string{ - "operation": operation, + first := rand.Intn(MAX) + var second int + if operation == "-" { + if first == 0 { + second = 0 + } else { + second = rand.Intn(first) + } + } else { + second = rand.Intn(MAX) } + r := prob{operation, first, second} + b, err := json.Marshal(r) if err != nil { log.Fatal("issue with json marshalling") } - j := string(b) + j := string(b) fmt.Println(j) fmt.Fprintf(w, j) } diff --git a/main.go b/main.go index f10e77c..15aaa09 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( "time" ) +const MAX = 12 + var addr = flag.String("addr", ":8000", "address I'll listen on.") var static_files = flag.String("static", "./static", "location of static files")