returning random json objects from problem

This commit is contained in:
Stephen McQuay 2013-05-08 23:11:03 -07:00
parent 60fc35e6c8
commit d3cbdfaf1f
2 changed files with 24 additions and 2 deletions

View File

@ -1,13 +1,32 @@
package main package main
import ( import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http" "net/http"
) )
func attempt(w http.ResponseWriter, req *http.Request) { 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) { func problem(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("hello")) fmt.Fprintf(w, "hello world")
} }

View File

@ -5,6 +5,8 @@ import (
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
"log" "log"
"net/http" "net/http"
"math/rand"
"time"
) )
var addr = flag.String("addr", ":8000", "address I'll listen on.") 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")) var store = sessions.NewCookieStore([]byte("hello world"))
func main() { func main() {
rand.Seed( time.Now().UTC().UnixNano())
flag.Parse() flag.Parse()
http.Handle("/", http.Handle("/",
http.FileServer(http.Dir(*static_files))) http.FileServer(http.Dir(*static_files)))