func (h *BackendAddressHandler) Get(ctx rest.Context) (int, interface{}) { if *useEtcd { client := etcd.NewClient([]string{*etcdAddr}) res, err := client.Get(backendListKey, false, false) if err != nil { log.Errorf("Error getting backend list: %v", err) return fallbackBackend() } // TODO(swsnider): retrieve backends via a map of shard to backend, rather // than just the first one in the list. return 200, map[string]string{"server": res.Node.Value} } return fallbackBackend() }
func (s *Square) UnmarshalJSON(data []byte) error { var blob halfDeserializedSquare if error := json.Unmarshal(data, &blob); error != nil { return error } s.ID = blob.ID var ok bool if s.Terrain, ok = terrainMap[blob.Terrain]; !ok { if glog.V(1) { glog.Errorf("Unknown Terrain %q. Using zero value %v", blob.Terrain, s.Terrain) } return fmt.Errorf("Unknown Terrain %q.", blob.Terrain) } s.X = blob.X s.Y = blob.Y return nil }
func (h *LoginHandler) Post(ctx rest.Context) (int, interface{}) { log.Infof("Handling login request %q") ar := AuthRequest{} ctx.Deserialize(&ar) status := 200 // Check for a cookie already present. c := getSessionCookie(ctx) if c == nil { c = &http.Cookie{} c.Name = authCookieName c.Value = simpleUUID4() // TODO(jwall): Session expiration? sess, err := h.ss.StartSession(c.Value) if err != nil { panic("Can't create user session. Something is very wrong!!!" + err.Error()) } sess.Values[usernameKey] = ar.Username err = h.ss.Save(sess) if err != nil { panic("Can't save user session. Something is very wrong!!!" + err.Error()) } } else { sess, err := h.ss.Get(c.Value) if err != nil || sess == nil { panic("Error Getting session " + err.Error()) } if ar.Username != sess.Values[usernameKey].(string) { // Status 409 Conflict. // There is a conflict with the current session username // and the requested login username. return 409, nil } } if ok, err := ctx.Auth.Authenticate(ar.Username, ar.Password); ok { ctx.Header().Add("Set-Cookie", c.String()) } else { log.Errorf("Unable to authenticate %q err %q", ar.Username, err) status = 403 } return status, nil }