Example #1
0
func getUser(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
	}

	user.Latitude = 0.0
	user.Longitude = 0.0
	user.LocatedAt = user.CreatedAt
	user.UpdatedAt = user.CreatedAt

	bytes, err := json.Marshal(user)
	if err != nil {
		log.Println("Error marshaling user:"******"Error serializing data", 500)
		return
	}

	w.Header().Add("Content-Type", "application/json")
	fmt.Fprintln(w, string(bytes))
}
Example #2
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
}
func TestPostUser(t *testing.T) {
	resp := httptest.NewRecorder()
	uri := "/users/"
	user := datastore.User{
		PublicKey: "something",
		Latitude:  10.0,
		Longitude: 11.0,
	}
	jsonBytes, err := json.Marshal(user)
	if err != nil {
		t.Fatal("Error marshaling json:", err)
	}
	req, err := http.NewRequest("POST", uri, bytes.NewBuffer(jsonBytes))
	if err != nil {
		t.Fatal(err)
	}

	http.DefaultServeMux.ServeHTTP(resp, req)
	if resp.Code != 200 {
		t.Fatal("Expected 200 on user create but got", resp.Code)
	}

	p, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}

	var parsedUser datastore.User
	if err := json.Unmarshal(p, &parsedUser); err != nil {
		t.Fatal(err)
	}

	if parsedUser.Id == "" {
		t.Error("Error: parsed user has no ID")
	}

	if parsedUser.PublicKey != user.PublicKey {
		t.Error("Error: expected public key", user.PublicKey, "but got", parsedUser.PublicKey)
	}

	dbUser, err := datastore.GetUser(parsedUser.Id)
	if err != nil {
		t.Error("Error getting posted user:"******"Posted user not found in database")
	}

	if err := dbUser.Delete(); err != nil {
		t.Error("Error deleting user:", err)
	}
}
Example #4
0
func WebSocketServer(ws *websocket.Conn) {
	r := ws.Request()
	user, err := datastore.GetUser(r.URL.Path)
	if err != nil || user == nil {
		return
	}

	changeChannel := user.ChangeJSON()
	for {
		payload, ok := <-changeChannel
		if !ok {
			return
		}
		if _, err := fmt.Fprintln(ws, payload); err != nil {
			return // client probably closed socket
		}
	}
}
Example #5
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)
}