mmg/handlers.go

56 lines
938 B
Go
Raw Normal View History

2013-05-08 22:57:52 -07:00
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
2013-05-08 22:57:52 -07:00
"net/http"
)
2013-05-08 23:21:18 -07:00
type prob struct {
Operation string
First int
2013-05-08 23:43:39 -07:00
Second int
2013-05-08 23:21:18 -07:00
}
2013-05-08 23:41:59 -07:00
type JsonHandler func(http.ResponseWriter, *http.Request)
func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
h(w, req)
}
2013-05-08 23:21:42 -07:00
func problem(w http.ResponseWriter, req *http.Request) {
operation := "+"
if r := rand.Intn(2); r == 0 {
operation = "-"
}
2013-05-08 23:21:18 -07:00
first := rand.Intn(MAX)
var second int
if operation == "-" {
if first == 0 {
second = 0
} else {
second = rand.Intn(first)
}
} else {
second = rand.Intn(MAX)
}
2013-05-08 23:21:18 -07:00
r := prob{operation, first, second}
b, err := json.Marshal(r)
if err != nil {
log.Fatal("issue with json marshalling")
}
2013-05-08 23:21:18 -07:00
j := string(b)
fmt.Println(j)
fmt.Fprintf(w, j)
2013-05-08 22:57:52 -07:00
}
2013-05-08 23:21:42 -07:00
func attempt(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "hello world")
2013-05-08 22:57:52 -07:00
}