Esempio n. 1
0
func putUser(w http.ResponseWriter, r *http.Request) {
	dec := json.NewDecoder(r.Body)
	var user datastore.User
	if err := dec.Decode(&user); err != nil {
		log.Println("Error decoding user:"******"Error decoding user", 400)
		return
	}

	dbUser, err := datastore.GetUser(user.Id)
	if err != nil {
		http.Error(w, "User not found", 404)
		return
	}

	rsaKey, _ := dbUser.RSAKey()
	if ok := auth.Request(w, r, rsaKey); !ok {
		return
	}

	if user.PublicKey == "" {
		user.PublicKey = dbUser.PublicKey
	}

	if user.DeviceId == "" {
		user.DeviceId = dbUser.DeviceId
	}

	if user.Latitude == 0.0 && user.Longitude == 0.0 {
		user.Latitude = dbUser.Latitude
		user.Longitude = dbUser.Longitude
	}

	if user.LatestLocationId == "" {
		user.LatestLocationId = dbUser.LatestLocationId
	}

	if user.LocatedAt.Before(dbUser.LocatedAt) {
		user.LocatedAt = dbUser.LocatedAt
	}

	if err := user.Update(); err != nil {
		log.Println("Error updating user", user, err)
		http.Error(w, "Error updating user", 500)
	}

	w.Header().Add("Content-Type", "application/json")
	enc := json.NewEncoder(w)
	if err := enc.Encode(user); err != nil {
		log.Println("Error marshaling saved user:"******"Error marshaling user data", 500)
	}
	return
}
Esempio n. 2
0
func (l LocationsController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	user, err := datastore.GetUser(r.URL.Path)
	if err != nil {
		log.Println("Error getting user:"******"Error getting user", 500)
		return
	}
	if user == nil {
		http.Error(w, "User does not exist", 404)
		return
	}

	rsaKey, err := user.RSAKey()
	if err != nil {
		http.Error(w, "Error authenticating", 500)
		return
	}

	if ok := auth.Request(w, r, rsaKey); !ok {
		return
	}

	webSocketHandler.ServeHTTP(w, r)
}