func gridHandler(w http.ResponseWriter, r *http.Request) { game.Reset() p.Tile = game.InitGrid() p.Size = game.GridSize() p.Win = game.Win() router(w, r, "grid", p) }
func moveHandler(w http.ResponseWriter, r *http.Request) { direction, err := ioutil.ReadAll(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } if strings.Contains(r.URL.Path, ".") { mux.ServeHTTP(w, r) } else { if r.Method == "GET" && r.URL.Path != "/" { http.Redirect(w, r, "/", http.StatusFound) return } p.Tile = game.Move(string(direction)) p.Win = game.Win() w.Header().Set("Content-Type", "application/json") enc := json.NewEncoder(w) err := enc.Encode(p) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } }