added a json handler

This commit is contained in:
Stephen McQuay 2013-05-08 23:41:59 -07:00
parent a8ceec3d6b
commit 97f18f1df2
3 changed files with 10 additions and 3 deletions

View File

@ -14,6 +14,13 @@ type prob struct {
Last int Last int
} }
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)
}
func problem(w http.ResponseWriter, req *http.Request) { func problem(w http.ResponseWriter, req *http.Request) {
operation := "+" operation := "+"
if r := rand.Intn(2); r == 0 { if r := rand.Intn(2); r == 0 {

View File

@ -21,8 +21,8 @@ func main() {
flag.Parse() flag.Parse()
http.Handle("/", http.Handle("/",
http.FileServer(http.Dir(*static_files))) http.FileServer(http.Dir(*static_files)))
http.HandleFunc("/api/v0/attempt/", attempt) http.HandleFunc("/api/v0/attempt/", JsonHandler(attempt))
http.HandleFunc("/api/v0/problem/", problem) http.Handle("/api/v0/problem/", JsonHandler(problem))
if err := http.ListenAndServe(*addr, nil); err != nil { if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err) log.Fatal("ListenAndServe:", err)
} }

View File

@ -7,7 +7,7 @@ function update_board(f, o, s, score) {
function new_problem() { function new_problem() {
$.get("/api/v0/problem/", function(d) { $.get("/api/v0/problem/", function(d) {
update_board(d["first"], d["operation"], d["second"], d["score"]); update_board(d["First"], d["Operation"], d["Second"], d["Score"]);
$("#answer").val(""); $("#answer").val("");
}) })
} }