From d3cbdfaf1ff7a53a25959c641a36a963aa941334 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Wed, 8 May 2013 23:11:03 -0700 Subject: [PATCH] returning random json objects from problem --- handlers.go | 23 +++++++++++++++++++++-- main.go | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/handlers.go b/handlers.go index 3652453..71a51de 100644 --- a/handlers.go +++ b/handlers.go @@ -1,13 +1,32 @@ package main import ( + "encoding/json" + "fmt" + "log" + "math/rand" "net/http" ) func attempt(w http.ResponseWriter, req *http.Request) { - w.Write([]byte("hello")) + operation := "+" + if r := rand.Intn(2); r == 0 { + operation = "-" + } + + r := map[string]string{ + "operation": operation, + } + + b, err := json.Marshal(r) + if err != nil { + log.Fatal("issue with json marshalling") + } + j := string(b) + fmt.Println(j) + fmt.Fprintf(w, j) } func problem(w http.ResponseWriter, req *http.Request) { - w.Write([]byte("hello")) + fmt.Fprintf(w, "hello world") } diff --git a/main.go b/main.go index 49cdfb5..f10e77c 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "github.com/gorilla/sessions" "log" "net/http" + "math/rand" + "time" ) var addr = flag.String("addr", ":8000", "address I'll listen on.") @@ -13,6 +15,7 @@ var static_files = flag.String("static", "./static", "location of static files") var store = sessions.NewCookieStore([]byte("hello world")) func main() { + rand.Seed( time.Now().UTC().UnixNano()) flag.Parse() http.Handle("/", http.FileServer(http.Dir(*static_files)))